Permalink
Browse files

[lib] added support for autoexpire cotr option

  • Loading branch information...
1 parent cb55595 commit c72ce1cd0581a104a5b98929f2cf220676ad5af8 Paolo Fragomeni committed Apr 7, 2012
Showing with 120 additions and 23 deletions.
  1. +100 −22 lib/eventvat.js
  2. +1 −1 package.json
  3. +19 −0 test/methods-test.js
View
@@ -31,6 +31,7 @@
var that = this;
this.hash = {};
+ this.autoexpire = (conf && conf.autoexpire) || 0;
if (conf && conf.data) {
@@ -99,15 +100,18 @@
p.expire = function(key, ttl) {
if (has(this.hash, key) && typeof ttl === 'number' && ttl > -1) {
+
if (this.hash[key].tid) {
clearTimeout(this.hash[key].tid);
}
var ms = ttl * 1000;
var that = this;
+
that.hash[key].tid = setTimeout(function() {
that.del(key);
}, ms);
+
that.hash[key].tend = +new Date() + ms;
this.emit('expire ' + key, ttl);
@@ -141,6 +145,14 @@
p.keys = function(regex) {
var keys = [];
+ //
+ // if no pattern is provided, report how many
+ // keys that there are in the entire object.
+ //
+ if(!regex) {
+ return Object.keys(this.hash).length;
+ }
+
for(var k in this.hash) {
if(has(this.hash, k) && regex.test(k)) {
keys.push(k);
@@ -466,7 +478,8 @@
this.persist(key);
delete this.hash[key].type;
this.hash[key].value = value;
- } else {
+ }
+ else {
this.hash[key] = { value: value };
}
}
@@ -482,6 +495,18 @@
var args = Array.prototype.slice.call(arguments);
args.unshift('mset');
+
+ //
+ // if the number of arguments is even, we must have been provided with a
+ // ttl, in which case, use it to expoire the new key
+ //
+ if (arguments.length % 2 === 0) {
+ this.expire(key, arguments[arguments.length-1]);
+ }
+ else if (this.autoexpire) {
+ this.expire(key, this.autoexpire);
+ }
+
this.emit.apply(this, args);
return true;
};
@@ -540,7 +565,11 @@
this.hash[key].time = Date.now().toString();
this.persist(key);
- this.expire(key, ttl);
+
+ if (ttl || this.autoexpire) {
+ this.expire(key, ttl || this.autoexpire);
+ }
+
this.emit('set ' + key, value);
this.emit('set', key, value);
return true;
@@ -840,19 +869,35 @@
// set the string value of a hash field
//
p.hset = function(key, field, value) {
+
var update;
var type = this.type(key);
+
if (type === 'none') {
this.hash[key] = { value: {}, type: 'hash' };
update = false;
- } else if (type === 'hash') {
+ }
+ else if (type === 'hash') {
this.persist(key);
update = has(this.hash[key].value, field);
- } else {
+ }
+ else {
return false;
}
this.hash[key].value[field] = value;
+
+ //
+ // if the number of arguments is even, we must have been provided with a
+ // ttl, in which case, use it to expoire the new key
+ //
+ if (arguments.length % 2 === 0) {
+ this.expire(key, arguments[arguments.length-1]);
+ }
+ else if (this.autoexpire) {
+ this.expire(key, this.autoexpire);
+ }
+
this.emit('hset ' + key, field, value, update);
this.emit('hset', key, field, value, update);
@@ -866,9 +911,11 @@
var type = this.type(key);
if (type === 'none') {
this.hash[key] = { value: {}, type: 'hash' };
- } else if (type !== 'hash') {
+ }
+ else if (type !== 'hash') {
return false;
- } else {
+ }
+ else {
this.persist(key);
}
@@ -877,7 +924,8 @@
this.emit('hsetnx ' + key, field, value);
this.emit('hsetnx', key, field, value);
return true;
- } else {
+ }
+ else {
return false;
}
};
@@ -926,7 +974,8 @@
this.emit('lindex ' + key, index, value);
this.emit('lindex', key, index, value);
return value;
- } else {
+ }
+ else {
return false;
}
};
@@ -963,7 +1012,8 @@
this.emit('lpop ' + key, value);
this.emit('lpop', key, value);
return value;
- } else {
+ }
+ else {
return null;
}
};
@@ -972,14 +1022,18 @@
// Prepend one or multiple values to a list
//
p.lpush = function(key) {
+
var type = this.type(key);
var list;
+
if (type === 'none') {
list = [];
this.hash[key] = { value: list, type: 'list' };
- } else if (type !== 'list') {
+ }
+ else if (type !== 'list') {
return false;
- } else {
+ }
+ else {
this.persist(key);
list = this.hash[key].value;
}
@@ -991,6 +1045,10 @@
this.emit('lpush', key, val, len);
}
+ if (this.autoexpire) {
+ this.expire(key, this.autoexpire);
+ }
+
return len;
};
@@ -1003,7 +1061,8 @@
this.emit('lpushx ' + key, value, len);
this.emit('lpushx', key, value, len);
return len;
- } else {
+ }
+ else {
return 0;
}
};
@@ -1044,7 +1103,8 @@
}
// if count is negative, list will be traversed backwards
- } else {
+ }
+ else {
i = list.length;
check = function() { return i >= 0; };
incr = function() { i--;};
@@ -1089,8 +1149,14 @@
list[index] = value;
this.emit('lset ' + key, index, value);
this.emit('lset', key, index, value);
+
+ if (this.autoexpire) {
+ this.expire(key, this.autoexpire);
+ }
+
return true;
- } else {
+ }
+ else {
return false;
}
};
@@ -1105,7 +1171,8 @@
this.emit('ltrim ' + key, start, stop);
this.emit('ltrim', key, start, stop);
return true;
- } else {
+ }
+ else {
return false;
}
};
@@ -1120,7 +1187,8 @@
this.emit('rpop ' + key, value);
this.emit('rpop', key, value);
return value;
- } else {
+ }
+ else {
return null;
}
};
@@ -1136,9 +1204,11 @@
if (dtype === 'none') {
dest = [];
this.hash[destination] = { value: dest, type: 'list' };
- } else if (dtype !== 'list') {
+ }
+ else if (dtype !== 'list') {
return null;
- } else {
+ }
+ else {
this.persist(destination);
dest = this.hash[destination].value;
}
@@ -1153,7 +1223,8 @@
this.emit('lpush ' + destination, value, len);
this.emit('lpush', destination, value, len);
return value;
- } else {
+ }
+ else {
return null;
}
};
@@ -1167,9 +1238,11 @@
if (type === 'none') {
list = [];
this.hash[key] = { value: list, type: 'list' };
- } else if (type !== 'list') {
+ }
+ else if (type !== 'list') {
return false;
- } else {
+ }
+ else {
this.persist(key);
list = this.hash[key].value;
}
@@ -1181,6 +1254,10 @@
this.emit('rpush', key, val, len);
}
+ if (this.autoexpire) {
+ this.expire(key, this.autoexpire);
+ }
+
return len;
};
@@ -1193,7 +1270,8 @@
this.emit('rpushx ' + key, value, len);
this.emit('rpushx', key, value, len);
return len;
- } else {
+ }
+ else {
return 0;
}
};
View
@@ -1,6 +1,6 @@
{
"name" : "eventvat",
- "version" : "0.1.9",
+ "version" : "0.2.0",
"description" : "Evented, in-process key/value store for Node.js and the browser",
"main" : "./lib/eventvat.js",
"directories" : {
View
@@ -986,6 +986,25 @@ this.methodSuite = {
test.done();
},
+ 'Autoexpire should expire all keys after a specified length of time': function(test) {
+
+ var vat = EventVat({
+ autoexpire: 1
+ });
+
+ vat.set('foo', 'hello');
+ vat.hset('bar', 'a', 'hello');
+
+ vat.rpush('bazz', 'a');
+ vat.rpush('bazz', 'b');
+
+ vat.on('del', function() {
+ if (Object.keys(vat.hash).length === 0) {
+ test.done();
+ }
+ });
+
+ },
'Reconstitute a vat from a JSON object': function(test) {
var vat1 = EventVat();

0 comments on commit c72ce1c

Please sign in to comment.