vyuh_cache
Cache<V> wraps CacheStorage<V> with a configured TTL. Use get for expiration-aware reads, build to retrieve or generate, set to store, remove to invalidate a key, and clear to remove stored entries. Concurrent build misses for a key share generation in the audited source.
How the pieces fit together
Choose a declaration
Cache · CacheConfig · CacheEntry · CacheStorage · CacheValueBuilder · MemoryCacheStorage
Cache
A generic cache implementation that manages data of type V. Provides comprehensive cache management functionality including configuration, storage operations (get, set, remove),and cache maintenance (build, clear).
final class Cache<V>Directly declared public members:
final CacheConfig<V> config;The cache configuration.
Cache(this.config);The cache implementation.
Future<V?> build(String key, {CacheValueBuilder<V>? generateValue});Build a value for the cache.
Future<V?> get(String key);Get a value from the cache.
Future<bool> has(String key);Check if a value is in the cache.
Future<void> set(String key, V value);Set a value in the cache.
Future<void> remove(String key);Remove a value from the cache.
Future<void> clear();Clear the cache.
CacheConfig
A configuration object for the cache.
class CacheConfig<V>Directly declared public members:
final CacheStorage<V> storage;The cache storage to use.
final Duration ttl;The time to live for the cache.
CacheConfig({required this.storage, required this.ttl});The cache config.
CacheEntry
A cache entry that encapsulates a value with time-based expiration.
final class CacheEntry<V>Directly declared public members:
final V value;The value of the cache entry.
final Duration ttl;The time to live for the cache entry.
CacheEntry(this.value, this.ttl);The cache entry.
bool get isExpired;Check if the cache entry is expired.
CacheStorage
A cache storage interface that defines the methods for a cache storage.
abstract interface class CacheStorage<T>Directly declared public members:
Future<CacheEntry<T>?> get(String key);Get a cache entry by its key.
Future<void> set(String key, CacheEntry<T> value);Set a cache entry by its key.
Future<void> delete(String key);Delete a cache entry by its key.
Future<void> clear();Clear the cache.
Future<List<String>> keys();Get all keys in the cache.
CacheValueBuilder
A function that builds a value for the cache.
typedef CacheValueBuilder<T> = Future<T> Function();MemoryCacheStorage
A simple in-memory cache storage implementation
final class MemoryCacheStorage<T> implements CacheStorage<T>Directly declared public members:
Future<void> clear();Future<void> delete(String key);Future<CacheEntry<T>?> get(String key);Future<List<String>> keys();Future<void> set(String key, CacheEntry<T> value);