Reputation: 165
I am trying to use
redis.mapped_mset({ "f1" => "v1", "f2" => "v2" })
to set multiple keys into Redis
and I can not set expire time at the same time.
The only way to set expire time to to use this:
set(key, value, options = {})
or
expire(key, seconds)
I have to call many times and this is not what I want to see. Are there any other ways to solve this problem?
Upvotes: 7
Views: 16809
Reputation: 121000
Redis itself does not support multiple setting with an expiration parameter. Redis#mapped_set
is a syntactic sugar to call mset
and mset
itself is a syntactic sugar to transactionally call subsequent set
many times.
So, the only thing you need is to wrap subsequent calls to set(... ex:...)
into a transaction with Redis#multi
.
Upvotes: 7
Reputation: 5214
You can write wrapper method to set a list and add expiration. Use redis.multi
to wrap it into trasactional module.
def set_list(list, expire_in = 10)
redis.multi do
list.each{ |k, v| redis.set(k,v, ex: expire_in)}
end
end
Upvotes: 6