Ruby-on-rails Rails.cache.clear 某些键名?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12005412/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Rails.cache.clear certain key names?
提问by Shpigford
Is it possible to somehow run Rails.cache.clearand only clear keys with a certain name/string?
是否有可能以某种方式运行Rails.cache.clear并只清除具有特定名称/字符串的键?
I don't want to clear the entire cache...just keys with the string blog/postin the name (ie. blog/post/1, blog/post/2).
我不想清除整个缓存...只是blog/post名称中带有字符串的键(即blog/post/1, blog/post/2)。
I'm using dalliwith memcached for my cache and running Rails 3.0.6.
我使用dalli和 memcached 作为我的缓存并运行 Rails 3.0.6。
采纳答案by Shpigford
回答by Sandip Ransing
This is how you can write to cache -
这是您可以写入缓存的方式 -
Rails.cache.write('key', 'value', :time_to_idle => 60.seconds, :timeToLive => 600.seconds)
and in order to delete from cache you can use deleteaction -
为了从缓存中删除,您可以使用delete操作 -
Rails.cache.delete('key')
Delete multiple keys -
删除多个键 -
Rails.cache.delete_if {|k, v| k =~ 'key' }
回答by fkoessler
You can use the https://github.com/Phobos98/dalli-delete-matchedgem that adds a simple implementation of delete_matchedmethod for dalli store with memcached.
您可以使用https://github.com/Phobos98/dalli-delete-matchedgem,它delete_matched为带有 memcached 的 dalli 商店添加了一个简单的方法实现。

