When does Picasso refresh image cache

2019-07-11 04:24发布

问题:

I am using picasso to fetch images from server.

This is what i am using.

            Picasso.with(getApplicationContext()).load(imageURL)
                    .placeholder(R.drawable.card_loading)
                    .fit().centerCrop()
                    .into(imageView);

The above code should cache the images, but when i update the same image on server, without changing its URL, it starts displaying the new image on app, whereas it should display the cached old image on the app.

In some devices it was displaying the older images, i closed and restart the app multiple times, then it started displaying the new images on those devices as well.

My Question is that how long picasso keep an image in cache, and how can i increase this from server or client

回答1:

I'm not sure how long cache file valid. But you can change cache file validity with incoming http response header. Basically you can create interceptor and add new header with "Cache-Control" name.

 OkHttpClient httpClient = new OkHttpClient();
    httpClient.networkInterceptors().add(new Interceptor(){

        @Override
        public Response intercept(Chain chain) throws IOException {
            Response originalResponse = chain.proceed(chain.request());
            return originalResponse.newBuilder().header("Cache-Control", "max-age=" + (60 * 60 * 24 * 365)).build();
        }
    });

After that you can pass it to picasso as a http client