redis-npm的设置中
detect_buffers 建议设置为 true,当传递二进制buffer的时候,再回调中这个buffer会返回回来,true则是将这个返回的二进制变为string
retry_strategy 替代 timeout,具体使用方法在 npm redis 页面有案例
实际操作
// RedisConfig.js 先编写一个redis的配置文件import redis from 'redis'const options = {host: '你的主机地址',port: 14001,password: '鉴权密码',detect_buffers: true,retry_strategy: function(options) { // 这一段直接从官网复制if (options.error && options.error.code === "ECONNREFUSED") {return new Error("The server refused the connection");}if (options.total_retry_time > 1000 * 60 * 60) {return new Error("Retry time exhausted");}if (options.attempt > 10) {return undefined;}return Math.min(options.attempt * 100, 3000);}}// 设置clientconst client = redis.createClient(options)// 设置值的函数const setValue = (key, value) => {return client.set(key, value)}const { promisify } = require('util')const getAsync = promisify(client.get).bind(client)// 获取值的函数const getValue = key => {return getAsync(key)}// 将方法统一暴露出去export {client,getValue,setValue}
// test.js 测试文件import { getValue, setValue } from './RedisConfig'setValue('imooc', 'imooc message from redis client')getValue('imooc').then(res => {console.log('结果是'+res)})
npx babel-node test.js结果是imooc message from redis client
