mirror of
https://github.com/wanghongenpin/proxypin.git
synced 2026-03-21 05:39:47 +08:00
37 lines
697 B
Dart
37 lines
697 B
Dart
|
|
|
|
import 'dart:async';
|
|
|
|
class ExpiringCache<K, V> {
|
|
final Duration duration;
|
|
final _cache = <K, V>{};
|
|
final _expirationTimes = <K, Timer>{};
|
|
|
|
ExpiringCache(this.duration);
|
|
|
|
void set(K key, V value) {
|
|
_expirationTimes[key]?.cancel();
|
|
_cache[key] = value;
|
|
_expirationTimes[key] = Timer(duration, () => remove(key));
|
|
}
|
|
|
|
V? putIfAbsent(K key, V Function() ifAbsent) {
|
|
if (_cache.containsKey(key)) {
|
|
return _cache[key];
|
|
}
|
|
final value = ifAbsent();
|
|
set(key, value);
|
|
return value;
|
|
}
|
|
|
|
V? get(K key) {
|
|
return _cache[key];
|
|
}
|
|
|
|
remove(K key) {
|
|
_expirationTimes[key]?.cancel();
|
|
_expirationTimes.remove(key);
|
|
_cache.remove(key);
|
|
}
|
|
|
|
} |