remember method
Retrieves a value from the cache or stores the default value if it doesn't exist.
This is a common caching pattern that checks for a cached value first, and if not found, executes the callback function to compute the value, stores it in the cache, and returns it.
The callback function should return a Future that resolves to the value
to be cached.
Throws CacheException if the key is empty or if any operation fails.
Implementation
@override
Future<dynamic> remember(
String key,
Duration ttl,
Future<dynamic> Function() callback,
) async {
_validator.validateKey(key);
_validator.validateTtl(ttl);
try {
if (await has(key)) {
return await get(key);
}
final value = await callback();
await put(key, value, ttl);
return value;
} catch (e) {
throw CacheException('Failed to remember cache item "$key": $e');
}
}