简体   繁体   中英

Rails.cache.clear certain key names?

Is it possible to somehow run Rails.cache.clear and only clear keys with a certain name/string?

I don't want to clear the entire cache...just keys with the string blog/post in the name (ie. blog/post/1 , blog/post/2 ).

I'm using dalli with memcached for my cache and running Rails 3.0.6.

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 delete action -

Rails.cache.delete('key')

Delete multiple keys -

Rails.cache.delete_if {|k, v| k =~ 'key' }

要回答我自己的问题......似乎在我使用memcached时,我实际上不能使用delete_ifdelete_matched因为memcached不支持按模式( 1 )枚举或查询键。

您可以使用https://github.com/Phobos98/dalli-delete-matched gem为dalli store添加memcached的delete_matched方法的简单实现。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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