So I'm using Play's built in cache API as seen here: http://www.playframework.com/documentation/2.1.x/JavaCache
In my code I've already set the cache to expire every 10 minutes. I am also using the session cache style.
So my main question is, since it's gonna be really hard to keep track of all cache, how do I clear all of the cache? I know that using Play's default cache is minimal but it's working perfectly for me at this point. I just want the ability to clear the cache once in awhile just in case too many sessions are made and somewhere in my code it's piling up the caches.
The Play Java API does not provide a way to clear the whole cache.
You'll have to use your own cache plugin, or extends the existing one to provide this feature.
Store all keys that you use for caching in a Set e.g. HashSet and when you want to delete the entire cache just iterate over the set and call
You can write an Akka system scheduler and Actor to clear your caches at given intervals then set it to run in your Global file. The Play Cache api doesn't have a method to list all of your keys, but I use scheduler jobs to manage clearing the cache by using Cache.remove manually on my list of cache keys. If you're using Play 2.2 the Cache module moved. Some caching modules have an api to clear the entire cache.
Thanks @alessandro.negrin for pointing how to access EhCachePlugin.
Here are some further details in that direction. Tested using Play 2.2.1 default EhCachePlugin:
Then you have access to Cache instance methods such as ehCache.removeAll():
Please note this is different than cacheManager.clearAll() described by @alessandro.negrin which according to the doc: "Clears the contents of all caches in the CacheManager,(...)", potentially other ehCache than "play" cache.
In addition you have access to Cache methods such as
getKeys
that might allow you to select a subset of keys containing amatchString
, for instance to perform remove operations to:here's an example if your using default EhCachePlugin
In Play 2.5.x one can access
EhCache
directly injectingCacheManagerProvider
and use the fullEhCache
API: