35 lines
761 B
Dart
35 lines
761 B
Dart
import 'dart:collection';
|
|
|
|
typedef EvictionHandler<K, V> = Function(K key, V value);
|
|
|
|
class LRUMap<K, V> {
|
|
final LinkedHashMap<K, V> _map = LinkedHashMap<K, V>();
|
|
final int _maxSize;
|
|
final EvictionHandler<K, V?>? _handler;
|
|
|
|
LRUMap(this._maxSize, [this._handler]);
|
|
|
|
V? get(K key) {
|
|
final V? value = _map.remove(key);
|
|
if (value != null) {
|
|
_map[key] = value;
|
|
}
|
|
return value;
|
|
}
|
|
|
|
void put(K key, V value) {
|
|
_map.remove(key);
|
|
_map[key] = value;
|
|
if (_map.length > _maxSize) {
|
|
final K evictedKey = _map.keys.first;
|
|
final V? evictedValue = _map.remove(evictedKey);
|
|
if (_handler != null) {
|
|
_handler!(evictedKey, evictedValue);
|
|
}
|
|
}
|
|
}
|
|
|
|
void remove(K key) {
|
|
_map.remove(key);
|
|
}
|
|
}
|