You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
1.8 KiB
84 lines
1.8 KiB
var Lru = require("lru-cache");
|
|
|
|
var memoryStore = function(args) {
|
|
args = args || {};
|
|
var self = {};
|
|
self.name = 'memory';
|
|
var ttl = args.ttl;
|
|
var lruOpts = {
|
|
max: args.max || 500,
|
|
maxAge: ttl ? ttl * 1000 : null,
|
|
dispose: args.dispose,
|
|
length: args.length,
|
|
stale: args.stale
|
|
};
|
|
|
|
var lruCache = new Lru(lruOpts);
|
|
|
|
self.set = function(key, value, options, cb) {
|
|
if (typeof options === 'function') {
|
|
cb = options;
|
|
options = {};
|
|
}
|
|
options = options || {};
|
|
|
|
var maxAge = (options.ttl || options.ttl === 0) ? options.ttl * 1000 : lruOpts.maxAge;
|
|
|
|
lruCache.set(key, value, maxAge);
|
|
if (cb) {
|
|
process.nextTick(cb);
|
|
}
|
|
};
|
|
|
|
self.get = function(key, options, cb) {
|
|
if (typeof options === 'function') {
|
|
cb = options;
|
|
}
|
|
var value = lruCache.get(key);
|
|
if (cb) {
|
|
process.nextTick(function() {
|
|
cb(null, value);
|
|
});
|
|
} else {
|
|
return value;
|
|
}
|
|
};
|
|
|
|
self.del = function(key, options, cb) {
|
|
if (typeof options === 'function') {
|
|
cb = options;
|
|
}
|
|
lruCache.del(key);
|
|
if (cb) {
|
|
process.nextTick(cb);
|
|
}
|
|
};
|
|
|
|
self.reset = function(cb) {
|
|
lruCache.reset();
|
|
if (cb) {
|
|
process.nextTick(cb);
|
|
}
|
|
};
|
|
|
|
self.keys = function(cb) {
|
|
var keys = lruCache.keys();
|
|
if (cb) {
|
|
process.nextTick(function() {
|
|
cb(null, keys);
|
|
});
|
|
} else {
|
|
return keys;
|
|
}
|
|
};
|
|
|
|
return self;
|
|
};
|
|
|
|
var methods = {
|
|
create: function(args) {
|
|
return memoryStore(args);
|
|
}
|
|
};
|
|
|
|
module.exports = methods;
|
|
|