1 ;(function () { // closure for web browsers
3 if (typeof module === 'object' && module.exports) {
4 module.exports = LRUCache
6 // just set the global for non-node platforms.
7 this.LRUCache = LRUCache
10 function hOP (obj, key) {
11 return Object.prototype.hasOwnProperty.call(obj, key)
14 function naiveLength () { return 1 }
16 function LRUCache (options) {
17 if (!(this instanceof LRUCache)) {
18 return new LRUCache(options)
22 if (typeof options === 'number') {
24 options = { max: max }
27 if (!options) options = {}
31 var lengthCalculator = options.length || naiveLength
33 if (typeof lengthCalculator !== "function") {
34 lengthCalculator = naiveLength
37 if (!max || !(typeof max === "number") || max <= 0 ) {
38 // a little bit silly. maybe this should throw?
42 var allowStale = options.stale || false
44 var maxAge = options.maxAge || null
46 var dispose = options.dispose
48 var cache = Object.create(null) // hash of items by key
49 , lruList = Object.create(null) // list of items in order of use recency
50 , mru = 0 // most recently used
51 , lru = 0 // least recently used
52 , length = 0 // number of items in the list
56 // resize the cache when the max changes.
57 Object.defineProperty(this, "max",
58 { set : function (mL) {
59 if (!mL || !(typeof mL === "number") || mL <= 0 ) mL = Infinity
61 // if it gets above double max, trim right away.
62 // otherwise, do it whenever it's convenient.
63 if (length > max) trim()
65 , get : function () { return max }
69 // resize the cache when the lengthCalculator changes.
70 Object.defineProperty(this, "lengthCalculator",
71 { set : function (lC) {
72 if (typeof lC !== "function") {
73 lengthCalculator = naiveLength
75 for (var key in cache) {
81 for (var key in cache) {
82 cache[key].length = lengthCalculator(cache[key].value)
83 length += cache[key].length
87 if (length > max) trim()
89 , get : function () { return lengthCalculator }
93 Object.defineProperty(this, "length",
94 { get : function () { return length }
99 Object.defineProperty(this, "itemCount",
100 { get : function () { return itemCount }
104 this.forEach = function (fn, thisp) {
105 thisp = thisp || this
107 for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) {
110 fn.call(thisp, hit.value, hit.key, this)
114 this.keys = function () {
115 var keys = new Array(itemCount)
117 for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) {
124 this.values = function () {
125 var values = new Array(itemCount)
127 for (var k = mru - 1; k >= 0 && i < itemCount; k--) if (lruList[k]) {
129 values[i++] = hit.value
134 this.reset = function () {
136 for (var k in cache) {
137 dispose(k, cache[k].value)
148 // Provided for debugging/dev purposes only. No promises whatsoever that
149 // this API stays stable.
150 this.dump = function () {
154 this.dumpLru = function () {
158 this.set = function (key, value) {
159 if (hOP(cache, key)) {
160 // dispose of the old one before overwriting
161 if (dispose) dispose(key, cache[key].value)
162 if (maxAge) cache[key].now = Date.now()
163 cache[key].value = value
168 var len = lengthCalculator(value)
169 var age = maxAge ? Date.now() : 0
170 var hit = new Entry(key, value, mru++, len, age)
172 // oversized objects fall out of cache automatically.
173 if (hit.length > max) {
174 if (dispose) dispose(key, value)
179 lruList[hit.lu] = cache[key] = hit
182 if (length > max) trim()
186 this.has = function (key) {
187 if (!hOP(cache, key)) return false
189 if (maxAge && (Date.now() - hit.now > maxAge)) {
195 this.get = function (key) {
196 if (!hOP(cache, key)) return
198 if (maxAge && (Date.now() - hit.now > maxAge)) {
200 return allowStale ? hit.value : undefined
204 lruList[hit.lu] = hit
208 this.del = function (key) {
213 while (lru < mru && length > max)
217 function shiftLU(hit) {
218 delete lruList[ hit.lu ]
219 while (lru < mru && !lruList[lru]) lru ++
224 if (dispose) dispose(hit.key, hit.value)
227 delete cache[ hit.key ]
233 // classy, since V8 prefers predictable objects.
234 function Entry (key, value, mru, len, age) {