You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

40 lines
748 B

  1. "use strict";
  2. var Redis = require('ioredis'),
  3. _ = require('lodash');
  4. /**
  5. * Redis module
  6. *
  7. * @param {Object} appconfig Application config
  8. * @return {Redis} Redis instance
  9. */
  10. module.exports = (appconfig) => {
  11. let rd = null;
  12. if(_.isArray(appconfig.redis)) {
  13. rd = new Redis.Cluster(appconfig.redis, {
  14. scaleReads: 'master',
  15. redisOptions: {
  16. lazyConnect: false
  17. }
  18. });
  19. } else {
  20. rd = new Redis(_.defaultsDeep(appconfig.redis), {
  21. lazyConnect: false
  22. });
  23. }
  24. // Handle connection errors
  25. rd.on('error', (err) => {
  26. winston.error('Failed to connect to Redis instance(s). [err-1]');
  27. });
  28. rd.on('node error', (err) => {
  29. winston.error('Failed to connect to Redis instance(s). [err-2]');
  30. });
  31. return rd;
  32. };