PVCache class
A configurable cache instance with hook-based extensibility.
Each cache has its own environment namespace and supports custom hooks for TTL, encryption, LRU, and other behaviors.
Example - Basic cache:
final cache = PVCache(
env: 'dev',
hooks: [],
defaultMetadata: {},
);
await cache.put('user:123', {'name': 'Alice'});
Example - Cache with TTL:
final cache = PVCache(
env: 'prod',
hooks: [createTTLHook()],
defaultMetadata: {},
);
await cache.put('session', token, metadata: {'ttl_seconds': 3600});
Example - Cache with macro get (pattern-based auto-fetch):
final cache = PVCache(
env: 'prod',
hooks: [createTTLHook()],
defaultMetadata: {},
macroGetHandlers: {
RegExp(r'^user:\d+$'): (key) async {
final userId = key.split(':')[1];
return await api.fetchUser(userId);
},
},
);
// Automatically fetches from API if not cached
final user = await cache.get('user:123');
Constructors
-
PVCache({required String env, required List<
PVCacheHook> hooks, required Map<String, dynamic> defaultMetadata, StorageType entryStorageType = StorageType.stdSembast, StorageType metadataStorageType = StorageType.stdSembast, bool noMetadataStoreIfEmpty = false, String metadataNameFunction(String)?, Map<Pattern, Future Function(String key)> macroGetHandlers = const {}, Map<String, dynamic> macroGetDefaultMetadata = const {}, bool heavy = false}) - Create a new cache instance.
Properties
-
defaultMetadata
→ Map<
String, dynamic> -
Default metadata applied to all operations if not overridden.
final
- entryStorageType → StorageType
-
Storage backend for cache entries.
final
- env → String
-
Environment name for this cache instance.
final
- hashCode → int
-
The hash code for this object.
no setterinherited
- heavy → bool
-
whether this cache is considered heavy
only useful for non-web platforms, this will create separate databases to provide on demand loading
final
-
macroGetDefaultMetadata
→ Map<
String, dynamic> -
Default metadata to apply when macro get fetches and caches data.
final
-
macroGetHandlers
→ Map<
Pattern, Future Function(String key)> -
Pattern-based auto-fetch handlers.
final
- metadataNameFunction → String Function(String)?
-
Function to generate metadata store name. Defaults to '${env}_metadata'.
final
- metadataStorageType → StorageType
-
Storage backend for metadata. Can differ from entryStorageType.
final
- noMetadataStoreIfEmpty → bool
-
If true, don't create metadata entry when metadata map is empty.
final
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
clear(
{Map< String, dynamic> ? metadata}) → Future<void> - Clear all entries from the cache.
-
delete(
String key, {Map< String, dynamic> ? metadata}) → Future<void> - Delete a value from the cache.
-
exists(
String key, {Map< String, dynamic> ? metadata}) → Future<bool> - Check if a key exists in the cache.
-
get(
String key, {Map< String, dynamic> ? metadata}) → Future - Retrieve a value from the cache.
-
ifNotCached<
T> (String key, Future< T?> compute(), {Map<String, dynamic> ? metadata}) → Future<T?> - Get cached value or compute and cache if missing.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
put(
String key, dynamic value, {Map< String, dynamic> ? metadata}) → Future<void> - Store a value in the cache.
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited