Advertisement
Guest User

Untitled

a guest
Oct 29th, 2016
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import _ from 'lodash';
  2. import Redis from 'ioredis';
  3. import { redis as cfg } from './environment';
  4. import logger from './logger';
  5.  
  6. Redis.Command.setReplyTransformer('zrevrangebyscore', result => {
  7.   const data = [];
  8.   for (let i = 0; i < result.length; i += 2) {
  9.     const object = {
  10.       username: result[i],
  11.       points: parseInt(result[i + 1])
  12.     }
  13.     data.push(object)
  14.   }
  15.   return data;
  16. });
  17.  
  18. Redis.Command.setReplyTransformer('zscore', result => {
  19.   return _.isNumber(result) ? result : parseInt(result, 10);
  20. });
  21.  
  22. Redis.Command.setReplyTransformer('zrem', result => {
  23.   return result === 1;
  24. });
  25.  
  26. Redis.Command.setReplyTransformer('zincrby', result => {
  27.   return parseInt(result, 10);
  28. });
  29.  
  30. /**
  31.  * General stuff + subscribe connection
  32.  * use redis.pipeline if > 5 commands
  33.  */
  34. const redis = new Redis({
  35.   port: cfg.port,
  36.   host: cfg.host,
  37.   password: cfg.password,
  38.   db: cfg.db
  39. });
  40.  
  41. export default redis;
  42.  
  43. redis.on('connect', () => {
  44.   logger.info('Established a connection to redis');
  45. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement