getAndPut method
Associates the specified value
with the specified key
in this cache,
returning an existing value if one existed. If the cache previously contained
a mapping for the key
, the old value is replaced by the specified value
key
: key with which the specified value is to be associatedvalue
: value to be associated with the specified key
The previous value is returned, or null
if there was no value
associated with the key
previously.
Implementation
@override
Future<dynamic> getAndPut(String key, dynamic value,
{Duration? expiryDuration}) {
// Try to get the entry from the cache
return _getStorageEntry(key).then((entry) {
final now = clock.now();
var expired = entry != null && entry.isExpired(now);
// If the entry exists on cache but is already expired we remove it first
final pre = expired
? _removeStorageEntry(key, ExpiredEntryEvent(this, entry!))
: Future.value();
// If the entry is expired or non existent
if (entry == null || expired) {
return pre.then((_) =>
_putEntry(key, value, now, expiryDuration).then((v) => null));
} else {
return pre.then((_) => _updateEntry(entry, value, now, expiryDuration));
}
});
}