有没有办法从 redis 哈希中获取单个字段
Is there a way go get a single field from redis hashes
我有一个 redis 缓存,里面装满了这样的项目 (100k+):
redis:6379> hgetall key1
1) "counter"
2) "100"
redis:6379> hgetall key2
1) "counter"
2) "200"
如何(在一次调用中)从 key1 和 key2 获得两个反值,例如在伪代码中是这样的
hmgetall key1 key2 counter
我尝试过使用 SUNION、HMGET 和 HMGETALL 等命令,但没有找到解决问题的方法。有人对我有想法吗?答案是:“这在技术上是不可能的!”也可以……那我得试试别的了。
根据@for_stack的回答,我试过:
$call = "local res = {}; for k, v in pairs(ARGV[1]) do res[#res + 1] = redis.call(\"hget\", v, \"counter\") end; return res";
$args = [];
$args[] = "2";
$args[] = "key1 key2";
$redis = new Redis();
$redis->connect($host, $port);
$counter = $redis->eval($call, $args, 1);
如果我转储 $counter,我将得到“0”。这样有什么问题吗?
您可以使用 Lua script 来完成这项工作。
eval 'local res = {}; local field = ARGV[1]; for k, v in pairs(KEYS) do res[#res + 1] = redis.call("hget", v, field) end; return res;' 2 key1 key2 counter
我有一个 redis 缓存,里面装满了这样的项目 (100k+):
redis:6379> hgetall key1
1) "counter"
2) "100"
redis:6379> hgetall key2
1) "counter"
2) "200"
如何(在一次调用中)从 key1 和 key2 获得两个反值,例如在伪代码中是这样的
hmgetall key1 key2 counter
我尝试过使用 SUNION、HMGET 和 HMGETALL 等命令,但没有找到解决问题的方法。有人对我有想法吗?答案是:“这在技术上是不可能的!”也可以……那我得试试别的了。
根据@for_stack的回答,我试过:
$call = "local res = {}; for k, v in pairs(ARGV[1]) do res[#res + 1] = redis.call(\"hget\", v, \"counter\") end; return res";
$args = [];
$args[] = "2";
$args[] = "key1 key2";
$redis = new Redis();
$redis->connect($host, $port);
$counter = $redis->eval($call, $args, 1);
如果我转储 $counter,我将得到“0”。这样有什么问题吗?
您可以使用 Lua script 来完成这项工作。
eval 'local res = {}; local field = ARGV[1]; for k, v in pairs(KEYS) do res[#res + 1] = redis.call("hget", v, field) end; return res;' 2 key1 key2 counter