constructor(options = {}) { const { port, host, client, setex, password, database, prefix } = options; if ('string' === typeof options) { options = parse(options); } if ('function' === typeof setex) { this.client = options; } else if (client) { this.client = client; } else if (!port && !host) { this.client = new redis.createClient(); } else { this.client = new redis.createClient(port, host, { ...options, prefix: null }); } if (password) { this.client.auth(password, err => { if (err) throw err; }); } if (database) { this.client.select(database, err => { if (err) throw err; }); } this.prefix = prefix || 'cacheman:'; }
Manager.prototype.createClient = function () { var self = this; var host = this.options.host || 'localhost'; var port = this.options.port || 6379; var options = this.options.options || {}; check(port).isInt(); var client = new redis.createClient(port, host, options); if (typeof options.auth == 'string') { client.auth(options.auth); } client.on('error', function (err) { console.log('[' + self.id + '] ' + err); }); client.on('ready', function () { console.log('[' + self.id + '] connected to redis!'); self.emit('ready'); self.retried = 0; }); client.on('end', function () { self.emit('end'); console.log('[' + self.id + '] disconnected from redis'); }); return client; };
set(key, val, ttl, fn = noop) { const k = `${this.prefix}${key}`; if ('function' === typeof ttl) { fn = ttl; ttl = null; } try { val = JSON.stringify(val) } catch (e) { return fn(e); } const cb = err => { if (err) return fn(err); fn(null, val); } if (-1 === ttl) { this.client.set(k, val, cb); } else { this.client.setex(k, (ttl || 60), val, cb); } }
after(() => { LoggerHandler.prototype.info.restore() LoggerHandler.prototype.warn.restore() LoggerHandler.prototype.error.restore() LoggerHandler.prototype.debug.restore() redis.createClient.restore() })
listen: function(queueName, handler) { if(!queueName) { throw new Error(consolePrefix, "Queue name must be provided. eg. 'emailQueue'."); } if(!handler) { throw new Error(consolePrefix, "You must provide a hander to .listen."); } if(alreadyListening) { throw new Error(consolePrefix, "You can only .listen once in an app. To listen to another queue, create another app."); } if(!blockingClient) { blockingClient = redis.createClient.apply(redis, redisArgs); } process.on("SIGINT", onKillSignal); process.on("SIGTERM", onKillSignal); alreadyListening = true; if(handler.length < 2) { console.log(consolePrefix, 'Assuming that the handler returns a promise.'); } continueListening("cumin." + queueName, handler); }
var uiPublisherPipeline = function(wlstDef) { var myProcessors = []; var redisClient = new redis.createClient(); var channelName = wlstDef.orgsite + "::" + wlstDef.name; if (wlstDef.publishers.dashboard) { myProcessors.push(highland.map(function(execObj) { // console.log("Emmitting data over socket room"); var watchResult = false; //If graph was selected if(wlstDef.publishers.dashboard.graphType) { if(execObj.path) { if(execObj.path.watchresult) watchResult = execObj.path.watchresult; } } var chData = { 'channel': channelName, 'logdata': execObj.data, 'watchresult': watchResult, 'path': execObj.path }; var chDataStr = JSON.stringify(chData); redisClient.publish('watchlist:onResultPublish', chDataStr); return execObj; })); } return highland.pipeline.apply(null, myProcessors); }
that.connect = function() { addClient = redis.createClient.apply(redis, arguments); addClient.on('error', function(err) { console.error(err); }); addClient.on('ready', function() { checkReady(); }); shuffleClient = redis.createClient.apply(redis, arguments); shuffleClient.on('error', function(err) { console.error(err); }); shuffleClient.on('ready', function() { checkReady(); }); return that; };
data.forEach(key => { this.client.del(key, (err, data) => { if (err) { count = 0; return fn(err); } if (--count == 0) { fn(null, null); } }); });
var RedisCache = function () { var redis = require('redis'); this.client = redis.createClient.apply(redis, arguments); this.client.on('ready', function () { debug('Redis Cache ready'); }); this.client.on('error', function (err) { debug('Redis Cache error - ' + err); }); this.defaultOptions = { autodisconnect: true, expiry: 60 }; };
util.enableRedis = function (port, host, options) { if (!_useRedis && redis.createClient) { if (port && host) { options = options || {}; redis = redis.createClient.apply(this, arguments); } else { redis = redis.createClient(); } _useRedis = true; } };
.end(function (err, res3) { if (err) return done(err); redis.createClient.restore(); res1.body.results.length.should.eql(2); res3.body.results.length.should.eql(3); res3.text.should.not.equal(res1.text); app.stop(function(){}); done(); });
.end(function (err, res) { if (err) return done(err); spy.called.should.eql(true); spy.args[0][0].should.eql(cacheKey); c.redisClient.exists.restore(); redis.createClient.restore(); app.stop(function(){}); done(); });
.end(function (err, res2) { if (err) return done(err); redis.createClient.restore(); res2.headers['x-cache'].should.eql('MISS'); res2.headers['x-cache-lookup'].should.eql('MISS'); app.stop(function(){}); done(); });
.end(function (err, getRes3) { if (err) return done(err); var result = _.findWhere(getRes3.body.results, { "_id": id }); redis.createClient.restore(); should.not.exist(result); app.stop(function(){}); done(); });
exports.initialize = function initializeSchema(dataSource, callback) { if (!redis) return; if (dataSource.settings.url) { var url = require('url'); var redisUrl = url.parse(dataSource.settings.url); var redisAuth = (redisUrl.auth || '').split(':'); dataSource.settings.host = redisUrl.hostname; dataSource.settings.port = redisUrl.port; if (redisAuth.length > 1) { dataSource.settings.db = redisAuth[0]; dataSource.settings.password = redisAuth.slice(1).join(':'); } } var params = []; if (dataSource.settings.port) { params.push(dataSource.settings.port); } if (dataSource.settings.host) { params.push(dataSource.settings.host); } if (dataSource.settings.options) { params.push(dataSource.settings.options); } dataSource.client = redis.createClient.apply(redis.createClient, params); dataSource.client.auth(dataSource.settings.password); var callbackCalled = false; var database = dataSource.settings.hasOwnProperty('database') && dataSource.settings.database; dataSource.client.on('connect', function () { if (!callbackCalled && database === false) { callbackCalled = true; callback(); } else if (database !== false) { if (callbackCalled) { return dataSource.client.select(dataSource.settings.database); } else { callbackCalled = true; return dataSource.client.select(dataSource.settings.database, callback); } } }); dataSource.client.on('error', function (error) { console.log(error); }) var clientWrapper = new Client(dataSource.client); dataSource.connector = new BridgeToRedis(clientWrapper); clientWrapper._adapter = dataSource.connector; };
get(key, fn = noop) { const k = `${this.prefix}${key}`; this.client.get(k, (err, data) => { if (err) return fn(err); if (!data) return fn(null, null); data = data.toString(); try { fn(null, JSON.parse(data)); } catch (e) { fn(e); } }); }
module.exports = function (options) { options = _.defaults(options, { port: 6379, host: '127.0.0.1' }); var client = redis.createClient.call(null, options.port, options.host, _.omit(options, 'port', 'host')); client.on('error', function (error) { throw error; }); return client; };
var Redback = exports.Redback = function (client, options) { if (typeof client === 'object' && client && client.command_queue && client.offline_queue) { this.client = client; } else { this.client = redis.createClient.apply(this, arguments); options = arguments[1] instanceof Object ? arguments[1] : arguments[2]; } this.namespace = ''; if (typeof options === 'object' && options.namespace) { this.namespace = options.namespace; } }
.end(function (err, getRes3) { if (err) return done(err); redis.createClient.restore(); // console.log(getRes2.body) // console.log(getRes3.body) var result = _.findWhere(getRes3.body.results, { "_id": id }); result.field1.should.eql('foo bar baz!'); app.stop(function(){}); done(); });
ws.on('message', function(message) { var messageObject = JSON.parse(message); if(messageObject.type === WsSocketMessages.SUBCRIBE){ wsClients.subscribe(ws,messageObject.channel); ws.send(JSON.stringify({subscription : messageObject.channel})); redisClient.lrange("chan:"+messageObject.channel, 0, 19, function(error, response) { if(!error) { for(var i=response.length;i>=0;--i){ wsClients.publish(messageObject.channel,response[i],ws); } } }); }else if(messageObject.type === WsSocketMessages.UNSUBSCRIBE){ wsClients.unsubscribe(ws,messageObject.channel); }else if(messageObject.type === WsSocketMessages.ADD){ var channelKey = "chan:"+messageObject.channel; redisClient.lpush(channelKey,messageObject.message, function(error, response) { if(error) { ws.send(JSON.stringify({subscription : messageObject.channel,status : 500})); } else { wsClients.publish(messageObject.channel,messageObject.message); ws.send(JSON.stringify({subscription : messageObject.channel,status : 204})); } }); }else{ // type does not exist ws.send(JSON.stringify({subscription : messageObject.channel,status : 404,message : 'message type not valid'})); } });
Pooldis.prototype._createClient = function(callback){ var client = Redis.createClient.apply(Redis, this.connectArgs); if (this.connectAuth) { debug("authenticating client"); return client.auth(this.connectAuth, function(error){ debug("authenticated"); debug("connected"); callback(error, client); }); } process.nextTick(function(){ debug("connected"); callback(null, client); }); };
.then(function(data){ token = data.token; // secondary index key var key = "user_id:" + user_id; redis_client.smembers(key, function(err, members){ if (err) return done(err); // members array should contain token for (var i = 0; i < members.length; i++) { if (members[i] == token) return done(); } done(new Error("user_id secondary index hasn't been used")); }); }).catch(done);
Client.prototype.connect = function(hallow) { var self = this; self.redisClient = redis.createClient.apply(null, self.redisSettings); self.redisClient.on('error', function(err) { self.emit('error', err); }); self.redisClient.once('connect', function() { var m = self.redisClient.multi(); if (self.redisOptions && self.redisOptions.overwriteConfig) { _.forEach(self.redisOptions, function(val, key) { if (/^config_\w+/.test(key)) { m.config('set', key.replace('config_', ''), val); } }); } m.exec(function(err) { if (err) { return self.emit('error', err); } if (hallow) { self.db = { s: { databaseName: hallow }, hallow: true }; self.emit('connect', new CacheLayer(self, self.db, self.redisClient)); } else { mongo.MongoClient.connect.apply(null, [self.mongoSettings[0], self.mongoSettings[1], function(err, db) { if (err) { self.emit('error', err); } else { self.db = db; self.emit('connect', new CacheLayer(self, self.db, self.redisClient) ); } } ]); } }); }); return self; };
middleWare.sio.on('connection', function (socket) { socket.pubsubClient = redis.createClient.apply(redis, middleWare.redisClientArgs); socket.on('redis-web', function (data) { data.args.push(function () { var d = { id: data.id, args: Array.prototype.slice.call(arguments) }; socket.emit('redis-web', d); }); socket.pubsubClient[data.cmd].apply(socket.pubsubClient, data.args); }); socket.on('disconnect', function () { socket.pubsubClient.quit(); }) });
router.get('/all', function(req, res) { var key = req.params.key; if(!redisCache) { res.status(500).send({'error': "No REDIS connection!"}); return; } redisCache.hgetall("awesome_v2", function(err, data) { debug("Get key", key, err, data); if(err) { res.status(200).send({ 'status':'error', 'error': err}); } else { res.status(200).send({'data': data}); } }); });
_del(key, fn = noop) { this.client.keys(key, (err, data) => { if (err) return fn(err); let count = data.length; if (count === 0) return fn(null, null); data.forEach(key => { this.client.del(key, (err, data) => { if (err) { count = 0; return fn(err); } if (--count == 0) { fn(null, null); } }); }); }); }
getAll(fn = noop) { var entries = []; this.client.keys('*', (err, keys) => { if (err) return fn(err); each(keys).call((key, index, next) => { key = key.replace(`${this.prefix}`, ''); this.get(key, (err, data) => { if (err) return fn(err); entries.push({ key: key, data: data }); setTimeout(next, 500) }); }).then(() => { fn(null, entries); }); }); }
function RedisTemp(options) { Temp.call(this, options); if ('undefined' == typeof options.prefix) { this.options.prefix = 'rtcs.io:' } if ('undefined' == typeof options.prefixUserSockets) { this.options.prefixUserSockets = 'userSockets:' } if ('undefined' == typeof options.prefixUserRooms) { this.options.prefixUserRooms = 'userRooms:' } if ('undefined' == typeof options.prefixRoomUsers) { this.options.prefixRoomUsers = 'roomUsers:' } if ('undefined' == typeof options.prefixToken) { this.options.prefixToken = 'token:' } this.options.prefixUserSockets = this.options.prefix + this.options.prefixUserSockets; this.options.prefixUserRooms = this.options.prefix + this.options.prefixUserRooms; this.options.prefixRoomUsers = this.options.prefix + this.options.prefixRoomUsers; this.options.prefixToken = this.options.prefix + this.options.prefixToken; if (options.client) { this.redis = options.client; } else { var args = []; if (options.socket) { args.push(options.socket); } else if (options.port && options.host) { args.push(options.port); args.push(options.host); } args.push(options); this.redis = redis.createClient.apply(null, args); } var self = this; self.redis.on('error', function (err) { self.emit('error', err); }); }
router.put('/:key/:value', function(req, res) { var key = req.params.key; var value = req.params.value; if(!redisCache) { res.status(500).send({'error': "No REDIS connection!"}); return; } redisCache.hset('awesome_v2', key, value, function(err) { if(err) { res.status(200).send({ 'status':'error', 'error': err}); } else { res.status(200).send({ 'status':'ok', 'value': value}); } }); });
it('should fallback to directory cache if Redis client fails', function(done) { delete require.cache[__dirname + '/../../config.js']; config.loadFile(config.configPath()); var EventEmitter = require('events'); var util = require('util'); /* Fake redis client */ function Client() { this.end = function(reallyEnd) { } EventEmitter.call(this); } util.inherits(Client, EventEmitter); var redisClient = new Client(); /* End Fake redis client */ sinon.stub(redis, 'createClient').returns(redisClient); delete require.cache[__dirname + '/../../dadi/lib/']; cache.reset(); var c = cache(app); redis.createClient.restore(); setTimeout(function() { // emit an error event redisClient.emit('error', { code: 'CONNECTION_BROKEN'}); config.get('caching.directory.enabled').should.eql(true) try { app.stop(function(){}); } catch (err) { } done(); }, 1000) });