连接数据库配置文件—src/config/index.js

    1. const REDIS = {
    2. host: '192.168.137.133',
    3. port: 15001,
    4. password: '123456'
    5. }
    6. export default {
    7. REDIS
    8. }

    RedisConfig.js

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

    test.js

    1. import { delValue, getHValue, getValue, setValue } from './RedisConfig'
    2. setValue('imooc', 'imooc message from redis client')
    3. getValue('imooc').then((res) => {
    4. console.log('getValue:' + res)
    5. }).catch((err) => {
    6. console.log('getValue,error:' + err)
    7. });
    8. delValue('imooc')
    9. setValue('imoocobj', {
    10. name: 'zbl',
    11. email: '95605355@qq.com',
    12. age: 30
    13. })
    14. getHValue('imoocobj').then((res) => {
    15. console.log('getHValue:' + JSON.stringify(res))
    16. })