使用Retrofit, okhttp和picasso缓存图像和字符串



我试图优化这段代码缓存的目的。此代码使缓存仅为1天之前,它重新连接到互联网,使新的缓存。我想让它60天之前再次访问网络,使新的缓存。此外,使用毕加索从缓存中取出的图像也会减慢速度毕加索:2.5.2retrofit2:改造:2.7.2retrofit2: converter-gson: 2.7.24.4.1 okhttp3: okhttp:4.4.1 okhttp3:日志记录拦截器:

if (retrofit==null) {
OkHttpClient okHttpClient = new OkHttpClient().newBuilder()
.addInterceptor( provideHttpLoggingInterceptor() )
.addInterceptor( provideOfflineCacheInterceptor() )
.addNetworkInterceptor( provideCacheInterceptor() )
.cache( provideCache() )
.connectTimeout(60, TimeUnit.SECONDS)
.readTimeout(60, TimeUnit.SECONDS)
.writeTimeout(60, TimeUnit.SECONDS)
.build();
OkHttp3Downloader okHttp3Downloader = new OkHttp3Downloader(okHttpClient);
Picasso picasso = new Picasso.Builder(MyApplication.getInstance())
.downloader(okHttp3Downloader)
.build();

Picasso.setSingletonInstance(picasso);
retrofit = new Retrofit.Builder()
.baseUrl(Global.API_URL)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
private static Cache provideCache ()
{
Cache cache = null;
try
{
cache = new Cache( new File(MyApplication.getInstance().getCacheDir(), "wallpaper-cache" ),
100 * 1024 * 1024 ); // 100 MB
}
catch (Exception e)
{
Timber.e( e, "Could not create Cache!" );
}
return cache;
}
private static HttpLoggingInterceptor provideHttpLoggingInterceptor ()
{
HttpLoggingInterceptor httpLoggingInterceptor =
new HttpLoggingInterceptor( new HttpLoggingInterceptor.Logger()
{
@Override
public void log (String message)
{
Timber.d( message );
Log.v("MYAPI",message);
}
} );
httpLoggingInterceptor.setLevel( BuildConfig.DEBUG ? HEADERS : NONE );
return httpLoggingInterceptor;
}
public static Interceptor provideCacheInterceptor ()
{
return new Interceptor()
{
@Override
public Response intercept (Chain chain) throws IOException
{
Response response = chain.proceed( chain.request() );
int maxAge = 60  * 60; // read from cache
return response.newBuilder()
.header( CACHE_CONTROL, "public, max-age=" + maxAge)
.removeHeader("Pragma")
.build();
}
};
}
public static Interceptor provideOfflineCacheInterceptor ()
{
return new Interceptor()
{
@Override
public Response intercept (Chain chain) throws IOException
{
Request request = chain.request();
if ( !MyApplication.hasNetwork() )
{
int maxStale = 60 * 60 * 24 * 28; // tolerate 4-weeks stale
request = request.newBuilder()
.header( CACHE_CONTROL,  "public, only-if-cached, max-stale=" + maxStale)
.removeHeader("Pragma")
.build();
}
return chain.proceed( request );
}
};
}
} ```


你可以在拦截器中重写缓存头,像下面的例子:如何缓存Web服务器的okHTTP响应

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();
}
}

最新更新