一、get&set方法 && 哈希表hset&hgetall && BlueBird

/src/config/index.js
  1. const DB_URL = 'mongodb://test:123456@203.195.161.212:15001/testdb'
  2. const REDIS = {
  3. host: '203.195.161.212',
  4. port: 15001,
  5. password: '123456'
  6. }
  7. export default {
  8. DB_URL,
  9. REDIS
  10. }

/src/config/RedisConfig.js
  1. import redis from 'redis'
  2. import { promisifyAll } from 'bluebird'
  3. import config from './index'
  4. const options = {
  5. host: config.REDIS.host,
  6. port: config.REDIS.port,
  7. password: config.REDIS.password,
  8. detect_buffers: true,
  9. retry_strategy: function(options) { // 重试
  10. if (options.error && options.error.code === "ECONNREFUSED") {
  11. // End reconnecting on a specific error and flush all commands with
  12. // a individual error
  13. return new Error("The server refused the connection");
  14. }
  15. if (options.total_retry_time > 1000 * 60 * 60) {
  16. // End reconnecting after a specific timeout and flush all commands
  17. // with a individual error
  18. return new Error("Retry time exhausted");
  19. }
  20. if (options.attempt > 10) {
  21. // End reconnecting with built in error
  22. return undefined;
  23. }
  24. // reconnect after
  25. return Math.min(options.attempt * 100, 3000);
  26. }
  27. }
  28. // const client = redis.createClient(options)
  29. const client = promisifyAll(redis.createClient(options))
  30. client.on('error', err => {
  31. console.log(`Redis Client Error:${err}`)
  32. })
  33. const setValue = (key, value) => {
  34. if (typeof value === 'undefined' || value === null || value === '') {
  35. return
  36. }
  37. if (typeof value === 'string') {
  38. client.set(key, value)
  39. } else if (typeof value === 'object') {
  40. Object.keys(value).forEach(item => {
  41. client.hset(key, item, value[item], redis.print) //redis.print返回日志信息
  42. })
  43. }
  44. }
  45. // const { promisify } = require("util");
  46. // const getAsync = promisify(client.get).bind(client);
  47. const getValue = (key) => {
  48. return client.getAsync(key)
  49. }
  50. const getHValue = (key) => {
  51. // v8 Promisify method use util, must node > 8
  52. // return promisify(client.hgetall).bind(client)(key) // hgetall转成promise方法 @return function
  53. // bluebird async
  54. return client.hgetallAsync(key)
  55. }
  56. const delValue = (key) => {
  57. client.del(key, (err, res) => {
  58. if (res === 1) {
  59. console.log('delete successfully')
  60. } else {
  61. console.log(`delete redis key error:${err}`)
  62. }
  63. })
  64. }
  65. export {
  66. client,
  67. getValue,
  68. setValue,
  69. getHValue,
  70. delValue
  71. }

/src/config/test.js
  1. import {getHValue, getValue, setValue, delValue} from "./RedisConfig"
  2. setValue('sampson', 'sampson message from redis client')
  3. getValue('sampson').then(res => {
  4. console.log(`getValue:${res}`)
  5. })
  6. delValue('sampson')
  7. setValue('sampsonobj', {name: 'sampson', age: 30, email: '419582275@qq.com'})
  8. getHValue('sampsonobj').then((res) => {
  9. console.log(`getHValue:${JSON.stringify(res, null, 2)}`)
  10. })

redis资料参考