redis_db.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. var Redis = require('ioredis'); //导入 安装好的 redis. npm i ioredis --save
  2. var config = require('../config/config.js') // 导入位置文件
  3. //redis 服务启动 /usr/local/bin/redis-server /usr/local/etc/redis.conf
  4. var port = config.db_config.test.redis.PORT
  5. var host = config.db_config.test.redis.HOST
  6. var username = config.db_config.test.redis.USERNAME
  7. var password = config.db_config.test.redis.PASSWORD
  8. if (config.build_config.open_test == 0) {
  9. port = config.db_config.main.redis.PORT
  10. host = config.db_config.main.redis.HOST
  11. username = config.db_config.main.redis.USERNAME
  12. password = config.db_config.main.redis.PASSWORD
  13. }
  14. var REDIS_INSTANCE = new Redis({
  15. port: port,
  16. host: host,
  17. username: username,
  18. password: password,
  19. connectTimeout: 10000,
  20. });
  21. // var REDIS_INSTANCE = new Redis.Cluster([
  22. // {
  23. // port: port,
  24. // password: password,
  25. // host: host,
  26. // },
  27. // ]);
  28. REDIS_INSTANCE.on('connect', () => {
  29. console.log('connected to redis')
  30. })
  31. REDIS_INSTANCE.on('error', function (err) {
  32. console.log('redis Error ', err);
  33. });
  34. function redis_set(key, value) {
  35. REDIS_INSTANCE.set(key, value);
  36. }
  37. async function redis_get(key) {
  38. console.log("redis get=",key);
  39. return await REDIS_INSTANCE.get(key);
  40. }
  41. module.exports = {
  42. redis_set,
  43. redis_get,
  44. }