2011-11-19 00:52:18 +00:00
|
|
|
var redis = require('redis');
|
|
|
|
var winston = require('winston');
|
|
|
|
|
2011-11-22 03:03:50 +00:00
|
|
|
// 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)
|
|
|
|
|
2012-09-27 15:46:53 +00:00
|
|
|
var RedisDocumentStore = function(options, client) {
|
2011-11-22 03:03:50 +00:00
|
|
|
this.expire = options.expire;
|
2012-09-27 15:46:53 +00:00
|
|
|
if (client) {
|
2012-09-27 15:47:23 +00:00
|
|
|
winston.info('using predefined redis client');
|
2012-09-27 15:46:53 +00:00
|
|
|
RedisDocumentStore.client = client;
|
|
|
|
} else if (!RedisDocumentStore.client) {
|
2012-09-27 15:50:56 +00:00
|
|
|
winston.info('configuring redis');
|
2011-11-19 00:59:41 +00:00
|
|
|
RedisDocumentStore.connect(options);
|
2011-11-19 00:52:18 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-19 00:59:41 +00:00
|
|
|
// Create a connection according to config
|
|
|
|
RedisDocumentStore.connect = function(options) {
|
|
|
|
var host = options.host || '127.0.0.1';
|
|
|
|
var port = options.port || 6379;
|
|
|
|
var index = options.db || 0;
|
|
|
|
RedisDocumentStore.client = redis.createClient(port, host);
|
2014-11-21 13:17:19 +00:00
|
|
|
// authenticate if password is provided
|
|
|
|
if (options.password) {
|
2015-12-27 17:59:59 +00:00
|
|
|
RedisDocumentStore.client.auth(options.password);
|
2014-11-21 13:17:19 +00:00
|
|
|
}
|
2018-09-19 14:37:34 +00:00
|
|
|
|
|
|
|
RedisDocumentStore.client.on('error', function(err) {
|
|
|
|
winston.error('redis disconnected', err);
|
|
|
|
});
|
|
|
|
|
2017-06-26 16:19:36 +00:00
|
|
|
RedisDocumentStore.client.select(index, function(err) {
|
2011-11-19 00:59:41 +00:00
|
|
|
if (err) {
|
2012-01-21 20:19:55 +00:00
|
|
|
winston.error(
|
|
|
|
'error connecting to redis index ' + index,
|
2013-11-24 16:54:01 +00:00
|
|
|
{ error: err }
|
2012-01-21 20:19:55 +00:00
|
|
|
);
|
2011-11-19 00:59:41 +00:00
|
|
|
process.exit(1);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
winston.info('connected to redis on ' + host + ':' + port + '/' + index);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-11-19 00:52:18 +00:00
|
|
|
// Save file in a key
|
2011-11-22 14:22:37 +00:00
|
|
|
RedisDocumentStore.prototype.set = function(key, data, callback, skipExpire) {
|
2011-11-22 03:03:50 +00:00
|
|
|
var _this = this;
|
2017-06-26 16:19:36 +00:00
|
|
|
RedisDocumentStore.client.set(key, data, function(err) {
|
2011-11-22 03:03:50 +00:00
|
|
|
if (err) {
|
|
|
|
callback(false);
|
|
|
|
}
|
|
|
|
else {
|
2011-11-22 14:22:37 +00:00
|
|
|
if (!skipExpire) {
|
|
|
|
_this.setExpiration(key);
|
|
|
|
}
|
2011-11-22 03:03:50 +00:00
|
|
|
callback(true);
|
|
|
|
}
|
2011-11-19 00:52:18 +00:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2011-11-22 03:03:50 +00:00
|
|
|
// Expire a key in expire time if set
|
|
|
|
RedisDocumentStore.prototype.setExpiration = function(key) {
|
|
|
|
if (this.expire) {
|
2017-06-26 16:19:36 +00:00
|
|
|
RedisDocumentStore.client.expire(key, this.expire, function(err) {
|
2011-11-28 06:13:14 +00:00
|
|
|
if (err) {
|
2011-11-22 03:03:50 +00:00
|
|
|
winston.error('failed to set expiry on key: ' + key);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-11-19 00:52:18 +00:00
|
|
|
// Get a file from a key
|
2011-11-28 06:13:14 +00:00
|
|
|
RedisDocumentStore.prototype.get = function(key, callback, skipExpire) {
|
|
|
|
var _this = this;
|
2011-11-19 00:52:18 +00:00
|
|
|
RedisDocumentStore.client.get(key, function(err, reply) {
|
2011-11-28 06:13:14 +00:00
|
|
|
if (!err && !skipExpire) {
|
|
|
|
_this.setExpiration(key);
|
|
|
|
}
|
2011-11-19 00:52:18 +00:00
|
|
|
callback(err ? false : reply);
|
2012-01-21 20:19:55 +00:00
|
|
|
});
|
2011-11-19 00:52:18 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
module.exports = RedisDocumentStore;
|