getOrAdd<T> method
Future<T>
getOrAdd<T>(
- String key, {
- required Future<
T> addValue(), - bool autoRemove = true,
- Duration expiration = const Duration(seconds: 60),
- bool listen = true,
override
Try to get a value from the cache by key. if the key is not in the cache, will be created.
Implementation
@override
Future<T> getOrAdd<T>(
String key, {
required Future<T> Function() addValue,
bool autoRemove = true,
Duration expiration = const Duration(seconds: 60),
bool listen = true,
}) async {
if (contains(key)) {
return await _cache[key] as T;
} else {
final value = await addValue();
add(
key,
value,
autoRemove: autoRemove,
expiration: expiration,
listen: listen,
);
if (listen) {
_notifyListeners();
}
return value;
}
}