|
|
|
@ -2,7 +2,15 @@ var redis = require('redis'); |
|
|
|
|
var winston = require('winston'); |
|
|
|
|
var hashlib = require('hashlib'); |
|
|
|
|
|
|
|
|
|
// For storing in redis
|
|
|
|
|
// options[type] = redis
|
|
|
|
|
// options[host] - The host to connect to (default localhost)
|
|
|
|
|
// options[port] - The port to connect to (default 5379)
|
|
|
|
|
// options[db] - The db to use (default 0)
|
|
|
|
|
// options[expire] - The time to live for each key set (default never)
|
|
|
|
|
|
|
|
|
|
var RedisDocumentStore = function(options) { |
|
|
|
|
this.expire = options.expire; |
|
|
|
|
if (!RedisDocumentStore.client) { |
|
|
|
|
RedisDocumentStore.connect(options); |
|
|
|
|
} |
|
|
|
@ -27,11 +35,31 @@ RedisDocumentStore.connect = function(options) { |
|
|
|
|
|
|
|
|
|
// Save file in a key
|
|
|
|
|
RedisDocumentStore.prototype.set = function(key, data, callback) { |
|
|
|
|
var _this = this; |
|
|
|
|
RedisDocumentStore.client.set(key, data, function(err, reply) { |
|
|
|
|
callback(!err); |
|
|
|
|
if (err) { |
|
|
|
|
callback(false); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
_this.setExpiration(key); |
|
|
|
|
callback(true); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Expire a key in expire time if set
|
|
|
|
|
RedisDocumentStore.prototype.setExpiration = function(key) { |
|
|
|
|
if (this.expire) { |
|
|
|
|
RedisDocumentStore.client.expire(key, this.expire, function(err, reply) { |
|
|
|
|
if (err || !reply) { |
|
|
|
|
winston.error('failed to set expiry on key: ' + key); |
|
|
|
|
} else { |
|
|
|
|
console.log('set'); |
|
|
|
|
} |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
}; |
|
|
|
|
|
|
|
|
|
// Get a file from a key
|
|
|
|
|
RedisDocumentStore.prototype.get = function(key, callback) { |
|
|
|
|
RedisDocumentStore.client.get(key, function(err, reply) { |
|
|
|
|