12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455 |
- var Redis = require('ioredis'); //导入 安装好的 redis. npm i ioredis --save
- var config = require('../config/config.js') // 导入位置文件
- //redis 服务启动 /usr/local/bin/redis-server /usr/local/etc/redis.conf
- var port = config.db_config.test.redis.PORT
- var host = config.db_config.test.redis.HOST
- var username = config.db_config.test.redis.USERNAME
- var password = config.db_config.test.redis.PASSWORD
- if (config.build_config.open_test == 0) {
- port = config.db_config.main.redis.PORT
- host = config.db_config.main.redis.HOST
- username = config.db_config.main.redis.USERNAME
- password = config.db_config.main.redis.PASSWORD
- }
- // username: username,
- // password: password,
- console.log('host , post',host,port)
- var REDIS_INSTANCE = new Redis({
- port: port,
- host: host,
- connectTimeout: 10000,
- });
- // var REDIS_INSTANCE = new Redis.Cluster([
- // {
- // port: port,
- // password: password,
- // host: host,
- // },
- // ]);
- REDIS_INSTANCE.on('connect', () => {
- console.log('connected to redis')
- })
- REDIS_INSTANCE.on('error', function (err) {
- console.log('redis Error ', err);
- });
- function redis_set(key, value) {
- REDIS_INSTANCE.set(key, value);
- }
- async function redis_get(key) {
- console.log("redis get=",key);
- return await REDIS_INSTANCE.get(key);
- }
- module.exports = {
- redis_set,
- redis_get,
- }
|