| OLD | NEW |
| (Empty) | |
| 1 part of angular.core; |
| 2 |
| 3 class CacheStats { |
| 4 final int capacity; |
| 5 final int size; |
| 6 final int hits; |
| 7 final int misses; |
| 8 CacheStats(this.capacity, this.size, this.hits, this.misses); |
| 9 String toString() => |
| 10 "[CacheStats: capacity: $capacity, size: $size, hits: $hits, misses: $miss
es]"; |
| 11 } |
| 12 |
| 13 |
| 14 /** |
| 15 * The Cache interface. |
| 16 */ |
| 17 abstract class Cache<K, V> { |
| 18 /** |
| 19 * Returns the value for `key` from the cache. If `key` is not in the cache, |
| 20 * returns `null`. |
| 21 */ |
| 22 V get(K key); |
| 23 /** |
| 24 * Inserts/Updates the `key` in the cache with `value` and returns the value. |
| 25 */ |
| 26 V put(K key, V value); |
| 27 /** |
| 28 * Removes `key` from the cache. If `key` isn't present in the cache, does |
| 29 * nothing. |
| 30 */ |
| 31 V remove(K key); |
| 32 /** |
| 33 * Removes all entries from the cache. |
| 34 */ |
| 35 void removeAll(); |
| 36 int get capacity; |
| 37 int get size; |
| 38 CacheStats stats(); |
| 39 } |
| 40 |
| 41 |
| 42 /** |
| 43 * An unbounded cache. |
| 44 */ |
| 45 class UnboundedCache<K, V> implements Cache<K, V> { |
| 46 Map<K, V> _entries = <K, V>{}; |
| 47 int _hits = 0; |
| 48 int _misses = 0; |
| 49 |
| 50 V get(K key) { |
| 51 V value = _entries[key]; |
| 52 if (value != null || _entries.containsKey(key)) { |
| 53 ++_hits; |
| 54 } else { |
| 55 ++_misses; |
| 56 } |
| 57 return value; |
| 58 } |
| 59 V put(K key, V value) => _entries[key] = value; |
| 60 V remove(K key) => _entries.remove(key); |
| 61 void removeAll() => _entries.clear(); |
| 62 int get capacity => 0; |
| 63 int get size => _entries.length; |
| 64 CacheStats stats() => new CacheStats(capacity, size, _hits, _misses); |
| 65 // Debugging helper. |
| 66 String toString() => "[$runtimeType: size=${_entries.length}, items=$_entries]
"; |
| 67 } |
| 68 |
| 69 |
| 70 /** |
| 71 * Simple LRU cache. |
| 72 * |
| 73 * TODO(chirayu): |
| 74 * - add docs |
| 75 * - add tests |
| 76 * - should stringify keys? |
| 77 */ |
| 78 class LruCache<K, V> extends Cache<K, V> { |
| 79 Map<K, V> _entries = new LinkedHashMap<K, V>(); |
| 80 int _capacity; |
| 81 int _hits = 0; |
| 82 int _misses = 0; |
| 83 |
| 84 LruCache({int capacity}) { |
| 85 this._capacity = (capacity == null) ? 0 : capacity; |
| 86 } |
| 87 |
| 88 V get(K key) { |
| 89 V value = _entries[key]; |
| 90 if (value != null || _entries.containsKey(key)) { |
| 91 ++_hits; |
| 92 // refresh |
| 93 _entries.remove(key); |
| 94 _entries[key] = value; |
| 95 } else { |
| 96 ++_misses; |
| 97 } |
| 98 return value; |
| 99 } |
| 100 |
| 101 V put(K key, V value) { |
| 102 // idempotent. needed to refresh an existing key. |
| 103 _entries.remove(key); |
| 104 // _capacity always > 0 but might not be true in some future. |
| 105 if (_capacity > 0 && _capacity == _entries.length) { |
| 106 // drop oldest entry when at capacity |
| 107 // _entries.keys.first is fairly cheap - 2 new calls. |
| 108 _entries.remove(_entries.keys.first); |
| 109 } |
| 110 _entries[key] = value; |
| 111 return value; |
| 112 } |
| 113 |
| 114 V remove(K key) => _entries.remove(key); |
| 115 void removeAll() => _entries.clear(); |
| 116 int get capacity => _capacity; |
| 117 int get size => _entries.length; |
| 118 CacheStats stats() => new CacheStats(capacity, size, _hits, _misses); |
| 119 // Debugging helper. |
| 120 String toString() => "[$runtimeType: capacity=$capacity, size=$size, items=$_e
ntries]"; |
| 121 } |
| OLD | NEW |