4

I was wondering if there is a way to delete elements from the Play cache using a regex.

I'm using play 2.2.x and I'm storing elements in the cache following this pattern:

collectionName.identifier

Is there a way to expire the caché using a regular expression to match the key, like:

collectionName.[a-zA-Z0-9]+

The reason I want to do that is because sometimes I will update elements in db matching some fields, and I can't really know which elements were updated.

If there is a way in ReactiveMongo to get the updated object identifiers, that would help me as well.

Thanks for any help.

Rich Dougherty
  • 3,231
  • 21
  • 24
Bruno Follon
  • 422
  • 3
  • 13

1 Answers1

3

Play's cache uses Ehcache underneath. You'll need to work with Ehcache directly.

  1. Access the underlying Ehcache object using app.plugin[EhCachePlugin].cache (see the plugin source).

  2. Then call the Ehcache object's getKeys method to get the list of cache entry keys.

  3. Then match the keys yourself and remove any entries that match your regex.

By the way, it would be better if you update or remove items from the cache when you update the database.

Rich Dougherty
  • 3,231
  • 21
  • 24
  • Yeah, that's exactly what I'm trying to do (refreshing the cache just after I update the database), I'll try your answer and let you know, thanks. – Bruno Follon Mar 06 '15 at 08:28