开发者

Using a regex to query the Rails cache to allow partial matches and return multiple items

开发者 https://www.devze.com 2023-04-12 17:07 出处:网络
In brief i\'m w开发者_如何学编程riting a plugin for a Rails app and I want to cache the data the plugin generates, however I want the ability to delete the cached items should I need to do so.

In brief i'm w开发者_如何学编程riting a plugin for a Rails app and I want to cache the data the plugin generates, however I want the ability to delete the cached items should I need to do so.

e.g.

Rails.cache.write("data_1", "the data")
Rails.cache.write("data_2", "the data")
Rails.cache.write("data_3", "the data")

What should I be calling to fetch all keys that match the regex /^data_/ so I can delete them?

I tried:

Rails.cache.fetch(/^data_/)
Rails.cache.read(/^data_/)

Any help/pointers would be greatly appreciated.


You can use .match. So something like this should work:

if Rails.cache.fetch(..).match(/^data_/)
  do something
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号