Manually caching Rest without checking headers for ETag and Last-Modified

huangapple go评论62阅读模式
英文:

Manually caching Rest without checking headers for ETag and Last-Modified

问题

我正在使用Rest来调用一个我无法访问的远程服务器。我想要永久缓存接收到的数据,以便离线使用,而不必检查标头中的Last-ModifiedETag

我期望CachingMode.MANUAL机制会检查是否存在离线内容,如果没有,就会在线获取内容,但事实并非如此。

为了绕过这个问题,我首先使用RestCachingMode.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

huangapple
  • 本文由 发表于 2020年8月9日 20:49:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63326528.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定