我有多个线程使用不同的参数调用相同的api。我要缓存它。在okhttp中使用缓存会导致以下错误。
java.io.IOException: failed to rename C:\Regression360\jiracache\journal to C:\Regression360\jiracache\journal.bkp
我相信它是okhttp中的一个已知错误。
我为客户提供的代码为
private int cacheSize = 10 * 1024 * 1024;
private String cacheFileName = "C:\\Regression360";
\*not relevant parts skipped\*
OkHttpClient client = new OkHttpClient.Builder()
.cache(cache)
.addNetworkInterceptor(provideCacheInterceptor())
.sslSocketFactory(trustAllSslSocketFactory, (X509TrustManager) trustAllCerts[0])
.retryOnConnectionFailure(true).connectionSpecs(Collections.singletonList(spec)).build();
Interceptor provideCacheInterceptor () {
return new Interceptor() {
@Override
public Response intercept (Chain chain) throws IOException {
Response response = chain.proceed( chain.request() );
CacheControl cacheControl = new CacheControl.Builder()
.maxAge( 2, TimeUnit.MINUTES )
.build();
return response.newBuilder()
.header("Cache-Control", cacheControl.toString() )
.build();
}
};
}
这里有什么我想念的吗?是否可以缓存多线程调用?