Permalink
Browse files

Added Database#lookup(key)

  • Loading branch information...
1 parent 2e1b032 commit 01332a960dac8fa8ece4245ce51c4709210a587a @tj committed Apr 15, 2011
Showing with 24 additions and 8 deletions.
  1. +1 −2 lib/commands/hash.js
  2. +6 −6 lib/commands/keys.js
  3. +17 −0 lib/database.js
View
3 lib/commands/hash.js
@@ -16,8 +16,7 @@ var utils = require('../utils');
*/
exports.hlen = function(client, key){
- var key = utils.string(key)
- , obj = this.db.data[key];
+ var obj = this.lookup(utils.string(key));
if (!obj) {
client.int(0);
View
12 lib/commands/keys.js
@@ -21,7 +21,7 @@ exports.expire = function(client, key, seconds){
, obj = this.db.data[key];
if (obj) {
- obj.ttl = Date.now() + Number(string(seconds));
+ obj.expires = Date.now() + Number(string(seconds));
client.bool(true);
} else {
client.bool(false);
@@ -37,7 +37,7 @@ exports.expireat = function(client, key, seconds){
, obj = this.db.data[key];
if (obj) {
- obj.ttl = +string(seconds);
+ obj.expires = +string(seconds);
client.bool(true);
} else {
client.bool(false);
@@ -52,8 +52,8 @@ exports.persist = function(client, key){
var key = string(key)
, obj = this.db.data[key];
- if (obj && 'number' == typeof obj.ttl) {
- delete obj.ttl;
+ if (obj && 'number' == typeof obj.expires) {
+ delete obj.expires;
client.bool(true);
} else {
client.bool(false);
@@ -68,8 +68,8 @@ exports.ttl = function(client, key){
var key = string(key)
, obj = this.db.data[key];
- if (obj && 'number' == typeof obj.ttl) {
- client.int(obj.ttl - Date.now());
+ if (obj && 'number' == typeof obj.expires) {
+ client.int(obj.expires - Date.now());
} else {
client.nil();
}
View
17 lib/database.js
@@ -73,6 +73,23 @@ Database.prototype.selectDB = function(index){
};
/**
+ * Lookup `key`, when volatile compare timestamps to
+ * expire the key.
+ *
+ * @param {String} key
+ * @return {Object}
+ */
+
+Database.prototype.lookup = function(key){
+ var obj = this.db.data[key];
+ if (obj && 'number' == typeof obj.expires && Date.now() > obj.expires) {
+ delete this.db.data[key];
+ return;
+ }
+ return obj;
+};
+
+/**
* Create a connection to the server.
*
* @return {net.Stream}

0 comments on commit 01332a9

Please sign in to comment.