Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(LRUCache): Support zero-length caches
Browse files Browse the repository at this point in the history
  • Loading branch information
jbdeboer committed Apr 29, 2014
1 parent 1292aac commit 3e60863
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
7 changes: 3 additions & 4 deletions lib/core/cache.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class LruCache<K, V> extends Cache<K, V> {
int _misses = 0;

LruCache({int capacity}) {

This comment has been minimized.

Copy link
@vicb

vicb May 10, 2014

Contributor

this._capacity as arg ?

this._capacity = (capacity == null) ? 0 : capacity;
this._capacity = capacity;
}

V get(K key) {
Expand All @@ -101,13 +101,12 @@ class LruCache<K, V> extends Cache<K, V> {
V put(K key, V value) {
// idempotent. needed to refresh an existing key.
_entries.remove(key);
// _capacity always > 0 but might not be true in some future.
if (_capacity > 0 && _capacity == _entries.length) {
_entries[key] = value;
if (_capacity != null && _capacity < _entries.length) {
// drop oldest entry when at capacity
// _entries.keys.first is fairly cheap - 2 new calls.
_entries.remove(_entries.keys.first);
}
_entries[key] = value;
return value;
}

Expand Down
3 changes: 2 additions & 1 deletion lib/core_dom/module_internal.dart
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ class CoreDomModule extends Module {
value(dom.Window, dom.window);
value(ElementProbe, null);

factory(TemplateCache, (_) => new TemplateCache(capacity: 0));
// Default to a unlimited-sized TemplateCache
factory(TemplateCache, (_) => new TemplateCache());
type(dom.NodeTreeSanitizer, implementedBy: NullTreeSanitizer);

type(TextMustache);
Expand Down
6 changes: 6 additions & 0 deletions test/core/cache_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ void main() {
expect(stats.hits).toEqual(2);
expect(stats.misses).toEqual(1);
}));

it('should hold nothing if capacity is zero', () {
var cache = new LruCache<int, int>(capacity: 0);
cache.put(1, 10);
expect(cache.get(1)).toBeNull();
});
});
});
}

0 comments on commit 3e60863

Please sign in to comment.