-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
introduce Ember._Cache still private API
- Loading branch information
1 parent
d6d0cc5
commit fde1505
Showing
1 changed file
with
55 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
}; |