documentation/manual/working/commonGuide/configuration/WsCache.md
[[Play WS|ScalaWS]] allows you to set up HTTP caching from configuration.
You must have a JSR 107 cache implementation (aka JCache) available in your Play application to use Play WS's cache facility. Play comes with an implementation that uses ehcache, so the easiest implementation is to add the following to build.sbt:
And enable the HTTP cache by adding the following to application.conf
play.ws.cache.enabled=true
If no JCache implementation is found, then Play WS will use an HTTP Cache with a stub cache that does not store anything.
By default, Play WS does not cache HTTP responses when no explicit cache information is passed in. However, HTTP caching does have an option to cache pages based off heuristics so that you can cache responses even without co-operation from the remote server.
To enable heuristics, set the following in application.conf:
play.ws.cache.heuristics.enabled=true
Play WS uses the LM-Factor algorithm to cache HTTP responses.
Cache size is limited by the underlying cache implementation. Play WS will create a generic cache if no cache was found, but you should bound the cache explicitly, as JCache does not provide many options.
NOTE: If you do not limit the HTTP cache or expire elements in the cache, then you may cause the JVM to run out of memory.
In ehcache, you can specify an existing cache by specifying CacheManager resource explicitly, which is used in cachingProvider.getCacheManager(uri, environment.classLoader):
play.ws.cache.cacheManagerResource="ehcache-play-ws-cache.xml"
and then adding a cache such as following into the conf directory:
<ehcache
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
name="play-ws-cache"
updateCheck="false"
>
<cache name="play-ws-cache" maxEntriesLocalHeap="10000" eternal="false"
timeToIdleSeconds="360" timeToLiveSeconds="1000" overflowToDisk="false" />
</ehcache>
NOTE:
play.ws.cache.cacheManagerURIis deprecated, useplay.ws.cache.cacheManagerResourcewith a path on the classpath instead.
To see exactly what the HTTP caching layer in Play WS is doing, please add the following to logback.xml:
<logger name="play.api.libs.ws.ahc.cache" level="TRACE" />
You can define a specific CachingProvider for the WS cache, even if you are already using ehcache as a caching provider for Play Cache. For example, you can load the Caffeine library:
// https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/jcache
libraryDependencies += "com.github.ben-manes.caffeine" % "jcache" % "2.4.0"
and then specify Caffeine JCache as the caching provider:
play.ws.cache.cachingProviderName="<jcache caching provider class name>"
The reference configuration shows the default settings for Play WS Caching: