Skip to content

Instantly share code, notes, and snippets.

@RichFromGalvanize
Created May 7, 2014 15:12
Show Gist options
  • Save RichFromGalvanize/c8d3b0010bf7e023c548 to your computer and use it in GitHub Desktop.
Save RichFromGalvanize/c8d3b0010bf7e023c548 to your computer and use it in GitHub Desktop.
Simple Node.js Cache Module
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