Is nested GET possible in Redis?

时间:2015-11-12 11:21:33

标签: redis

The basic setup is this:

SET foo bar
SET baz foo

Is something like this possible in a single statement?

GET GET baz
"bar"

thanks

2 个答案:

答案 0 :(得分:1)

You can't do it using basic redis API.

I can see two options for you:

  • do it yourself in whatever programming language you're using with redis;
  • do it with a lua script.

Here is a very simple lua script example, doing what you want:

eval "return redis.call('get', redis.call('get', KEYS[1]))" 1 baz

N.B.: You can only use this script with standalone redis installation, because it's using keys that aren't explicitly declared via the KEYS array, so it's not cluster-safe (thanx to Itamar Haber's comment).

答案 1 :(得分:1)

虽然您无法使用Redis字符串执行此操作,但如果您使用所有"密钥保留哈希值,那么这是可行的。和"价值观"代替。例如:

HSET miniredis foo bar
HSET miniredis baz foo

现在,要获取商品,您需要Lua,但由于所有内容都捆绑在一个数据结构中,因此它将是群集安全的(以可扩展性为代价)。

EVAL "return redis.call('HGET', KEYS[1], redis.call('HGET', KEYS[1], ARGV[1]))" 1 miniredis baz

当然,这仅在总有一个解除引用级别时才有效。另一个特点是自我引用的能力(即HSET miniredis minime minime)。如果需要,应该处理这些。