mirror of
https://github.com/wanghongenpin/proxypin.git
synced 2026-03-28 06:49:47 +08:00
28 lines
507 B
Dart
28 lines
507 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? get(K key) {
|
|
return _cache[key];
|
|
}
|
|
|
|
remove(K key) {
|
|
_expirationTimes[key]?.cancel();
|
|
_expirationTimes.remove(key);
|
|
_cache.remove(key);
|
|
}
|
|
|
|
} |