我将 redis 与 ruby 一起使用,我希望 redis 生成一个散列,其中的值是一个数组。但是,该数组是一个键的值,该键是另一个键的值(简称 - 另一个哈希中的哈希)。
arr = ["this", "is", "an", "array"]
r = Redis.new
r.hset("super_key", "key", arr)
这给了我错误:ERR wrong number of arguments for 'hset' command
。
我希望散列看起来像这样:
{ "super_key" => { "key" => ["this", "is", "an", "array"] } }
那么执行此操作的正确方法是什么?
最佳答案
不能 100% 确定这就是让该方法生气的原因,但看起来您正在以它不允许的方式使用它。来自 the Redis gem documentation :
Redis only stores strings as values. If you want to store an object, you can use a serialization mechanism such as JSON...
因此尝试存储 array.to_json
,并使用 JSON.parse(obj)
再次取回它。
关于ruby - 在 Redis 中将数组设置为哈希值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25368574/