PersistentCache<V> class

纯持久化缓存(仅磁盘)

专注于 SharedPreferences 持久化存储,不包含内存缓存。 如需内存 + 磁盘组合,请使用 CacheStrategy 的 hybrid 模式。

使用示例:

final cache = PersistentCache<UserEntity>(
  cacheKey: 'user_cache',
  maxSize: 100,
  expiration: Duration(hours: 1),
  fromJson: UserEntity.fromJson,
  toJson: (e) => e.toJson(),
);

await cache.init();
await cache.put('user_1', user);
final user = await cache.get('user_1');

Constructors

PersistentCache({required String cacheKey, required int maxSize, required V fromJson(Map<String, dynamic> json), required Map<String, dynamic> toJson(V value), Duration? expiration})

Properties

cacheKey String
持久化存储的 key 前缀
final
expiration Duration?
过期时间(可选,null 表示永不过期)
final
fromJson → V Function(Map<String, dynamic> json)
JSON 反序列化函数
final
hasExpiration bool
是否启用过期时间
no setter
hashCode int
The hash code for this object.
no setterinherited
keys Iterable<String>
获取所有缓存的键
no setter
length int
获取缓存大小
no setter
maxSize int
最大缓存数量
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited
toJson Map<String, dynamic> Function(V value)
JSON 序列化函数
final

Methods

cleanExpired() Future<void>
清理过期数据
clear() Future<void>
清空所有缓存
containsKey(String key) bool
检查是否包含 key
get(String key) → V?
获取缓存数据(从磁盘读取)
init() Future<void>
初始化:从磁盘恢复索引
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
put(String key, V value) Future<void>
存入缓存数据(写入磁盘)
putAll(Map<String, V> entries) Future<void>
批量存入
remove(String key) Future<V?>
移除指定 key
toString() String
A string representation of this object.
override

Operators

operator ==(Object other) bool
The equality operator.
inherited