EhCachePlugin へのアクセス方法を指摘してくれた @alessandro.negrin に感謝します。
ここでは、その方向の詳細をいくつか示します。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)
次に、 ehCache.removeAll()などの Cache インスタンス メソッドにアクセスできます。
// Removes all cached items.
ehCache.removeAll()
これは、ドキュメントによると、@ alessandro.negrin によって説明されているcacheManager.clearAll()とは異なることに注意してください:「CacheManager 内のすべてのキャッシュの内容を消去します (...)」、「再生」キャッシュ以外の ehCache である可能性があります。
さらにgetKeys
、 を含むキーのサブセットを選択できるような Cache メソッドにアクセスできますmatchString
。たとえば、次のような削除操作を実行できます。
val matchString = "A STRING"
val ehCacheKeys = ehCache.getKeys()
for (key <- ehCacheKeys) {
key match {
case stringKey: String =>
if (stringKey.contains(matchString)) { ehCache.remove(stringKey) }
}
}