Invalidate cache in Picasso

2019-01-01 03:25发布

I load an image from disk using Picasso, e.g., Picasso.with(ctx).load(new File("/path/to/image")).into(imageView), but whenever I save a new image in that file, and refresh my ImageView, Picasso still has the bitmap cached.

Is it possible to invalidate the cache in Picasso?

14条回答
素衣白纱
2楼-- · 2019-01-01 04:03

Another option is to save the new image into a different file than the original. Since the Picasso bitmap cache is keyed off of the file path, loading the new image from a different file will result in a cache miss. This also has the side benefit of not having to clear the entire cache.

查看更多
不再属于我。
3楼-- · 2019-01-01 04:04

You can skip memory cache by skipMemoryCache()

see the following

        Picasso.with(this)
            .load(IMAGE_URL)
            .skipMemoryCache()
            .placeholder(R.drawable.placeholder)
            .error(R.drawable.no_image)
            .into(mImageViewPicasso);

gradle compile "com.squareup.picasso:picasso:2.4.0"

查看更多
心情的温度
4楼-- · 2019-01-01 04:07

Abort memory cache and disk cache check by indicate memory policy by flag: emoryPolicy.NO_CACHE and NetworkPolicy.NO_CACHE as below code snippet:

   mPicasso.with(mContext)
            .load(url)
            .memoryPolicy(MemoryPolicy.NO_CACHE )
            .networkPolicy(NetworkPolicy.NO_CACHE)
            .resize(512, 512)
            .error(R.drawable.login)
            .noFade()
            .into(imageView);
查看更多
ら面具成の殇う
5楼-- · 2019-01-01 04:10

The order of search image in Picasso is: Memory cache -> Disk cache -> Network

So there are few scenario we need to invalidate cache in Picasso:

1.Invalidate memory cache:

  • Usercase: When image already update in disk cache or remote host
  • Solution: Clear cache of Url, File, Uri if exist

    mPicasso.with(appContext).invalidate(File);
    mPicasso.with(appContext).invalidate(Url);
    mPicasso.with(appContext).invalidate(Uri);
    

.

2.Invalidate memory cache and disk cache Online

※note: Online mean update directly to ImageView

  • User case: Image updated on remote host

  • Solution: Abort image on memory cache and disk cache then request image on remote host

    mPicasso.with(appContext)
        .load(url)
        .memoryPolicy(MemoryPolicy.NO_CACHE )
        .networkPolicy(NetworkPolicy.NO_CACHE)
        .into(imageView);
    

    -> Abort memory cache and disk cache

.

3.Invalidate memory cache and disk cache Offline

※ note: Offline mean update not update to ImageView, just background fetch to using later

  • User case: You know image on remote host updated, but only want to update cache only to using afterward (not update into image view)
  • Solution: fetch only

     mPicasso.with(appContext)
        .load(url)
        .memoryPolicy(MemoryPolicy.NO_CACHE)
        .networkPolicy(NetworkPolicy.NO_CACHE)
        .fetch();
    

※Note: Using fetch() is good but it also consume network resource, so please consider carefully, check scenario 4 in below for better solution

4.Invalidate memory cache and disk cache Offline if disk cache is exist

  • User case: Only invalidate cache if already exist in disk cache
  • Solution: Should check disk by using parameter: NetworkPolicy.OFFLINE cache before fetch

     mPicasso.with(appContext)
        .load(url)
        .memoryPolicy(MemoryPolicy.NO_CACHE)
        .networkPolicy(NetworkPolicy.OFFLINE)
        .fetch(new Callback() {
            @Override
            public void onSuccess() {
                //Success: mean disk cache exist -> should do actual fetch
                picasso.load(url).fetch();
            }
    
            @Override
            public void onError() {
            //Failed: mean disk cache not exist
        }
    });
    

Picasso is an amazing libs, I hope squareup will add more convenience API to manage cache in upcoming future.

查看更多
梦该遗忘
6楼-- · 2019-01-01 04:10

use shutdown() instead As per source code; shutdown will stop accepting further request as well as clear all cache

 /** Stops this instance from accepting further requests. */
  public void shutdown() {
    if (this == singleton) {
      throw new UnsupportedOperationException("Default singleton instance cannot be shutdown.");
    }
    if (shutdown) {
      return;
    }
    cache.clear();
    cleanupThread.shutdown();
    stats.shutdown();
    dispatcher.shutdown();
    for (DeferredRequestCreator deferredRequestCreator : targetToDeferredRequestCreator.values()) {
      deferredRequestCreator.cancel();
    }
    targetToDeferredRequestCreator.clear();
    shutdown = true;
  }

Also you can not shutdown singleton instance. So you need to have instance variable for Picasso. Do not forget to reinitialize picasso instance everytime you shutdown() it in order to reuse it

查看更多
无色无味的生活
7楼-- · 2019-01-01 04:12

What you can do if you want to delete all cache at once, is to create a custom Picasso.LruCache, and then use the clear method on it.

Here is a sample:

Picasso.Builder builder = new Picasso.Builder(this);
LruCache picassoCache = new LruCache(this);
builder.memoryCache(picassoCache);
Picasso.setSingletonInstance(builder.build());

To clear the cache:

picassoCache.clear();
查看更多
登录 后发表回答