store method
Stores value in the cache with expiration time.
correlationId
(optional) transaction id to trace execution through call chain.key
a unique value key.value
a value to store.timeout
expiration timeout in milliseconds. Return Future that receives an null for success Throws error
Implementation
@override
Future<dynamic> store(
String? correlationId, String? key, value, int? timeout) async {
if (key == null) {
var err = Exception('Key cannot be null');
throw err;
}
// Get the entry
CacheEntry? entry = _cache[key]; //.cast<CacheEntry>();
// Shortcut to remove entry from the cache
if (value == null) {
if (entry != null) {
_cache.remove(key);
_count--;
}
return value;
}
timeout = timeout != null && timeout > 0 ? timeout : _timeout;
// Update the entry
if (entry != null) {
entry.setValue(value, timeout);
}
// Or create a new entry
else {
entry = CacheEntry(key, value, timeout);
_cache[key] = entry;
_count++;
}
// Clean up the cache
if (_maxSize > 0 && _count > _maxSize) _cleanup();
return value;
}