如何缓存来自 Web 服务器的 okHTTP 响应?

How to cache okHTTP response from Web server?

我想知道如何缓存来自 Web 服务器的 okHTTP 响应(returns json 数据)?

我希望我的应用程序下载 RecycleView 所需的所有数据,并在用户 运行 第一次使用该应用程序时缓存它 - 避免 re-downloading 并从 Web 服务器解析所有相同的数据,如果数据没有改变。

我试图获得响应 headers,但这是我得到的:

Request URL: https://somedomain.com/wp-json/?categories=3&per_page=100&status=publish
Request Method: GET
Status Code: 200 OK
Remote Address: 176.28.12.139:443
Referrer Policy: no-referrer-when-downgrade
Access-Control-Allow-Headers: Authorization, Content-Type
Access-Control-Expose-Headers: X-WP-Total, X-WP-TotalPages
Allow: GET
Connection: keep-alive
Content-Encoding: gzip
Content-Type: application/json; charset=UTF-8
Date: Fri, 23 Mar 2018 15:41:23 GMT
Link: <https://somedomain.com/wp-json/>; rel="https://api.w.org/"
Server: nginx
Transfer-Encoding: chunked
Vary: Accept-Encoding
X-Content-Type-Options: nosniff
X-Powered-By: PleskLin
X-Powered-By: PHP/7.1.15
X-Robots-Tag: noindex
X-WP-Total: 43
X-WP-TotalPages: 1
Accept: application/json, text/plain, */*
Accept-Encoding: gzip, deflate, br
Accept-Language: hr,en-US;q=0.9,en;q=0.8,de;q=0.7,nb;q=0.6
Connection: keep-alive
Host: somedomain.com
Referer: https://somedomain.com/somedir/
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36
categories: 3
per_page: 100
status: publish

我读到我可以从服务器响应中找到 ETAG 或 Last-Modified 以检查是否有任何更改,但如您所见,没有这样的东西。

如果应用程序只需要在第一个 运行 下载数据,之后才需要数据发生变化,你知道该怎么办吗?

只需将缓存添加到您的 OkHttp 客户端。 首先,您需要创建缓存:

int cacheSize = 10 * 1024 * 1024;
Cache cache = new Cache(cacheDirectory, cacheSize);

请注意,使用此代码,您将提供大小限制为 10MB 的缓存。在此之后,您将使用以下代码创建您的客户端:

OkHttpClient client = new OkHttpClient.Builder()
    .cache(cache)
    .build();

客户端将接受相关的缓存 headers。您可以参考官方文档以获取更多详细信息: https://github.com/square/okhttp/wiki/Recipes

你需要这样的缓存拦截器:

public class CacheInterceptor implements Interceptor {
    @Override
    public Response intercept(Chain chain) throws IOException {
        Response response = chain.proceed(chain.request());

        CacheControl cacheControl = new CacheControl.Builder()
                .maxAge(15, TimeUnit.MINUTES) // 15 minutes cache
                .build();

        return response.newBuilder()
                .removeHeader("Pragma")
                .removeHeader("Cache-Control")
                .header("Cache-Control", cacheControl.toString())
                .build();
    }
}

将带有 Cache 的拦截器添加到您的 OkHttpClient,如下所示:

File httpCacheDirectory = new File(applicationContext.getCacheDir(), "http-cache");
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(httpCacheDirectory, cacheSize);
OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addNetworkInterceptor(new CacheInterceptor())
            .cache(cache)
            .build();