UNPKG

rendr

Version:

Render your Backbone.js apps on the client and the server.

77 lines (63 loc) 1.68 kB
module.exports = MemoryStore; function MemoryStore(options) { this.options = options || {}; this.app = this.options.app; this.modelUtils = this.options.modelUtils; this.cache = {}; } MemoryStore.prototype.cacheVersion = ''; MemoryStore.prototype.get = function(key) { if (!key) { return; } return this.validateExpiration(key, this._get(key)); }; MemoryStore.prototype.validateExpiration = function(key, data) { 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); };