haste-server/lib/redis_document_store.js

43 lines
1.2 KiB
JavaScript
Raw Normal View History

2011-11-19 01:52:18 +01:00
var redis = require('redis');
var winston = require('winston');
var hashlib = require('hashlib');
var RedisDocumentStore = function(options) {
if (!RedisDocumentStore.client) {
2011-11-19 01:59:41 +01:00
RedisDocumentStore.connect(options);
2011-11-19 01:52:18 +01:00
}
};
2011-11-19 01:59:41 +01: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);
RedisDocumentStore.client.select(index, function(err, reply) {
if (err) {
winston.error('error connecting to redis index ' + index, { error: error.message });
process.exit(1);
}
else {
winston.info('connected to redis on ' + host + ':' + port + '/' + index);
}
});
};
2011-11-19 01:52:18 +01:00
// Save file in a key
RedisDocumentStore.prototype.set = function(key, data, callback) {
RedisDocumentStore.client.set(key, data, function(err, reply) {
callback(!err);
});
};
// Get a file from a key
RedisDocumentStore.prototype.get = function(key, callback) {
RedisDocumentStore.client.get(key, function(err, reply) {
callback(err ? false : reply);
});
};
module.exports = RedisDocumentStore;