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
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
byfrom 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 forstalemore: it is still returned right away, and one caller refreshes it in the background. Afterfresh+staleit is gone, and the next call waits forcompute. -
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
defaultValuewhen missing or expired.defaultValuecan 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
byto 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
ttlmeans 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.
defaultValuecan be a function, like in get. -
put(
String key, Object? value, [Duration? ttl]) → Future< void> -
Store a value. Null
ttlmeans 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 forttland 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.
-
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
ttlon a key and keep its value. Nullttlmeans forever. True when the key was there.
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited