-
-
Notifications
You must be signed in to change notification settings - Fork 193
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added status code white list and black listing
- Loading branch information
Kevin Whitley
committed
Oct 22, 2016
1 parent
fa26af4
commit 850a42d
Showing
4 changed files
with
308 additions
and
133 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
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,61 @@ | ||
function MemoryCache() { | ||
this.cache = {} | ||
this.size = 0 | ||
} | ||
|
||
MemoryCache.prototype.add = function(key, value, time, timeoutCallback) { | ||
var old = this.cache[key] | ||
var instance = this | ||
|
||
if (old) { | ||
clearTimeout(old.timeout) | ||
} | ||
|
||
var entry = { | ||
value: value, | ||
expire: time + Date.now(), | ||
timeout: setTimeout(function() { | ||
instance.delete(key) | ||
return timeoutCallback && typeof timeoutCallback === 'function' && timeoutCallback(value, key) | ||
}, time) | ||
} | ||
|
||
this.cache[key] = entry | ||
this.size = Object.keys(this.cache).length | ||
|
||
return entry | ||
} | ||
|
||
MemoryCache.prototype.delete = function(key) { | ||
var entry = this.cache[key] | ||
|
||
if (entry) { | ||
clearTimeout(entry.timeout) | ||
} | ||
|
||
delete this.cache[key] | ||
|
||
this.size = Object.keys(this.cache).length | ||
|
||
return null | ||
} | ||
|
||
MemoryCache.prototype.get = function(key) { | ||
var entry = this.cache[key] | ||
|
||
if (entry && entry.expire < Date.now()) { | ||
return this.delete(key) | ||
} | ||
|
||
return entry | ||
} | ||
|
||
MemoryCache.prototype.clear = function() { | ||
Object.keys(this.cache).forEach(function(key) { | ||
this.delete(key) | ||
}) | ||
|
||
return true | ||
} | ||
|
||
module.exports = MemoryCache |
Oops, something went wrong.