RedisConfig.js

    1. ...
    2. const setValue = (key, value) => {
    3. if (typeof value === 'undefined' || value == null || value === '') {
    4. return
    5. }
    6. if (typeof value === 'string') {
    7. client.set(key, value)
    8. } else if (typeof value === 'object') {
    9. Object.keys(value).forEach((item) => {
    10. client.hset(key, item, value[item], redis.print)
    11. })
    12. }
    13. return
    14. }
    15. const { promisify } = require("util");
    16. const getAsync = promisify(client.get).bind(client);
    17. const getValue = (key) => {
    18. return getAsync(key)
    19. }
    20. const getHValue = (key) => {
    21. return promisify(client.hgetall).bind(client)(key)
    22. }
    23. export {
    24. client,
    25. setValue,
    26. getValue,
    27. getHValue
    28. }

    test.js

    1. ...
    2. setValue('imoocobj', {
    3. name: 'zbl',
    4. email: '95605355@qq.com',
    5. age: 30
    6. })
    7. getHValue('imoocobj').then((res) => {
    8. console.log('getHValue:' + JSON.stringify(res))
    9. })