Ejemplo n.º 1
0
  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:';
  }
Ejemplo n.º 2
0
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;
};
Ejemplo n.º 3
0
  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);
    }
  }
Ejemplo n.º 4
0
after(() => {
  LoggerHandler.prototype.info.restore()
  LoggerHandler.prototype.warn.restore()
  LoggerHandler.prototype.error.restore()
  LoggerHandler.prototype.debug.restore()
  redis.createClient.restore()
})
Ejemplo n.º 5
0
		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);
}
Ejemplo n.º 7
0
  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;
  };
Ejemplo n.º 8
0
 data.forEach(key => {
   this.client.del(key, (err, data) => {
     if (err) {
       count = 0;
       return fn(err);
     }
     if (--count == 0) {
       fn(null, null);
     }
   });
 });
Ejemplo n.º 9
0
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 };
};
Ejemplo n.º 10
0
 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;
   }
 };
Ejemplo n.º 11
0
Archivo: cache.js Proyecto: PoeBlu/api
                        .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();
                        });
Ejemplo n.º 12
0
Archivo: cache.js Proyecto: PoeBlu/api
            .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();
            });
Ejemplo n.º 13
0
Archivo: cache.js Proyecto: PoeBlu/api
                    .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();

                    });
Ejemplo n.º 14
0
Archivo: cache.js Proyecto: PoeBlu/api
                                .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();
                                });
Ejemplo n.º 15
0
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;
};
Ejemplo n.º 16
0
 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);
     }
   });
 }
Ejemplo n.º 17
0
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;
};
Ejemplo n.º 18
0
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;
    }
}
Ejemplo n.º 19
0
Archivo: cache.js Proyecto: PoeBlu/api
                               .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();
                               });
Ejemplo n.º 20
0
  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'}));
    }
  });
Ejemplo n.º 21
0
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);
  });
};
Ejemplo n.º 22
0
		.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);
Ejemplo n.º 23
0
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;
};
Ejemplo n.º 24
0
 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();
   })
 });
Ejemplo n.º 25
0
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});
        }
    });
});
Ejemplo n.º 26
0
 _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);
         }
       });
     });
   });
 }
Ejemplo n.º 27
0
  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);
      });
    });
  }
Ejemplo n.º 28
0
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);
    });
}
Ejemplo n.º 29
0
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});
        }
    });
});
Ejemplo n.º 30
0
Archivo: cache.js Proyecto: PoeBlu/api
    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)

    });