How to Clear All Cache in Play Framework 2.1

倖福魔咒の 提交于 2019-12-23 16:31:05

问题


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.


回答1:


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.




回答2:


here's an example if your using default EhCachePlugin

import play.api.Play.current

...

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



回答3:


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) }
    }
  }



回答4:


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
    }
}



回答5:


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.




回答6:


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());
}


来源:https://stackoverflow.com/questions/17682536/how-to-clear-all-cache-in-play-framework-2-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!