mirror of
https://github.com/flame-engine/flame.git
synced 2025-11-15 20:29:46 +08:00
28 lines
569 B
Dart
28 lines
569 B
Dart
import 'dart:collection';
|
|
|
|
/// Simple class to cache values on the cache
|
|
///
|
|
class MemoryCache<K, V> {
|
|
final LinkedHashMap<K, V> _cache = LinkedHashMap();
|
|
final int cacheSize;
|
|
|
|
MemoryCache({this.cacheSize = 10});
|
|
|
|
void setValue(K key, V value) {
|
|
if (!_cache.containsKey(key)) {
|
|
_cache[key] = value;
|
|
|
|
while (_cache.length > cacheSize) {
|
|
final k = _cache.keys.first;
|
|
_cache.remove(k);
|
|
}
|
|
}
|
|
}
|
|
|
|
V getValue(K key) => _cache[key];
|
|
|
|
bool containsKey(K key) => _cache.containsKey(key);
|
|
|
|
int get size => _cache.length;
|
|
}
|