redis.js 845 B

12345678910111213141516171819202122232425262728
  1. const promisify = require('util').promisify;
  2. module.exports = function(config) {
  3. const redis_lib =
  4. config.env === 'development' && config.redis_host === 'localhost'
  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.pingAsync = promisify(client.ping);
  23. return client;
  24. };