CacheStore class

The main cache API for one store. Wraps a CacheDriver.

Values must be JSON-encodable: Map, List, String, num, bool or null. They are stored as JSON, so a Map comes back as Map<String, dynamic> and a List as List<dynamic>. A stored null reads as a miss.

A null ttl means forever. A zero or negative ttl removes the key.

Constructors

CacheStore(CacheDriver driver, {CacheClock clock = const CacheClock()})

Properties

clock → CacheClock
Time source of flexible.
final
driver → CacheDriver
The backend of this store.
final
hashCode → int
The hash code for this object.
no setterinherited
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(String key, Object? value, [Duration? ttl]) → Future<bool>
Store only when the key is missing. True when stored.
decrement(String key, [int by = 1]) → Future<int>
Subtract by from an integer value. A missing key starts at 0.
flexible<T>(String key, Duration fresh, Duration? stale, FutureOr<T> compute(), {T decode(Object? json)?}) → Future<T>
Stale while revalidate. The value is fresh for fresh. After that it is stale for stale more: it is still returned right away, and one caller refreshes it in the background. After fresh + stale it is gone, and the next call waits for compute.
flush() → Future<void>
Remove every item of this store.
forever(String key, Object? value) → Future<void>
Store a value that never expires.
forget(String key) → Future<bool>
Remove a key. True when it existed.
get(String key, [Object? defaultValue]) → Future
The value, or defaultValue when missing or expired. defaultValue can be a function. It runs only on a miss: get('a', loadA).
getRaw(String key) → Future<String?>
The stored value as JSON text, with no decode, or null when missing. Use it to send a cached value as a JSON response as is. A stored String comes back with its quotes: "bar".
has(String key) → Future<bool>
True when the key has a non-null value. Does not decode the value.
increment(String key, [int by = 1]) → Future<int>
Add by to an integer value. A missing key starts at 0.
itemKey(String key) → FutureOr<String>
Key sent to the driver. Tagged caches change it. Plain stores return it right away, with no async step.
itemKeys(List<String> keys) → FutureOr<List<String>>
Keys sent to the driver for many keys. Tagged caches read the tag versions once for all keys.
lock(String name, [Duration? ttl, String? owner]) → CacheLock
An atomic lock. Supported by memory, file and redis drivers. Null ttl means the lock never expires by itself.
many(List<String> keys) → Future<Map<String, dynamic>>
Many values at once. Missing keys map to null.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pull(String key, [Object? defaultValue]) → Future
Get the value and remove it. The built-in drivers do this in one atomic step. defaultValue can be a function, like in get.
put(String key, Object? value, [Duration? ttl]) → Future<void>
Store a value. Null ttl means forever.
putMany(Map<String, Object?> values, [Duration? ttl]) → Future<void>
Store many values with the same ttl.
remember<T>(String key, Duration? ttl, FutureOr<T> compute(), {T decode(Object? json)?, bool lock = false}) → Future<T>
The cached value, or run compute, store its result for ttl and return it.
rememberForever<T>(String key, FutureOr<T> compute(), {T decode(Object? json)?, bool lock = false}) → Future<T>
Like remember, but the value never expires.
tags(List<String> names) → TaggedCache
A view of this store where keys belong to names. tags(['a']).flush() removes only items stored with tag 'a'.
toString() → String
A string representation of this object.
inherited
touch(String key, Duration? ttl) → Future<bool>
Set a new ttl on a key and keep its value. Null ttl means forever. True when the key was there.

Operators

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