Skip to content

Instantly share code, notes, and snippets.

@matthewhudson
Created November 19, 2013 16:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matthewhudson/7548045 to your computer and use it in GitHub Desktop.
Save matthewhudson/7548045 to your computer and use it in GitHub Desktop.
Simple in-memory caching. Defaults to 2s
# Simple in-memory caching. Defaults to 2s
# http://jsperf.com/date-now-vs-new-date-gettime/4
store = {}
cache = {}
cache.set = (key, value, milliseconds) ->
if not store[key]?
store[key] = value
setTimeout () ->
delete store[key]
, Date.now() + (milliseconds or 2000)
cache.get = (key) ->
store[key] or null
cache.flush = ->
store = {}
cache.size = ->
Object.keys(store).length
exports = module.exports = cache
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment