123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- var Redis = require('ioredis'); //导入 安装好的 redis. npm i ioredis --save
- var {db_config} = require('../config/config.js') // 导入位置文件
- //redis 服务启动 /usr/local/bin/redis-server /usr/local/etc/redis.conf
- var port = db_config.redis.PORT
- var host = db_config.redis.HOST
- var username = db_config.redis.USERNAME
- var password = db_config.redis.PASSWORD
- // username: username,
- // password: password,
- console.log('host , post', host, port, process.env.NODE_ENV)
- var opts;
- if (process.env.NODE_ENV == 'test') {
- opts = {
- port: port,
- host: host,
- connectTimeout: 10000,
- }
- } else if (process.env.NODE_ENV == 'dev') {
- opts = {
- port: port,
- host: host,
- username: username,
- password: password,
- connectTimeout: 10000,
- }
- } else if (process.env.NODE_ENV == 'prd') {
- opts = {
- port: port,
- host: host,
- username: username,
- password: password,
- connectTimeout: 10000,
- }
- }
- var REDIS_INSTANCE = new Redis(opts);
- 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,
- }
|