How to Clear All Cache in Play Framework 2.1

2019-07-03 22:17发布

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.

6条回答
2楼-- · 2019-07-03 22:30

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.

查看更多
Explosion°爆炸
3楼-- · 2019-07-03 22:32

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());
}
查看更多
不美不萌又怎样
4楼-- · 2019-07-03 22:34

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.

查看更多
Bombasti
5楼-- · 2019-07-03 22:39

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) }
    }
  }
查看更多
地球回转人心会变
6楼-- · 2019-07-03 22:45

here's an example if your using default EhCachePlugin

import play.api.Play.current

...

for(p <- current.plugin[EhCachePlugin]){
  p.manager.clearAll
}
查看更多
贼婆χ
7楼-- · 2019-07-03 22:46

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
    }
}
查看更多
登录 后发表回答