Invalidate cache in Picasso

64,034

Solution 1

In the recent versions of Picasso, there is a new method for invalidate, without any workarounds, so I think that custom PicassoTools class mentioned earlier, is now obsolete in this case

Picasso.with(getActivity()).invalidate(file);

Solution 2

Actually, based on your own answer, there is an easier way to do it without forking the library. Add this class to the com.squareup.picasso package.

package com.squareup.picasso;

public class PicassoTools {

    public static void clearCache (Picasso p) {
        p.cache.clear();
    }
}

Because cache has package visibility, this util class can clear the cache for you. You just have to call it:

PicassoTools.clearCache(Picasso.with(context));

Solution 3

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

Solution 4

Try to use:

Picasso.with(ctx).load(new File("/path/to/image")).skipMemoryCache().into(imageView)

Solution 5

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.

Share:
64,034

Related videos on Youtube

Maarten
Author by

Maarten

~ kissu kissu ~

Updated on July 05, 2022

Comments

  • Maarten
    Maarten almost 2 years

    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?

    • tux-world
      tux-world about 7 years
      after Picasso.with(getActivity()).invalidate(file); how can i cache again?
  • Maarten
    Maarten over 10 years
    This works, however I would like the image to store the image to cache, just not read from it if we know the image is stale! I've found out it's not possible to do that in Picasso as-is, so I'll mark your answer as correct.
  • TheLettuceMaster
    TheLettuceMaster about 10 years
    How do you access this class from the jar file?
  • shalafi
    shalafi about 10 years
    You don't have to access the jar. you have to create this file and place it in the right package (com.squareup.picasso)
  • TheLettuceMaster
    TheLettuceMaster about 10 years
    Oh, I see. Makes perfect sense. Thanks.
  • TheLettuceMaster
    TheLettuceMaster almost 10 years
    Is there a way to do this if you are using Gradle with Android Studio instead of the add-on jar file?
  • virendrao
    virendrao over 9 years
    we need to create this file inside picasso jar ?
  • shalafi
    shalafi over 9 years
    No you don't need to touch the Picasso jar, you just need to create the file in the right package.
  • russellhoff
    russellhoff over 9 years
    I wanted to refresh somehow a image which was previously loaded. skipMemoryCache() did the trick! Thanks!
  • shalafi
    shalafi over 9 years
    Well, that does invalidate a single file. There is still no method to invalidate all the cache. Anyway, I replied to that almost a year ago, so I'm not surprised they have added it since then.
  • mes
    mes over 9 years
    Yes, it doe's invalidate a single file, as author wanted, there's no need to clear entire cache, it's not effective. Did you try to implement it in Picasso yourself a year ago?
  • Jared Rummler
    Jared Rummler over 9 years
    Make sure you are on the latest version of Picasso or an IllegalStateException might be thrown. github.com/square/picasso/commit/…
  • Michael Schmidt
    Michael Schmidt about 9 years
    It is a pity, that Picasso.with(getApplication()).cache.clear();does not work. It would be much cleaner.
  • Can Uludağ
    Can Uludağ over 8 years
    Thanks! Adding .networkPolicy(NetworkPolicy.NO_CACHE) worked for me.
  • user5155835
    user5155835 over 8 years
    invalidate is not working, the image still remains in disk cache
  • madsongr
    madsongr over 8 years
    @Lawrence Kesteloot could you help me with this question? stackoverflow.com/questions/33657619/… . I have already used your method with great success but I´m having problems now. Thanks in advance
  • Joe Maher
    Joe Maher over 8 years
    You need to load the image with Picasso.with(imageView.getContext()).load(imageUrl).networkP‌​olicy(NetworkPolicy.‌​NO_CACHE).into(image‌​View); as invalidate() does not clear the networking cache
  • Iman Marashi
    Iman Marashi over 8 years
    By this answer images never cached! so why use it?
  • Iman Marashi
    Iman Marashi over 8 years
    The field Picasso.cache is not visible!!
  • shalafi
    shalafi over 8 years
    That is why you have to put that class into the specified package
  • Borja
    Borja about 8 years
    @ImanMarashi it's useful if you are usually working with same url and different image value (Example: amazonws.com/yourProject/user_profile_images/user1/profile.j‌​pg. The url never changes, but its value does.
  • Mohit Singh
    Mohit Singh almost 8 years
    Other answers just get rid of Memory Cache but not Disc Cache - this takes care of the disc too! :)
  • Sanket Berde
    Sanket Berde over 7 years
    @shalafi Picasso.with(getActivity()).cache.clear(); will clear all cache.
  • Rob
    Rob about 7 years
    This is deprecated now
  • tux-world
    tux-world about 7 years
    @mes .invalidate(url) can be cache after get again image from url?
  • usernotnull
    usernotnull almost 7 years
    @shalafi the field "cache" is not public anymore
  • shalafi
    shalafi almost 7 years
    @RJFares the field cache has always had package visibility, hence the need to put the Tools class in the correct package.
  • usernotnull
    usernotnull almost 7 years
    @shalafi i saw your answer now actually, the custom class. Can it be used to delete a specific file instead of the whole cache?
  • user924
    user924 over 6 years
    seems doesn't work, I tried to call this function and then opened my fragment with images and it showed images in imageviews instantly (from cache)... for the first time to load them it usually takes some time, but it's still instantly and memory using by app doesn't decrease after calling this function, so DOES NOT work
  • vmeyer
    vmeyer over 5 years
    Same recommandation from Picasso lib : github.com/square/picasso/issues/935