简体   繁体   English

使用ruby删除redis键匹配模式

[英]Delete redis key matching pattern using ruby

I want to delete all redis keys defined under namespace "datetime_filter" in ruby (maintenance task).我想删除在 ruby​​ 命名空间“datetime_filter”下定义的所有 redis 键(维护任务)。 How to do this ?这该怎么做 ?

The right way to do this if you don't want to block the server is to use the SCAN command.如果您不想阻止服务器,正确的方法是使用SCAN命令。 The command will provide you an iterator returning only the keys matching your pattern if you wish (in this case it is appropriate to use the MATCH option for sure).如果您愿意,该命令将为您提供一个迭代器,仅返回与您的模式匹配的键(在这种情况下,使用MATCH选项肯定是合适的)。 The Ruby script will just have to iterate and delete. Ruby 脚本只需要迭代和删除。

So:所以:

WHILE keys = SCAN MATCH datetime_filter*
    FOREACH key in keys DEL key

Try this -尝试这个 -

 $redis.del(datetime_filter_key)

and follow following approach -并遵循以下方法 -

In redis, how do i remove keys? 在redis中,我如何删除密钥?

You could use:你可以使用:

Rails.cache.redis.keys.grep(/pattern/).each do |k|
  Rails.cache.redis.del(k)
end

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM