Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(288)

Side by Side Diff: pkg/analysis_server/lib/src/index/lru_cache.dart

Issue 329633002: Some clean ups for caching tests. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/analysis_server/lib/src/index/page_node_manager.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library index.lru_cache; 5 library index.lru_cache;
6 6
7 import 'dart:collection'; 7 import 'dart:collection';
8 8
9 9
10 /** 10 /**
11 * This handler is notified when an item is evicted from the cache. 11 * This handler is notified when an item is evicted from the cache.
12 */ 12 */
13 typedef EvictionHandler<K, V>(K key, V value); 13 typedef EvictionHandler<K, V>(K key, V value);
14 14
15 /** 15 /**
16 * A hash-table based cache implementation. 16 * A hash-table based cache implementation.
17 * 17 *
18 * When it reaches the specified number of items, the item that has not been 18 * When it reaches the specified number of items, the item that has not been
19 * accessed recently is evicted. 19 * accessed (both get and put) recently is evicted.
20 */ 20 */
21 class LRUCache<K, V> { 21 class LRUCache<K, V> {
22 final LinkedHashSet<K> _lastKeys = new LinkedHashSet<K>(); 22 final LinkedHashSet<K> _lastKeys = new LinkedHashSet<K>();
23 final HashMap<K, V> _map = new HashMap<K, V>(); 23 final HashMap<K, V> _map = new HashMap<K, V>();
24 final int _maxSize; 24 final int _maxSize;
25 final EvictionHandler _handler; 25 final EvictionHandler _handler;
26 26
27 LRUCache(this._maxSize, [this._handler]); 27 LRUCache(this._maxSize, [this._handler]);
28 28
29 /** 29 /**
(...skipping 17 matching lines...) Expand all
47 _map.remove(key); 47 _map.remove(key);
48 } 48 }
49 49
50 /** 50 /**
51 * Associates the [key] with the given [value]. 51 * Associates the [key] with the given [value].
52 * 52 *
53 * If the cache is full, an item that has not been accessed recently is 53 * If the cache is full, an item that has not been accessed recently is
54 * evicted. 54 * evicted.
55 */ 55 */
56 void put(K key, V value) { 56 void put(K key, V value) {
57 _lastKeys.remove(key);
57 _lastKeys.add(key); 58 _lastKeys.add(key);
58 if (_lastKeys.length > _maxSize) { 59 if (_lastKeys.length > _maxSize) {
59 K evictedKey = _lastKeys.first; 60 K evictedKey = _lastKeys.first;
60 V evictedValue = _map.remove(evictedKey); 61 V evictedValue = _map.remove(evictedKey);
61 _lastKeys.remove(evictedKey); 62 _lastKeys.remove(evictedKey);
62 if (_handler != null) { 63 if (_handler != null) {
63 _handler(evictedKey, evictedValue); 64 _handler(evictedKey, evictedValue);
64 } 65 }
65 } 66 }
66 _map[key] = value; 67 _map[key] = value;
67 } 68 }
68 } 69 }
OLDNEW
« no previous file with comments | « no previous file | pkg/analysis_server/lib/src/index/page_node_manager.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698