Created
November 19, 2012 19:53
-
-
Save jperkelens/4113393 to your computer and use it in GitHub Desktop.
TinyCache
This file contains hidden or 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
module.exports = function() { | |
var cache = {} | |
var timeouts = {} | |
function replaceTimeout(key) { | |
clearTimeout(timeouts[key]) | |
timeouts[key] = setTimeout(function() { | |
delete cache[key] | |
}, 5000) | |
} | |
return { | |
put: function(key, value) { | |
cache[key] = value | |
replaceTimeout(key) | |
}, | |
find: function(key) { | |
if (!cache[key]) return null | |
replaceTimeout(key) | |
return cache[key] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
i'm trying to get find down to ...
find: (key) ->
try return cache[key]
finally addTimeout key