【问题标题】:How to Clear All Cache in Play Framework 2.1Play Framework 2.1如何清除所有缓存
【发布时间】:2013-07-16 16:52:49
【问题描述】:

所以我正在使用 Play 的内置缓存 API,如下所示:http://www.playframework.com/documentation/2.1.x/JavaCache

在我的代码中,我已经将缓存设置为每 10 分钟过期一次。我也在使用会话缓存样式。

所以我的主要问题是,既然很难跟踪所有缓存,我该如何清除所有缓存?我知道使用 Play 的默认缓存很少,但目前它对我来说非常有效。我只是希望能够偶尔清除一次缓存,以防万一进行了太多会话并且在我的代码中某处它正在堆积缓存。

【问题讨论】:

  • 你是如何清除缓存的?

标签: playframework playframework-2.1


【解决方案1】:

Play Java API 不提供清除整个缓存的方法。

您必须使用自己的缓存插件,或扩展 the existing one 以提供此功能。

【讨论】:

  • 感谢您的意见。好吧,我知道清除缓存的一种方法是重新编译代码。
【解决方案2】:

如果您使用默认的 EhCachePlugin,这是一个示例

import play.api.Play.current

...

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

【讨论】:

    【解决方案3】:

    感谢@alessandro.negrin 指出如何访问 EhCachePlugin。

    这里有一些关于这个方向的更多细节。使用 Play 2.2.1 默认 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)
    

    那么你就可以访问Cache实例方法如ehCache.removeAll()

      // Removes all cached items.
      ehCache.removeAll()
    

    请注意这与@alessandro.negrin 描述的cacheManager.clearAll() 不同 根据文档:“清除CacheManager中所有缓存的内容,(...)”, 可能是“播放”缓存之外的其他 ehCache。

    此外,您还可以访问 Cache 方法,例如 getKeys,这可能允许您 选择包含matchString 的键子集,例如执行删除操作:

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

    【讨论】:

      【解决方案4】:

      在 Play 2.5.x 中,可以直接注入 CacheManagerProvider 访问 EhCache 并使用完整的 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】:

        您可以编写一个 Akka 系统调度程序和 Actor 以在给定的时间间隔清除您的缓存,然后将其设置为在您的全局文件中运行。 Play Cache api 没有列出所有键的方法,但我使用调度程序作业通过在我的缓存键列表上手动使用 Cache.remove 来管理清除缓存。如果您使用Play 2.2,则缓存模块已移动。一些缓存模块有一个api来清除整个缓存。

        【讨论】:

          【解决方案6】:

          将用于缓存的所有密钥存储在 Set 中,例如HashSet,当你想删除整个缓存时,只需遍历集合并调用

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

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2017-09-18
            • 2020-03-28
            • 2020-10-02
            • 2010-10-07
            • 2011-01-09
            • 2016-05-04
            • 1970-01-01
            相关资源
            最近更新 更多