【问题标题】:Is it possible to store the array of objects as hash into the Redis?是否可以将对象数组作为哈希存储到 Redis 中?
【发布时间】:2020-06-18 08:52:26
【问题描述】:

我将 Redis 与我的 NodeJS 应用程序一起使用。我想在 Redis 中将一组对象(策略)作为哈希存储在一个键上。以下是我的关键和价值:-

密钥- <tenantid>~<userid> e.g. - tenant123~john123

价值 - [{ ptype: 'p', v0: 'admin', v1: '/*', v2: 'GET' }, { ptype: 'p', v0: 'viewer', v1: '/post', v2: 'GET' }]

我遇到以下错误:-

err
ReplyError: ERR wrong number of arguments for 'hmset' command
args:Array(2) ["tenant123~john123", "[{ ptype: 'p', v0: 'admin', v1: '/*', v2: 'GET' }, { ptype: 'p', v0: 'viewer', v1: '/post', v2: 'GET' }]"]
code:"ERR"
command:"HMSET"
message:"ERR wrong number of arguments for 'hmset' command"
name:"ReplyError"
stack:"ReplyError: ERR wrong number of arguments for 'hmset' command
    at parseError (/var/task/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/var/task/node_modules/redis-parser/lib/parser.js:302:14)"
__proto__:RedisError {constructor: , name: <accessor>}

下面是我的 NodeJS 代码,我在其中调用 hmset() 将策略保存到 Redis 中。

module.exports.updateCache = async (hashKey, ttl, obj, logger) => {
    logger.debug('Redis:: Key:', hashKey, ', Obj:', obj);
    return new Promise(function (resolve, reject) {
        myRedisClient.hmset(hashKey, obj, (err, result) => {
            if (err) {
                logger.error(err);
                reject(err);
            } else {
                myRedisClient.expire(hashKey, ttl);
                console.log('result:', result);
                resolve(true);
            }
            console.log('closing redis connection');
            myRedisClient.quit();
        });
    });
}

我尝试了很多调试但无法解决这个问题。在 Redis 中是否不能针对 hashkey 存储对象数组?

请帮忙。谢谢

[更新]

新代码:-

module.exports.updateCache = async (hashKey, ttl, obj, logger) => {
    logger.debug('Redis:: Key:', hashKey, ', Obj:', obj);
    return new Promise(function (resolve, reject) {
        myRedisClient.hmset(hashKey, JSON.stringify(obj), (err, result) => {
            if (err) {
                logger.error(err);
                reject(err);
            } else {
                myRedisClient.expire(hashKey, ttl);
                console.log('result:', result);
                resolve(true);
            }
            console.log('closing redis connection');
            myRedisClient.quit();
        });
    });
}

错误:-

err
ReplyError: ERR wrong number of arguments for 'hmset' command
args:Array(2) ["tenant123~john123", "[{"ptype":"p","v0":"admin","v1":"/*","v2":"GET"}]"]
code:"ERR"
command:"HMSET"
message:"ERR wrong number of arguments for 'hmset' command"
name:"ReplyError"
stack:"ReplyError: ERR wrong number of arguments for 'hmset' command
    at parseError (/var/task/node_modules/redis-parser/lib/parser.js:179:12)
    at parseType (/var/task/node_modules/redis-parser/lib/parser.js:302:14)"

【问题讨论】:

    标签: node.js redis node-redis


    【解决方案1】:

    不可能

    Redis 将所有内容存储在字符串或其字符串表示形式中。你不能在redis中存储对象数组甚至对象(字符串除外)。

    没有直接的方法来存储对象 - 它只能通过 序列化 并存储结果字节数组来完成。

    【讨论】:

    • 对不起,可以在这里分享一个示例代码解释序列化吗?我试过 JSON.stringify(obj) 但没有用
    【解决方案2】:

    Redis 存储 strings,因此您必须序列化您的 JSON 对象/数组来存储它们,然后在读取这些键时对其进行反序列化。

    module.exports.updateCache = async (hashKey, ttl, obj, logger) => {
        logger.debug('Redis:: Key:', hashKey, ', Obj:', obj);
        return new Promise(function (resolve, reject) {
            myRedisClient.hmset(hashKey, { array: JSON.stringify(obj) }, (err, result) => {
                if (err) {
                    logger.error(err);
                    reject(err);
                } else {
                    myRedisClient.expire(hashKey, ttl);
                    console.log('result:', result);
                    resolve(true);
                }
                console.log('closing redis connection');
                myRedisClient.quit();
            });
        });
    }
    

    【讨论】:

    • 这不起作用。 “为‘hmset’命令获取错误 ERR 错误数量的参数”。我尝试通过传递对象数组来使用您的解决方案,但收到此错误
    • 修正了答案,hmset,我为hset写了代码。文档将在这些情况下为您提供帮助
    猜你喜欢
    • 2022-01-09
    • 2016-09-17
    • 2015-08-14
    • 2017-08-23
    • 2020-12-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-19
    相关资源
    最近更新 更多