getOrCreate<T> method
Gets the value associated with key, or creates and caches a new
value using factory if not found.
The factory function receives an ICacheEntry that can be configured
with expiration settings.
Example:
final value = cache.getOrCreate<String>('key', (entry) {
entry.slidingExpiration = Duration(minutes: 5);
return 'computed value';
});
Implementation
T getOrCreate<T>(Object key, T Function(ICacheEntry entry) factory) {
T? result;
if (tryGetValue<T>(key, (value) => result = value)) {
return result as T;
}
final entry = createEntry(key);
final value = factory(entry);
entry.value = value;
_commitEntry(entry);
return value;
}