3
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 stale
private static final int MAX_STALE = 60 * 60 * 24 * 28;
private final Context context;
public OfflineResponseInterceptor(Context context) {
this.context = context;
}
@Override
public 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 {
@Override
public 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();
}
}
それが正常に動作しているが、私は何を達成するための最良の方法を知りたいですまた、私の要求を識別し、ヘッダー以外のものを区別する別の方法があります。