redis_db.js 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. // username: username,
  15. // password: password,
  16. console.log('host , post',host,port)
  17. var REDIS_INSTANCE = new Redis({
  18. port: port,
  19. host: host,
  20. connectTimeout: 10000,
  21. });
  22. // var REDIS_INSTANCE = new Redis.Cluster([
  23. // {
  24. // port: port,
  25. // password: password,
  26. // host: host,
  27. // },
  28. // ]);
  29. REDIS_INSTANCE.on('connect', () => {
  30. console.log('connected to redis')
  31. })
  32. REDIS_INSTANCE.on('error', function (err) {
  33. console.log('redis Error ', err);
  34. });
  35. function redis_set(key, value) {
  36. REDIS_INSTANCE.set(key, value);
  37. }
  38. async function redis_get(key) {
  39. console.log("redis get=",key);
  40. return await REDIS_INSTANCE.get(key);
  41. }
  42. module.exports = {
  43. redis_set,
  44. redis_get,
  45. }