android – 使用改造缓存特定响应

android – 使用改造缓存特定响应,第1张

概述我正在使用retrofit2来使用缓存拦截器兑现响应,但我想缓存特定请求而不是所有请求,所以我执行以下 *** 作. 我将我的api定义为: public interface ExampleApi {@GET("home/false")@Headers("MyCacheControl: public, max-age=60000")Observable<ExampleModel> getDataWi 我正在使用retrofit2来使用缓存拦截器兑现响应,但我想缓存特定请求而不是所有请求,所以我执行以下 *** 作.
我将我的API定义为:

public interface ExampleAPI {@GET("home/false")@headers("MyCacheControl: public,max-age=60000")Observable<ExampleModel> getDataWithCache();@GET("hello")@headers("MyCacheControl: no-cache")Observable<ExampleModel> getDataWithoutCache(); }

然后我的拦截器是:

public class OfflineResponseInterceptor implements Interceptor {// tolerate 4-weeks staleprivate static final int MAX_STALE = 60 * 60 * 24 * 28;private final Context context;public OfflineResponseInterceptor(Context context) {    this.context = context;}@OverrIDepublic Response intercept(Chain chain) throws IOException {    Request request = chain.request();    if (!NetworkUtil.isConnected(context)) {        request = request.newBuilder()                .removeheader("Pragma")                .header("Cache-Control","public,only-if-cached,max-stale=" + MAX_STALE)                .build();    }    return chain.proceed(request);   }}

public class OnlineResponseInterceptor implements Interceptor {@OverrIDepublic Response intercept(Chain chain) throws IOException {    Request request = chain.request();    String cacheControl = request.header("MyCacheControl");    Logger.deBUG("okhttp MyCacheControl ",cacheControl + "  ");    okhttp3.Response originalResponse = chain.proceed(request);    return originalResponse.newBuilder()            .removeheader("Pragma")            .header("Cache-Control",cacheControl)            .build();     } }

它工作正常,但我想知道实现我想要的最佳方式.还有另一种方法来识别我的请求,并区分他们除了标题.

解决方法 最后,感谢 andre tietz,他通过自定义的annoutations提出了另一个解决方案. 总结

以上是内存溢出为你收集整理的android – 使用改造缓存特定响应全部内容,希望文章能够帮你解决android – 使用改造缓存特定响应所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1124030.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-30
下一篇 2022-05-30

发表评论

登录后才能评论

评论列表(0条)

保存