cYn
cYn

Reputation: 3381

How to Clear All Cache in Play Framework 2.1

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.

Upvotes: 3

Views: 10159

Answers (6)

pcejrowski
pcejrowski

Reputation: 622

In Play 2.5.x one can access EhCache directly injecting CacheManagerProvider and use the full EhCache API:

import com.google.inject.Inject
import net.sf.ehcache.{Cache, Element}
import play.api.cache.CacheManagerProvider
import play.api.mvc.{Action, Controller}

class MyController @Inject()(cacheProvider: CacheManagerProvider) extends Controller {
    def testCache() = Action{
        val cache: Cache = cacheProvider.get.getCache("play")
        cache.put(new Element("key1", "val1"))
        cache.put(new Element("key2", "val3"))
        cache.put(new Element("key3", "val3"))
        cache.removeAll()
        Ok
    }
}

Upvotes: 2

enigma969
enigma969

Reputation: 427

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

Iterator iter = cacheSet.iterator();
while (iter.hasNext()) {
   Cache.remove(iter.next());
}

Upvotes: 0

Patrick Refondini
Patrick Refondini

Reputation: 685

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:

  import play.api.cache.Cache
  import play.api.Play
  import play.api.Play.current
  import play.api.cache.EhCachePlugin

  // EhCache is a Java library returning i.e. java.util.List
  import scala.collection.JavaConversions._

  // Default Play (2.2.x) cache name 
  val CACHE_NAME = "play"

  // Get a reference to the EhCachePlugin manager
  val cacheManager = Play.application.plugin[EhCachePlugin]
    .getOrElse(throw new RuntimeException("EhCachePlugin not loaded")).manager

  // Get a reference to the Cache implementation (here for play)
  val ehCache = cacheManager.getCache(CACHE_NAME)

Then you have access to Cache instance methods such as ehCache.removeAll():

  // Removes all cached items.
  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 a matchString, for instance to perform remove operations to:

  val matchString = "A STRING"

  val ehCacheKeys = ehCache.getKeys() 

  for (key <- ehCacheKeys) {
    key match {
      case stringKey: String => 
        if (stringKey.contains(matchString)) { ehCache.remove(stringKey) }
    }
  }

Upvotes: 2

alessandro.negrin
alessandro.negrin

Reputation: 31

here's an example if your using default EhCachePlugin

import play.api.Play.current

...

for(p <- current.plugin[EhCachePlugin]){
  p.manager.clearAll
}

Upvotes: 3

JMParsons
JMParsons

Reputation: 506

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.

Upvotes: 1

ndeverge
ndeverge

Reputation: 21564

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.

Upvotes: 4

Related Questions