Skip to content

Commit

Permalink
introduce Ember._Cache still private API
Browse files Browse the repository at this point in the history
  • Loading branch information
stefanpenner committed Jul 24, 2014
1 parent d6d0cc5 commit fde1505
Showing 1 changed file with 55 additions and 0 deletions.
55 changes: 55 additions & 0 deletions packages/ember-metal/lib/cache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import dictionary from 'ember-metal/dictionary';
export default Cache;

function Cache(limit, func) {
this.store = dictionary(null);
this.size = 0;
this.misses = 0;
this.hits = 0;
this.limit = limit;
this.func = func;
}

var FALSE = function() { };
var ZERO = function() { };
var UNDEFINED = function() { };
var NULL = function() { };

Cache.prototype = {
set: function(key, value) {
if (this.limit > this.size) {
this.size ++;
if (value === undefined) {
this.store[key] = UNDEFINED;
} else {
this.store[key] = value;
}
}

return value;
},

get: function(key) {
var value = this.store[key];

if (value === undefined) {
this.misses ++;
value = this.set(key, this.func(key));
} else if (value === UNDEFINED) {
this.hits ++;
value = UNDEFINED;
} else {
this.hits ++;
// nothing to translate
}

return value;
},

purge: function() {
this.store = dictionary(null);
this.size = 0;
this.hits = 0;
this.misses = 0;
}
};

0 comments on commit fde1505

Please sign in to comment.