Created
November 19, 2013 16:25
-
-
Save matthewhudson/7548045 to your computer and use it in GitHub Desktop.
Simple in-memory caching. Defaults to 2s
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
# 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