This repository has been archived by the owner on Oct 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#10 use injectable Promise implementation in caches
- Loading branch information
Showing
4 changed files
with
88 additions
and
59 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 |
---|---|---|
@@ -1,63 +1,66 @@ | ||
var clone = require('clone'); | ||
|
||
module.exports = function(maxEntries) { | ||
return new LRUCache(maxEntries); | ||
}; | ||
module.exports = function(maxEntries, Promise) { | ||
Promise = Promise || global.Promise; | ||
|
||
/** | ||
* A simple in-memory good guy cache implementation that has a cap on the maximum number of cached entries. | ||
* If that number is exceeded, least recently used entries are dropped. | ||
* @param maxEntries the maximum number of entries | ||
* @constructor | ||
*/ | ||
function LRUCache(maxEntries) { | ||
this.maxEntries = maxEntries || 500; | ||
this._entryOrder = []; | ||
this._entryMap = {}; | ||
} | ||
LRUCache.prototype = { | ||
retrieve: function(key) { | ||
var entry = this._entryMap[key]; | ||
if (entry !== undefined) { | ||
this._markAsRecent(key); | ||
return Promise.resolve(clone(entry)); | ||
} else { | ||
return Promise.resolve(undefined); | ||
} | ||
}, | ||
|
||
store: function(key, value) { | ||
value = clone(value); | ||
var entry = this._entryMap[key]; | ||
if (entry !== undefined) { | ||
this._entryMap[key] = value; | ||
this._markAsRecent(key); | ||
} else { | ||
this._entryMap[key] = value; | ||
this._entryOrder.unshift(key); | ||
if (this._entryOrder.length > this.maxEntries) | ||
this._discardLeastRecentlyUsed(); | ||
} | ||
/** | ||
* A simple in-memory good guy cache implementation that has a cap on the maximum number of cached entries. | ||
* If that number is exceeded, least recently used entries are dropped. | ||
* @param maxEntries the maximum number of entries | ||
* @constructor | ||
*/ | ||
function LRUCache(maxEntries) { | ||
this.maxEntries = maxEntries || 500; | ||
this._entryOrder = []; | ||
this._entryMap = {}; | ||
} | ||
LRUCache.prototype = { | ||
retrieve: function (key) { | ||
var entry = this._entryMap[key]; | ||
if (entry !== undefined) { | ||
this._markAsRecent(key); | ||
return Promise.resolve(clone(entry)); | ||
} else { | ||
return Promise.resolve(undefined); | ||
} | ||
}, | ||
|
||
return Promise.resolve(); | ||
}, | ||
store: function (key, value) { | ||
value = clone(value); | ||
var entry = this._entryMap[key]; | ||
if (entry !== undefined) { | ||
this._entryMap[key] = value; | ||
this._markAsRecent(key); | ||
} else { | ||
this._entryMap[key] = value; | ||
this._entryOrder.unshift(key); | ||
if (this._entryOrder.length > this.maxEntries) | ||
this._discardLeastRecentlyUsed(); | ||
} | ||
|
||
evict: function(key) { | ||
delete this._entryMap[key]; | ||
return Promise.resolve(); | ||
}, | ||
return Promise.resolve(); | ||
}, | ||
|
||
_markAsRecent: function(key) { | ||
var index = this._entryOrder.indexOf(key); | ||
if (index >= 0) { | ||
this._entryOrder.splice(index, 1); | ||
this._entryOrder.unshift(key); | ||
evict: function (key) { | ||
delete this._entryMap[key]; | ||
return Promise.resolve(); | ||
}, | ||
|
||
_markAsRecent: function (key) { | ||
var index = this._entryOrder.indexOf(key); | ||
if (index >= 0) { | ||
this._entryOrder.splice(index, 1); | ||
this._entryOrder.unshift(key); | ||
} | ||
}, | ||
|
||
_discardLeastRecentlyUsed: function () { | ||
var key = this._entryOrder.pop(); | ||
delete this._entryMap[key]; | ||
} | ||
}, | ||
}; | ||
|
||
_discardLeastRecentlyUsed: function() { | ||
var key = this._entryOrder.pop(); | ||
delete this._entryMap[key]; | ||
} | ||
return new LRUCache(maxEntries); | ||
}; | ||
|
||
|
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
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
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