node.js—是否可以将对象数组作为哈希存储到redis中?

toiithl6  于 2021-06-09  发布在  Redis
关注(0)|答案(2)|浏览(806)

我在nodejs应用程序中使用redis。我想在redis中将对象(策略)数组作为散列存储在一个键上。下面是我的钥匙和钥匙value:-
钥匙- <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存储对象数组吗?
请帮忙。谢谢
[更新]
新建code:-

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();
        });
    });
}

error:-

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)"
t30tvxxf

t30tvxxf1#

这是不可能的。
redis以字符串或其字符串表示形式存储所有内容。不能在redis中存储对象数组甚至对象(字符串除外)。
没有直接的方法来存储对象-只能通过序列化和存储结果字节数组来完成。

cbeh67ev

cbeh67ev2#

redis存储字符串,因此必须序列化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();
        });
    });
}

相关问题