英文:
Manually caching Rest without checking headers for ETag and Last-Modified
问题
我正在使用Rest
来调用一个我无法访问的远程服务器。我想要永久缓存接收到的数据,以便离线使用,而不必检查标头中的Last-Modified或ETag。
我期望CachingMode.MANUAL
机制会检查是否存在离线内容,如果没有,就会在线获取内容,但事实并非如此。
为了绕过这个问题,我首先使用Rest
和CachingMode.OFFLINE
,如果返回404,则再次使用CachingMode.SMART
进行调用。
是否应该有一个选项(比如CachingMode.OFFLINE_FIRST
)来首先检查离线内容,如果没有内容,则使用(CachingMode.SMART
)在线获取?
以下是我的当前方法:
英文:
I'm using Rest
to make a call to a remote server that I have no access to. I want to cache received data permanently for offline use without checking Last-Modified or ETag in the headers.
I expected CachingMode.MANUAL
mechanism to check if there's an offline content and if there's none, then go online to fetch the content, but it doesn't.
To circumvent this, I had to first use Rest
with CachingMode.OFFLINE
and if that returns 404, then make another call with CachingMode.SMART
.
Shouldn't there be an option of (let's say CachingMode.OFFLINE_FIRST
) that checks offline first and if no content then goes online with (CachingMode.SMART
)?
Below is my current approach:
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();
}
答案1
得分: 1
这有道理。在这次提交中添加了对此的支持:https://github.com/codenameone/CodenameOne/commit/fd81d979507fb08ee1d595b94df5973b322766a3
英文:
This makes sense. Added support for this in this commit: https://github.com/codenameone/CodenameOne/commit/fd81d979507fb08ee1d595b94df5973b322766a3
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论