Created
May 7, 2014 15:12
-
-
Save RichFromGalvanize/c8d3b0010bf7e023c548 to your computer and use it in GitHub Desktop.
Simple Node.js Cache Module
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 = {}, | |
expires = 1000 * 60 * 60; // default expiration, 1 hour | |
var isValid = function(key) { | |
if(!cache.hasOwnProperty(key) || cache[key].expires <= Date.now()) { | |
return false; | |
} | |
return true; | |
}; | |
var set = function(key, value, expiration) { | |
expiration = expiration || expires; | |
cache[key] = { | |
expires: Date.now() + expiration, | |
data: value | |
}; | |
}; | |
var get = function(key) { | |
if(!cache.hasOwnProperty(key)) { | |
return null; | |
} | |
return cache[key].data; | |
}; | |
return { | |
isValid: isValid, | |
set: set, | |
get: get | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment