Question

I am looking to make an efficient function to clear out a redis-based cache.

I have a method call that returns a number of keys from redis:

$redis.keys("foo:*")

That returns all the keys that start with "foo:". Next, I'd like to delete all the values for these keys.

One (memory-intensive) way to do this is:

$redis.keys("foo:*").each do |key|
  $redis.del(key)
end

I'd like to avoid loading all the keys into memory, and then making numerous requests to the redis server.

Another way that I like is to use the splat operator:

keys = $redis.keys("foo:*")
$redis.del(*keys)

The problem is that I don't know what the maximum arity of the $redis.del method, nor of any ruby method, I can't seem to find it online.

What is the maximum arity?

Was it helpful?

Solution

@muistooshort in the comments had a good suggestion that turned out to be right, the redis driver knows what to do with an array argument:

 # there are 1,000,000 keys of the form "foo:#{number}"
 keys = $redis.keys("foo:*")
 $redis.del(keys) # => 1000000

Simply pass an array of keys to $redis.del

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top