When does Picasso refresh image cache

久未见 提交于 2019-12-24 03:03:21

问题


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



来源:https://stackoverflow.com/questions/49651666/when-does-picasso-refresh-image-cache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!