Cache class

Cache facade on top of dox_cache.

Stores come from AppConfig.cache. Each store is built once per isolate.

await Cache().put('foo', 'bar', duration: Duration(minutes: 5));
await Cache().store('redis').get('foo');
await Cache().tag('users').flush();

Values must be JSON types (Map, List, String, num, bool).

Constructors

Cache({CacheManager? manager})
manager is the dox_cache CacheManager. Default is Dox().cache.

Properties

hashCode → int
The hash code for this object.
no setterinherited
repository → CacheStore
The dox_cache store, for the methods that are not here.
no setter
runtimeType → Type
A representation of the runtime type of the object.
no setterinherited

Methods

add(String key, Object? value, {Duration? duration}) → Future<bool>
Store only when the key is missing. True when it was stored.
decrement(String key, [int by = 1]) → Future<int>
Take by from an integer. 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. For fresh the value is served as it is. After that, until fresh + stale, the old value is still served at once, and one caller runs compute in the background.
flush() → Future<void>
Remove all items of the store (or of the tag).
forever(String key, Object? value) → Future<void>
Store a value forever.
forget(String key) → Future<bool>
Remove a key.
get(String key, [Object? defaultValue]) → Future
Get a value, or defaultValue when it is missing.
getRaw(String key) → Future<String?>
The stored value as JSON text, with no decode. Null when missing. Fast way to send a cached value as a JSON response.
has(String key) → Future<bool>
True when the key has a value.
increment(String key, [int by = 1]) → Future<int>
Add by to an integer. A missing key starts at 0.
lock(String name, [Duration? ttl, String? owner]) → CacheLock
An atomic lock. Memory, file and Redis stores support it.
many(List<String> keys) → Future<Map<String, dynamic>>
Many values at once. A missing key gives null.
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
pull(String key, [Object? defaultValue]) → Future
Get a value and remove it.
put(String key, Object? value, {Duration? duration}) → Future<void>
Store a value. No duration means forever.
putMany(Map<String, Object?> values, {Duration? duration}) → Future<void>
Store many values. No duration means forever.
remember<T>(String key, Duration? duration, FutureOr<T> compute(), {T decode(Object? json)?, bool lock = false}) → Future<T>
The cached value, or run compute and store the result. The value is stored as JSON, so return maps, lists, strings and numbers, not models:
rememberForever<T>(String key, FutureOr<T> compute(), {T decode(Object? json)?, bool lock = false}) → Future<T>
Like remember, but the value never expires.
store(String store) → Cache
Use the store with this name from the config.
tag(String name) → Cache
Put the items under a tag, so you can flush only this tag.
tags(List<String> names) → Cache
Same as tag, with many tags.
toString() → String
A string representation of this object.
inherited
touch(String key, {Duration? duration}) → Future<bool>
Give key a new duration (null: forever). False when not found.

Operators

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

Static Methods

withAppPrefix(CacheConfig config, String appName) → CacheConfig
config where each Redis store with no prefix gets <app name>_cache:, so two apps on one Redis do not share keys. My Shop gives my_shop_cache:. An empty appName changes nothing, so the dox_cache default stays.