4

I am running Ruby on Rails and am using the Dalli gem to access memcached.

Question: how do I delete a range of keys (not multiple, but a range) for something like: delete all memcached entries with a key that begins with "USERINFO", in other words, how can I use wildcards to delete a range of keys?

KKK
  • 1,085
  • 11
  • 33

2 Answers2

0

Short answer is, no and you don't want to do that.

The dalli gem nor memcached support deleting multiple keys with a single command out of the box and for good reason. Since memcached determines the location of cached values by hashing the key, in a production environment with multiple cache nodes, a delete_matched operation would need to scan across all the nodes looking for keys that potentially match. This defeats a key goal of memcached which is performance.

There exist several implementations that extend dalli and promise to provide an implementation of deleted_matched. These all appear to trade-off programmer convenience over performance so use them with caution. Taking a look at sources of these gems before using them is a good start.


Related questions Is it possible to get/search Memcached keys by a prefix?

Community
  • 1
  • 1
Shyam Habarakada
  • 15,367
  • 3
  • 36
  • 47
-3

You should have a look at Rails.cache.delete_matched : http://apidock.com/rails/ActiveSupport/Cache/Store/delete_matched

Does exactly what you want, theorically :

Rails.cache.delete_matched /^USERINFO/

demental
  • 1,444
  • 13
  • 25
  • 4
    This method does not work with Dalli (or the standard Memcached cache store in Rails). – Mark Oct 15 '12 at 10:26
  • You can try this gem for adding delete_matched functionality to dalli https://github.com/Phobos98/dalli-delete-matched – mgauthier May 30 '14 at 15:01