文件

§設定 WS 快取

Play WS 允許您從設定中設定 HTTP 快取。

§啟用快取

您必須在 Play 應用程式中提供 JSR 107 快取實作(又稱 JCache)才能使用 Play WS 的快取功能。Play 附帶使用 ehcache 的實作,因此最簡單的實作方式是在 build.sbt 中加入下列內容

libraryDependencies += ws
libraryDependencies += ehcache

並在 application.conf 中加入下列內容以啟用 HTTP 快取

play.ws.cache.enabled=true

如果找不到 JCache 實作,Play WS 將使用具有不儲存任何內容的 stub 快取的 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" />

§定義快取提供者

你可以為 WS 快取定義特定的 CachingProvider,即使你已經將 ehcache 用作 Play 快取的快取提供者。例如,你可以載入 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
}

下一頁:靜態資源


在此文件中發現錯誤?此頁面的原始程式碼可在此處找到 here。在閱讀 文件指南 後,請隨時提交拉取請求。有問題或建議要分享嗎?前往 我們的社群論壇 與社群展開對話。