Created
August 26, 2013 02:17
-
-
Save anonymous/6337588 to your computer and use it in GitHub Desktop.
Made with Gist Clipboard
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
module.exports = MemoryStore; | |
function MemoryStore(options) { | |
this.options = options || {}; | |
this.app = this.options.app; | |
this.cache = {}; | |
} | |
MemoryStore.prototype.cacheVersion = ''; | |
MemoryStore.prototype.get = function(key) { | |
var data; | |
if (!key) { | |
return; | |
} | |
data = this._get(key); | |
if (data && data.expires && Date.now() > data.expires) { | |
if (typeof console !== "undefined") { | |
console.log("MemoryStore: Expiring key \"" + key + "\"."); | |
} | |
this.clear(key); | |
data = undefined; | |
} else if (data && data.value) { | |
data = data.value; | |
} | |
return data; | |
}; | |
MemoryStore.prototype.set = function(key, value, ttlSec) { | |
var expires; | |
if (!key || value === undefined) { | |
return false; | |
} | |
expires = ttlSec ? Date.now() + ttlSec * 1000 : null; | |
this._set(key, { | |
value: value, | |
expires: expires | |
}); | |
return true; | |
}; | |
MemoryStore.prototype._get = function(key) { | |
return this.cache[this._formatKey(key)]; | |
}; | |
MemoryStore.prototype._set = function(key, data) { | |
this.cache[this._formatKey(key)] = data; | |
}; | |
MemoryStore.prototype._clear = function(key) { | |
delete this.cache[this._formatKey(key)]; | |
}; | |
MemoryStore.prototype._clearAll = function() { | |
this.cache = {}; | |
}; | |
MemoryStore.prototype.clear = function(key) { | |
if (key != null) { | |
return this._clear(key); | |
} else { | |
return this._clearAll(); | |
} | |
}; | |
MemoryStore.prototype._versionKey = function(key) { | |
return key + ":" + this.cacheVersion; | |
}; | |
MemoryStore.prototype._formatKey = function(key) { | |
return this._versionKey(key); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment