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.

74 lines
1.5 KiB

12 years ago
var Lru = require("lru-cache");
10 years ago
var memory_store = function(args) {
args = args || {};
var self = {};
12 years ago
self.name = 'memory';
var ttl = args.ttl;
var lru_opts = {
max: args.max || 500,
maxAge: ttl ? ttl * 1000 : null
};
var lru_cache = new Lru(lru_opts);
self.set = function(key, value, options, cb) {
lru_cache.set(key, value);
if (cb) {
process.nextTick(cb);
}
};
self.get = function(key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
var value = lru_cache.get(key);
if (cb) {
10 years ago
process.nextTick(function() {
cb(null, value);
});
} else {
return value;
}
};
self.del = function(key, options, cb) {
if (typeof options === 'function') {
cb = options;
}
lru_cache.del(key);
if (cb) {
process.nextTick(cb);
}
};
10 years ago
self.reset = function(cb) {
lru_cache.reset();
if (cb) {
process.nextTick(cb);
}
};
10 years ago
self.keys = function(cb) {
var keys = lru_cache.keys();
if (cb) {
10 years ago
process.nextTick(function() {
cb(null, keys);
});
} else {
return keys;
}
};
return self;
};
var methods = {
10 years ago
create: function(args) {
return memory_store(args);
}
};
module.exports = methods;