Skip to content

Instantly share code, notes, and snippets.

@jperkelens
Created November 19, 2012 19:53
Show Gist options
  • Save jperkelens/4113393 to your computer and use it in GitHub Desktop.
Save jperkelens/4113393 to your computer and use it in GitHub Desktop.
TinyCache
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]
}
}
}
@msuarz
Copy link

msuarz commented Nov 19, 2012

i'm trying to get find down to ...

find: (key) ->
try return cache[key]
finally addTimeout key

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment