我在我的 NodeJS 应用程序中使用 Redis。我想在 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>}
下面是我调用 hmset() 将策略保存到 Redis 中的 NodeJS 代码。
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 中不能根据哈希键存储对象数组吗?
请协助。谢谢
[更新]
新代码:-
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)"
这不可能.
Redis 将所有内容存储在字符串或其字符串表示中。你不能在 redis 中存储对象数组甚至对象(字符串除外)。
没有直接的方法来存储对象 - 它只能通过 序列化 和存储结果字节数组来完成。
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();
});
});
}
从 Redis v4.0 开始,函数
hmset
已弃用。使用hset
代替。
使用你的新例子,比如
myRedisClient.hset(hashKey, , 'name', JSON.stringify(obj), (err, result) => ...
可以工作。失败。什么失败了,错误的原因是什么?密切注意键名 - 如果这是调用同名键但数据类型不同的键,例如集合,则此调用将失败。