简体   繁体   中英

Play 2.6: Cache Api and Memory Usage Configuration

I'm using cache api to cache some ordinary http responses; however I would like to configure its memory usage. Is it possible to put a memory limitation on it? Or even configure it simillar to the other parts of the Playframework (via application.conf ).

Not particularly well documented, but here are some clues.

From ScalaCache#Accessing-different-caches :

In the default Ehcache implementation, the default cache is called play, and can be configured by creating a file called ehcache.xml.

And from Play's Dependencies.scala :

val ehcacheVersion = "2.10.4"
val playEhcacheDeps = Seq(
  "net.sf.ehcache" % "ehcache" % ehcacheVersion,
  "org.ehcache" % "jcache" % "1.0.1"
) ++ jcacheApi

And here's Play's ehcache-default.xml :

<!--
~ Copyright (C) 2009-2017 Lightbend Inc. <https://www.lightbend.com>
-->
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../config/ehcache.xsd" updateCheck="false">

    <defaultCache
            maxElementsInMemory="10000"
            eternal="false"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            overflowToDisk="false"
            maxElementsOnDisk="10000000"
            diskPersistent="false"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU"
    /> 

</ehcache>

Copy the contents of that file and include it as a resource in your project named ehcache.xml .

So let's go find the documentation for EhCache 2.10.4 .

Tuning Ehcache often involves sizing the data storage tiers appropriately. You can size the different data tiers in a number of ways using simple sizing attributes. These sizing attributes affect memory and disk resources. The following table summarizes the sizing attributes you can use.

I won't quote the table but it mentions a maxBytesLocalHeap which sounds promising.

Play! documentation here shows that you can configure your application's cache with configuration file. This is an example that shows you how to limit you cache size based on the number of objects rather than the number of bytes which is the default. Here also you will find the ehcache (default Play! cache provider) xml configuration documentation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM