redis.js 987 B

12345678910111213141516171819202122232425262728293031
  1. const promisify = require('util').promisify;
  2. module.exports = function(config) {
  3. const redis_lib =
  4. config.env === 'development' && config.redis_host === 'mock'
  5. ? 'redis-mock'
  6. : 'redis';
  7. //eslint-disable-next-line security/detect-non-literal-require
  8. const redis = require(redis_lib);
  9. const client = redis.createClient({
  10. host: config.redis_host,
  11. retry_strategy: options => {
  12. if (options.total_retry_time > config.redis_retry_time) {
  13. client.emit('error', 'Retry time exhausted');
  14. return new Error('Retry time exhausted');
  15. }
  16. return config.redis_retry_delay;
  17. }
  18. });
  19. client.ttlAsync = promisify(client.ttl);
  20. client.hgetallAsync = promisify(client.hgetall);
  21. client.hgetAsync = promisify(client.hget);
  22. client.hincrbyAsync = promisify(client.hincrby);
  23. client.hmgetAsync = promisify(client.hmget);
  24. client.pingAsync = promisify(client.ping);
  25. client.existsAsync = promisify(client.exists);
  26. return client;
  27. };