RedisCache class

Cache backed by Redis — for shared / distributed caching across processes and hosts.

Values are serialized with jsonEncode on set and decoded on get, so anything jsonEncode accepts round-trips (strings, numbers, booleans, nulls, and Map/List of those). Pass prefix to namespace this cache — [clear] will only drop keys under that prefix.

final cache = await RedisCache.connect(host: 'localhost', port: 6379, prefix: 'app:');
await cache.set('user:42', {'name': 'Eva'}, ttl: Duration(minutes: 5));
final user = await cache.get<Map<String, dynamic>>('user:42');
await cache.close();
Implemented types
Available extensions

Properties

hashCode int
The hash code for this object.
no setterinherited
prefix String
Prefix prepended to every key, e.g. 'app:'. Empty means "no prefix".
final
runtimeType Type
A representation of the runtime type of the object.
no setterinherited

Methods

clear() Future<void>
Drops every key under prefix. When prefix is empty this calls FLUSHDBwipes the whole database. Otherwise it iterates with SCAN + batched DELs so it doesn't block the server on big keyspaces.
override
close() Future<void>
Releases underlying resources (Redis connection, timers, etc.).
override
delete(String key) Future<bool>
Removes key. Returns true if it was present, false otherwise.
override
get<T>(String key) Future<T?>
Returns the cached value, or null when the key is absent or expired.
override
has(String key) Future<bool>
Whether key is currently present (and unexpired).
override
noSuchMethod(Invocation invocation) → dynamic
Invoked when a nonexistent method or property is accessed.
inherited
remember<T>(String key, {Duration? ttl, required Future<T?> builder()}) Future<T?>

Available on Cache, provided by the CacheRemember extension

set<T>(String key, T value, {Duration? ttl}) Future<void>
Stores value under key, replacing any previous entry. When ttl is null the entry has no expiration; readers may still evict it (LRU).
override
toString() String
A string representation of this object.
inherited

Operators

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

Static Methods

connect({String host = 'localhost', int port = 6379, String prefix = ''}) Future<RedisCache>
Opens a TCP connection to a Redis server.