Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Redis Future + Rails

I am using redis in one of my rails project where I tried to union the sets of redis like

$redis.smembers('set1') | $redis.smembers('set2') 

but it throws error like this

undefined method `|' for #<Redis::Future:0x000001306e5830>

What is Redis::Future? I am using redis and redis-store gems

Thank you

like image 755
DeathHammer Avatar asked Dec 27 '22 23:12

DeathHammer


2 Answers

Future objects are typically returned when methods are called in a pipeline or a transaction.

The returned value is only available when the EXEC command has been applied to the Redis server. With redis-rb, it means you should exit a pipelined or multi block before.

If you want to select/read data, do it before the multi/exec block, and do only write in the multi/exec block.

By the way, it will be more efficient to use $redis.sunion() to generate the result on server-side.

like image 143
Didier Spezia Avatar answered Jan 10 '23 16:01

Didier Spezia


What Didier said, plus check that your code is not running inside a multi block, like this:

$redis.multi do
  $set1 = $redis.smembers("set1")
end

In that case, $set1 is a "future" pointing to the result you'll get as soon as the block exists.

You can access the real, underlying value by calling $set1.value.

like image 38
djanowski Avatar answered Jan 10 '23 15:01

djanowski