redis_db.js 1.3 KB

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