§WSキャッシュの設定
Play WSでは、設定からHTTPキャッシュを設定できます。
§キャッシュの有効化
Play WSのキャッシュ機能を使用するには、PlayアプリケーションでJSR 107キャッシュ実装(別名JCache)が利用可能である必要があります。Playにはehcacheを使用する実装が付属しているため、最も簡単な実装方法は、`build.sbt`に以下を追加することです。
libraryDependencies += ws
libraryDependencies += ehcache
そして、`application.conf`に以下を追加してHTTPキャッシュを有効にします。
play.ws.cache.enabled=true
JCache実装が見つからない場合、Play WSは何も保存しないスタブキャッシュを持つHTTPキャッシュを使用します。
§鮮度ヒューリスティックの有効化
デフォルトでは、明示的なキャッシュ情報が渡されない場合、Play WSはHTTPレスポンスをキャッシュしません。ただし、HTTPキャッシュには、ヒューリスティックに基づいてページをキャッシュするオプションがあり、リモートサーバーからの協力なしでもレスポンスをキャッシュできます。
ヒューリスティックを有効にするには、`application.conf`で以下を設定します。
play.ws.cache.heuristics.enabled=true
Play WSは、LM-Factorアルゴリズムを使用してHTTPレスポンスをキャッシュします。
§キャッシュサイズの制限
キャッシュサイズは、基盤となるキャッシュ実装によって制限されます。キャッシュが見つからない場合、Play WSは汎用キャッシュを作成しますが、JCacheは多くのオプションを提供していないため、キャッシュを明示的に制限する必要があります。
**注意**:HTTPキャッシュを制限しない、またはキャッシュ内の要素を期限切れにしない場合、JVMのメモリ不足が発生する可能性があります。
ehcacheでは、CacheManagerリソースを明示的に指定することで既存のキャッシュを指定できます。これは`cachingProvider.getCacheManager(uri, environment.classLoader)`で使用されます。
play.ws.cache.cacheManagerResource="ehcache-play-ws-cache.xml"
そして、`conf`ディレクトリに次のようなキャッシュを追加します。
<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>
**注意**: `play.ws.cache.cacheManagerURI`は非推奨です。代わりにクラスパス上のパスを持つ`play.ws.cache.cacheManagerResource`を使用してください。
§キャッシュのデバッグ
Play WSのHTTPキャッシュレイヤーが何をしているかを正確に確認するには、`logback.xml`に以下を追加してください。
<logger name="play.api.libs.ws.ahc.cache" level="TRACE" />
§キャッシュプロバイダーの定義
Play Cacheに`ehcache`をキャッシュプロバイダーとして既に使用している場合でも、WSキャッシュに特定のCachingProviderを定義できます。たとえば、Caffeineライブラリを読み込むことができます。
// https://mvnrepository.com/artifact/com.github.ben-manes.caffeine/jcache
libraryDependencies += "com.github.ben-manes.caffeine" % "jcache" % "2.4.0"
そして、Caffeine JCacheをキャッシュプロバイダーとして指定します。
play.ws.cache.cachingProviderName="<jcache caching provider class name>"
§参照設定
参照設定は、Play WSキャッシュのデフォルト設定を示しています。
# Copyright (C) from 2022 The Play Framework Contributors <https://github.com/playframework>, 2011-2021 Lightbend Inc. <https://www.lightbend.com>
play {
modules {
enabled += "play.api.libs.ws.ahc.AhcWSModule"
enabled += "play.libs.ws.ahc.AhcWSModule"
}
}
# Configuration settings for JSR 107 Cache for Play WS.
play.ws.cache {
# True if caching is enabled for the default WS client, false by default
enabled = false
# Calculates heuristic freshness if no explicit freshness is enabled
# according to https://tools.ietf.org/html/rfc7234#section-4.2.2 with LM-Freshness
heuristics.enabled = false
# The name of the cache
name = "play-ws-cache"
# A specific caching provider name (e.g. if both ehcache and caffeine are set)
cachingProviderName = ""
# The CacheManager resource to use. For example:
#
# cacheManagerResource = "ehcache-play-ws-cache.xml"
#
# If null, will use the ehcache default URI.
cacheManagerResource = null
# The CacheManager URI to use. If non-null, this is used instead of cacheManagerResource.
cacheManagerURI = null
}
**次:**静的アセット
このドキュメントに誤りを見つけましたか?このページのソースコードはこちらにあります。ドキュメントガイドラインを読んだ後、プルリクエストを送信してください。質問やアドバイスがあれば、コミュニティフォーラムにアクセスして、コミュニティとの会話を始めてください。