From 438f90d859de6672336bbc572ea1b781296e67b2 Mon Sep 17 00:00:00 2001 From: Baris Soner Usakli Date: Tue, 31 Dec 2013 19:08:38 -0500 Subject: [PATCH] expire functionality in dbal --- src/database/mongo.js | 25 ++++++++++++++++++++----- src/database/redis.js | 8 ++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/database/mongo.js b/src/database/mongo.js index f9b6ee8eea..9f1f8ea6a9 100644 --- a/src/database/mongo.js +++ b/src/database/mongo.js @@ -50,13 +50,20 @@ function createCollections() { db.createCollection('objects', function(err, collection) { if(err) { - winston.error("Error creating collection " + err.message); + winston.error('Error creating collection ' + err.message); return; } + if(collection) { - collection.ensureIndex({_key :1}, {background:true}, function(err, name){ + collection.ensureIndex({_key :1}, {background:true}, function(err, name) { + if(err) { + winston.error('Error creating index ' + err.message); + } + }); + + collection.ensureIndex({'expireAt':1}, {expireAfterSeconds:0, background:true}, function(err, name) { if(err) { - winston.error("Error creating index " + err.message); + winston.error('Error creating index ' + err.message); } }); } @@ -64,13 +71,13 @@ db.createCollection('search', function(err, collection) { if(err) { - winston.error("Error creating collection " + err.message); + winston.error('Error creating collection ' + err.message); return; } if(collection) { collection.ensureIndex({content:'text'}, {background:true}, function(err, name){ if(err) { - winston.error("Error creating index " + err.message); + winston.error('Error creating index ' + err.message); } }); } @@ -241,6 +248,14 @@ }); } + module.expire = function(key, seconds, callback) { + module.expireAt(key, Math.round(Date.now() / 1000) + seconds, callback); + } + + module.expireAt = function(key, timestamp, callback) { + module.setObjectField(key, 'expireAt', new Date(timestamp * 1000), callback); + } + //hashes module.setObject = function(key, data, callback) { data['_key'] = key; diff --git a/src/database/redis.js b/src/database/redis.js index 02941be081..a298980fef 100644 --- a/src/database/redis.js +++ b/src/database/redis.js @@ -211,6 +211,14 @@ redisClient.keys(key, callback); } + module.expire = function(key, seconds, callback) { + redisClient.expire(key, seconds, callback); + } + + module.expireAt = function(key, timestamp, callback) { + redisClient.expireat(key, timestamp, callback); + } + //hashes module.setObject = function(key, data, callback) {