Java 游戏框架中的咖啡因缓存不';t返回缓存的响应
我在java Play Framework作业中使用咖啡因缓存实现来缓存传出的http请求,我发现,尽管缓存已启动并正在运行,但我的服务仍会每次命中外部服务器,而不是从缓存返回值。 我看到日志中记录的密钥总是相同的 因此,我看到未设置Java 游戏框架中的咖啡因缓存不';t返回缓存的响应,java,caching,playframework,caffeine,caffeine-cache,Java,Caching,Playframework,Caffeine,Caffeine Cache,我在java Play Framework作业中使用咖啡因缓存实现来缓存传出的http请求,我发现,尽管缓存已启动并正在运行,但我的服务仍会每次命中外部服务器,而不是从缓存返回值。 我看到日志中记录的密钥总是相同的 因此,我看到未设置freshnesslifest,因此这可能意味着缓存立即过期。但我仍然不知道如何在配置中设置它 我没有更改我的java代码,如果需要,我找不到任何示例,或者我只需要为所有传出请求提供缓存配置。 我将非常感谢您的帮助 我的build.sbt包含: libraryDep
freshnesslifest
,因此这可能意味着缓存立即过期。但我仍然不知道如何在配置中设置它
我没有更改我的java代码,如果需要,我找不到任何示例,或者我只需要为所有传出请求提供缓存配置。
我将非常感谢您的帮助
我的build.sbt
包含:
libraryDependencies += ws
libraryDependencies += caffeine
# Configuration settings for JSR 107 Cache for Play WS.
play.ws.cache {
enabled = true
heuristics.enabled = false
name = "play-ws-cache"
}
# source https://github.com/ben-manes/caffeine/blob/master/jcache/src/main/resources/reference.conf
caffeine.jcache {
# A named cache is configured by nesting a new definition under the caffeine.jcache namespace. The
# per-cache configuration is overlaid on top of the default configuration.
play-ws-cache {
read-through {
# If enabled, the entry is loaded automatically on a cache miss
enabled = true
}
# The eviction policy for automatically removing entries from the cache
policy {
# The expiration threshold before lazily evicting an entry. This single threshold is reset on
# every operation where a duration is specified. As expected by the specification, if an entry
# expires but is not accessed and no resource constraints force eviction, then the expired
# entry remains in place.
lazy-expiration {
# The duration before a read of an entry is considered expired. If set to 0 then the entry
# is considered immediately expired. May be a time duration, null to indicate no change, or
# "eternal" to indicate no expiration.
access = 5m
}
# The expiration thresholds before eagerly evicting an entry. These settings correspond to the
# expiration supported natively by Caffeine where expired entries are collected during
# maintenance operations.
eager-expiration {
# Specifies that each entry should be automatically removed from the cache once a fixed
# duration has elapsed after the entry's creation, the most recent replacement of its value,
# or its last read. Access time is reset by all cache read and write operation. This setting
# cannot be combined with the variable configuration.
after-access = 5m
}
# The maximum bounding of the cache based upon its logical size
maximum {
# The maximum number of entries that can be held by the cache. This setting cannot be
# combined with the weight configuration.
size = 10000
}
}
}
}
我的reference.conf
包含:
libraryDependencies += ws
libraryDependencies += caffeine
# Configuration settings for JSR 107 Cache for Play WS.
play.ws.cache {
enabled = true
heuristics.enabled = false
name = "play-ws-cache"
}
# source https://github.com/ben-manes/caffeine/blob/master/jcache/src/main/resources/reference.conf
caffeine.jcache {
# A named cache is configured by nesting a new definition under the caffeine.jcache namespace. The
# per-cache configuration is overlaid on top of the default configuration.
play-ws-cache {
read-through {
# If enabled, the entry is loaded automatically on a cache miss
enabled = true
}
# The eviction policy for automatically removing entries from the cache
policy {
# The expiration threshold before lazily evicting an entry. This single threshold is reset on
# every operation where a duration is specified. As expected by the specification, if an entry
# expires but is not accessed and no resource constraints force eviction, then the expired
# entry remains in place.
lazy-expiration {
# The duration before a read of an entry is considered expired. If set to 0 then the entry
# is considered immediately expired. May be a time duration, null to indicate no change, or
# "eternal" to indicate no expiration.
access = 5m
}
# The expiration thresholds before eagerly evicting an entry. These settings correspond to the
# expiration supported natively by Caffeine where expired entries are collected during
# maintenance operations.
eager-expiration {
# Specifies that each entry should be automatically removed from the cache once a fixed
# duration has elapsed after the entry's creation, the most recent replacement of its value,
# or its last read. Access time is reset by all cache read and write operation. This setting
# cannot be combined with the variable configuration.
after-access = 5m
}
# The maximum bounding of the cache based upon its logical size
maximum {
# The maximum number of entries that can be held by the cache. This setting cannot be
# combined with the weight configuration.
size = 10000
}
}
}
}
我的application.conf
包含:
libraryDependencies += ws
libraryDependencies += caffeine
# Configuration settings for JSR 107 Cache for Play WS.
play.ws.cache {
enabled = true
heuristics.enabled = false
name = "play-ws-cache"
}
# source https://github.com/ben-manes/caffeine/blob/master/jcache/src/main/resources/reference.conf
caffeine.jcache {
# A named cache is configured by nesting a new definition under the caffeine.jcache namespace. The
# per-cache configuration is overlaid on top of the default configuration.
play-ws-cache {
read-through {
# If enabled, the entry is loaded automatically on a cache miss
enabled = true
}
# The eviction policy for automatically removing entries from the cache
policy {
# The expiration threshold before lazily evicting an entry. This single threshold is reset on
# every operation where a duration is specified. As expected by the specification, if an entry
# expires but is not accessed and no resource constraints force eviction, then the expired
# entry remains in place.
lazy-expiration {
# The duration before a read of an entry is considered expired. If set to 0 then the entry
# is considered immediately expired. May be a time duration, null to indicate no change, or
# "eternal" to indicate no expiration.
access = 5m
}
# The expiration thresholds before eagerly evicting an entry. These settings correspond to the
# expiration supported natively by Caffeine where expired entries are collected during
# maintenance operations.
eager-expiration {
# Specifies that each entry should be automatically removed from the cache once a fixed
# duration has elapsed after the entry's creation, the most recent replacement of its value,
# or its last read. Access time is reset by all cache read and write operation. This setting
# cannot be combined with the variable configuration.
after-access = 5m
}
# The maximum bounding of the cache based upon its logical size
maximum {
# The maximum number of entries that can be held by the cache. This setting cannot be
# combined with the weight configuration.
size = 10000
}
}
}
}
我在日志中看到:
[调试]-restclient.BaseRestClient-使用url执行GET
http://localhost:9001/entities/v2/867386732 [跟踪]-
p、 a.l.w.a.c.CachingAsyncHttpClient-执行:请求=请求(获取
http://localhost:9001/entities/v2/867386732),handler=
AsyncHandler(play.libs.ws.ahc.StandaloneAhcWSClient$ResponseAsyncCompletionHandler@65b0b233),
future=null[debug]-p.a.libs.ws.ahc.cache.AhcHttpCache-get:key
得到http://localhost:9001/entities/v2/867386732 [调试]-p.a.l.w.a.c.CachingAsyncHttpClient-执行GET
http://localhost:9001/entities/v2/867386732: 结果=
列表(ResponseEntry)缓存响应(status=
CachableHttpResponseStatus(代码=200,文本=OK),标题=
DefaultHttpHeaders[参考者策略:跨来源时的来源,
严格原点交叉原点时,X帧选项:拒绝,
X-XSS-Protection:1;模式=块,X-Content-Type-Options:nosniff,
X-允许-跨域-策略:仅主策略,日期:2020年10月1日星期四
17:44:08 GMT,内容类型:application/json,内容长度:2144],
车身零件尺寸=
1) ,GET,Map(),一些(2020-10-02T10:44:08.760450-07:00[美国/洛杉矶])
[跟踪]-c.t.c.响应选择计算器-查找匹配响应:
请求=
缓存请求(http://localhost:9001/entities/v2/867386732,GET,TreeMap()),
回应=
列表(存储响应)(http://localhost:9001/entities/v2/867386732,200,树形图(日期)
->列表(周四,2020年10月1日17:44:08 GMT),内容类型->列表(应用程序/json),内容长度->列表(2144),推荐人策略
->列表(交叉原点时原点,交叉原点时严格原点),X-Frame-Options->List(拒绝),X-XSS-Protection->List(1;
模式=块),X-Content-Type-Options->List(nosniff),
X-allowed-Cross-Domain-Policies->List(仅限主服务器)),GET,Map())
[调试]-p.a.l.w.a.c.CachingAsyncHttpClient-执行GET
http://localhost:9001/entities/v2/867386732: 从缓存中选择:
ResponseEntry(可缓存响应(状态=
CachableHttpResponseStatus(代码=200,文本=OK),标题=
DefaultHttpHeaders[参考者策略:跨来源时的来源,
严格原点交叉原点时,X帧选项:拒绝,
X-XSS-Protection:1;模式=块,X-Content-Type-Options:nosniff,
X-允许-跨域-策略:仅主策略,日期:2020年10月1日星期四
17:44:08 GMT,内容类型:application/json,内容长度:2144],
车身零件尺寸=
1) ,GET,Map(),一些(2020-10-02T10:44:08.760450-07:00[美国/洛杉矶])
[trace]-c.t.c.CurrentAgeClulator-CalculateUrrentage(标题:
树状图(日期->列表(周四,2020年10月1日17:44:08 GMT),内容类型->
列表(应用程序/json)、内容长度->列表(2144)、推荐人策略
->列表(交叉原点时原点,交叉原点时严格原点),X-Frame-Options->List(拒绝),X-XSS-Protection->List(1;
模式=块),X-Content-Type-Options->List(nosniff),
X-Allowed-Cross-Domain-Policys->List(仅限主服务器)),现在:
2020-10-01T17:44:38.690992Z[GMT],请求时间:
2020-10-01T17:44:38.690274Z[GMT],响应时间:
2020-10-01T17:44:38.690981Z[GMT])[trace]-c.t.c.currentagecoculator
- calculateCurrentAge:currentAge=PT30S[debug]-c.t.c.ResponseServingCalculator-服务器响应:找到的响应 “得到http://localhost:9001/entities/v2/867386732,年龄=30[跟踪]- c、 t.c.ResponseServingCalculator-noCacheFound:请求= 缓存请求(http://localhost:9001/entities/v2/867386732,GET,TreeMap()), 回应= 存储响应(http://localhost:9001/entities/v2/867386732,200,树形图(日期) ->列表(周四,2020年10月1日17:44:08 GMT),内容类型->列表(应用程序/json),内容长度->列表(2144),推荐人策略 ->列表(交叉原点时原点,交叉原点时严格原点),X-Frame-Options->List(拒绝),X-XSS-Protection->List(1; 模式=块),X-Content-Type-Options->List(nosniff), X-allowed-Cross-Domain-Policies->List(仅限主服务器)),GET,Map() [trace]-c.t.c.响应存储计算器-isCachedResponseFresh: 请求= 缓存请求(http://localhost:9001/entities/v2/867386732,GET,TreeMap()), 回应= 存储响应(http://localhost:9001/entities/v2/867386732,200,树形图(日期) ->列表(周四,2020年10月1日17:44:08 GMT),内容类型->列表(应用程序/json),内容长度->列表(2144),推荐人策略 ->列表(交叉原点时原点,交叉原点时严格原点),X-Frame-Options->List(拒绝),X-XSS-Protection->List(1; 模式=块),X-Content-Type-Options->List(nosniff), X-allowed-Cross-Domain-Policies->List(仅限主服务器)),GET,Map() [跟踪]-c.t.c.新鲜度计算器-计算新鲜度寿命: [调试]-c.t.c.FreshnessCalculator-calculateFreshnessLifetime: freshnessLifetime=无[debug]-c.t.c.Freshness计算器- calculateFreshnessLifetime:result=PT0S[debug]- c、 t.c.响应服务计算器-isCachedResponseFresh: freshnessLifetime=PT0S,currentAge=PT30S[调试]- c、 t.c.响应服务计算器-isCachedResponseFresh: freshnessLifetime=PT0S,currentAge=PT30S[跟踪]- c、 t.c.责任服务计算器-ISStalleResponseProhibited:请求= 缓存请求(http://localhost:9001/entities/v2/867386732,GET,TreeMap()), 回应= 仓库响应