• 使用nodeJs操作redis


    配置文件:RedisOptions.js
    const options = {
      host: '208.167.233.104',
      port: 15001,
      password: '123456',
      detect_buffers: true // 传入buffer 返回也是buffer 否则会转换成String
    }
     
    module.exports = options
     
    封装redis操作:RedisConfig.js 需要安装redis的npm包 (3.0.2)
    const redis = require('redis')
    const redisOptions = require('./RedisOptions')
     
    const options = {
      host: redisOptions.host,
      port: redisOptions.port,
      password: redisOptions.password,
      detect_buffers: redisOptions.detect_buffers, // 传入buffer 返回也是buffer 否则会转换成String
      retry_strategy: function (options) {
        // 重连机制
        if (options.error && options.error.code === "ECONNREFUSED") {
          // End reconnecting on a specific error and flush all commands with
          // a individual error
          return new Error("The server refused the connection");
        }
        if (options.total_retry_time > 1000 * 60 * 60) {
          // End reconnecting after a specific timeout and flush all commands
          // with a individual error
          return new Error("Retry time exhausted");
        }
        if (options.attempt > 10) {
          // End reconnecting with built in error
          return undefined;
        }
        // reconnect after
        return Math.min(options.attempt * 100, 3000);
      }
    }
     
    // 生成redis的client
    const client = redis.createClient(options)
     
    // 存储值
    const setValue = (key, value) => {
      if (typeof value === 'string') {
        client.set(key, value)
      } else if (typeof value === 'object') {
        for (let item in value) {
          client.hmset(key, item, value[item],redis.print)
        }
      }
    }
     
    // 获取string
    const getValue = (key) => {
      return new Promise((resolve, reject) => {
        client.get(key, (err, res) => {
          if (err) {
            reject(err)
          }else{
            resolve(res)
          }
        })
      })
    }
     
    // 获取hash
    const getHValue = (key) => {
      return new Promise((resolve, reject) => {
        client.hgetall(key, function (err, value) {
          if (err) {
            reject(err)
          } else {
            resolve(value)
          }
        })
      })
    }
     
    // 导出
    module.exports = {
      setValue,
      getValue,
      getHValue
    } 
     
    使用:test.js
    const redis = require('./RedisConfig')
     
    redis.setValue('student', {
      name: 'xiaoming',
      age: 18,
      sex: 1
    })
     
    redis.setValue('book', 'yuwen')
     
    redis.getValue('book').then(res => {
      console.log(res)
    }).catch(err => {
      throw new Error(err)
    })
     
    redis.getHValue('student').then(res => {
      console.log(res)
    }).catch(err => {
      throw new Error(err)
    })
  • 相关阅读:
    肯德基也会出这档子事?
    全球华人大签名反对日本成为安理会常任理事国
    Google "invasion" into our desktop
    你真的认真考虑过周期性的构建工作的对于项目成功重要性吗?
    [Eclipse笔记]Give TestNG a try in Eclipse.
    .NET, 想说爱你不容易
    [Eclipse笔记]Just for fun – 在Eclipse下编译和运行C#的代码
    面试注意事项
    名字隐藏深入研究what's in class (什么“类”的“接口”/一部分)
    Catalan数——卡特兰数
  • 原文地址:https://www.cnblogs.com/likewpp/p/12652221.html
Copyright © 2020-2023  润新知