gpt4 book ai didi

java - 手动缓存 Rest 而不检查 ETag 和 Last-Modified 的 header

转载 作者:行者123 更新时间:2023-12-01 14:31:36 25 4
gpt4 key购买 nike

我正在使用 Rest调用我无权访问的远程服务器。我想永久缓存接收到的数据以供离线使用,而不检查 header 中的 Last-ModifiedETag

我预计 CachingMode.MANUAL检查是否有离线内容的机制,如果没有,则上网获取内容,但没有。

为了避免这种情况,我必须先使用 RestCachingMode.OFFLINE如果返回 404,则使用 CachingMode.SMART 再次调用.

难道不应该有一个选项(比方说 CachingMode.OFFLINE_FIRST )首先离线检查,如果没有内容然后在线使用( CachingMode.SMART )?

以下是我目前的做法:

Response<Map> response = Rest.get(url)
.cacheMode(CachingMode.OFFLINE)
.queryParam("param", value)
.jsonContent().onErrorCodeJSON(e -> {
throw new RuntimeException(createErrorMessage(e));
}).onError(e -> {
if (e.getResponseCode() == 0 || e.getConnectionRequest().getResponseCode() == 404) {
is404 = true;
return;
}

throw new RuntimeException("Network error. Please check your connection and try again.");
}).timeout(6000).getAsJsonMap();

if (is404) {
is404 = false;
response = Rest.get(url)
.cacheMode(CachingMode.SMART)
.queryParam("param", value)
.jsonContent().onErrorCodeJSON(e -> {
throw new RuntimeException(createErrorMessage(e));
}).onError(e -> {
throw new RuntimeException("Network error. Please check your connection and try again.");
}).timeout(6000).getAsJsonMap();
}

最佳答案

这是有道理的。在此提交中添加了对此的支持:https://github.com/codenameone/CodenameOne/commit/fd81d979507fb08ee1d595b94df5973b322766a3

关于java - 手动缓存 Rest 而不检查 ETag 和 Last-Modified 的 header ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63326528/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com