Picasso Invalid Cache

I load the image from the Picasso disk, for example Picasso.with(ctx).load(new File("/path/to/image")).into(imageView) , but whenever I save a new image in this file and update my ImageView , Picasso still has cache memory.

Is it possible to cancel the cache in Picasso?

+62
android caching picasso
Feb 25 '14 at
source share
14 answers

In recent versions of Picasso, a new method for invalidation has appeared without any workarounds, so I think the custom PicassoTools class mentioned earlier is now deprecated in this case

 Picasso.with(getActivity()).invalidate(file); 
+74
Feb 15 '15 at 9:09
source share

In fact, based on your own answer, there is an easier way to do this without opening 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(); } } 

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

 PicassoTools.clearCache(Picasso.with(context)); 
+71
May 8 '14 at 14:25
source share

Cancel the cache cache and check the disk cache by specifying a memory policy by the flag: emoryPolicy.NO_CACHE and NetworkPolicy.NO_CACHE, as shown below:

  mPicasso.with(mContext) .load(url) .memoryPolicy(MemoryPolicy.NO_CACHE ) .networkPolicy(NetworkPolicy.NO_CACHE) .resize(512, 512) .error(R.drawable.login) .noFade() .into(imageView); 
+55
March 10. '15 at 12:52
source share

Try using:

 Picasso.with(ctx).load(new File("/path/to/image")).skipMemoryCache().into(imageView) 
+14
Feb 27 '14 at 18:03
source share

Another option is to delete the cache directory itself, for example, when starting the application:

 deleteDirectoryTree(context.getCacheDir()); 

Where:

 /** * Deletes a directory tree recursively. */ public static void deleteDirectoryTree(File fileOrDirectory) { if (fileOrDirectory.isDirectory()) { for (File child : fileOrDirectory.listFiles()) { deleteDirectoryTree(child); } } fileOrDirectory.delete(); } 

This removes the entire cache directory, which is great if you want to simulate the first use of your application. If you want to delete the Picasso cache, add "picasso-cache" to the path.

+10
Jul 31 '14 at 18:50
source share

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

Here is an example:

 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(); 
+8
Mar 20 '15 at 18:42
source share

Picasso Image Search Order: Cache → Disk Cache → Network

So, there are several scenarios that we need for Picasso's cache invalidation:

1. Invalid memory cache:

  • Usercase: when the image is already being updated in the disk cache or remote host
  • Solution: flush url, file, uri cache if exists

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

.

2. Make sure the memory cache and disk cache .

※ note: Online means updating directly in ImageView

  • User case: image updated on the remote host

  • Solution: Interrupting the image in the cache and disk cache then requests the image on the remote host

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

    -> Discard cache memory and disk cache

.

3. Make sure the cache cache and offline disk cache

※ note: Offline means the update does not upgrade to ImageView, just extracting the background for later use

  • User case: you know that the image on the remote host is updated, but only want to update the cache only after use (do not update the image)
  • Solution: select only

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

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

4. Make sure the cache cache and disk cache is offline, if there is a disk cache

  • User case: only invalid cache if it already exists in disk cache
  • Decision. You should check the drive using the parameter: NetworkPolicy.OFFLINE cache before ejecting

      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 library, I hope the square will add more convenience to the cache management API in the future.

+8
Oct 12 '16 at 8:51
source share

You can clear the picasso image cache by setting your own cache and clearing it. This code has been tested on Picasso 2.5.0

 private Picasso picasso; private LruCache picassoLruCache; picassoLruCache = new LruCache(context); // Set cache picasso = new Picasso.Builder(context) // .memoryCache(picassoLruCache) // .build(); // Clear cache picassoLruCache.clear(); 
+7
Dec 14 '15 at 8:36
source share

Not too pretty, but this approach fixed my cache and Picasso problem. Use this only if you want to invalidate the cache for a specific URL, this approach is slow and probably the wrong way, but works for me.

  String url = "http://www.blablabla.com/Raiders.jpg"; Picasso.with(this).invalidate(url); Picasso.with(this) .load(url) .networkPolicy( NetworkUtils.isConnected(this) ? NetworkPolicy.NO_CACHE : NetworkPolicy.OFFLINE) .resize(200, 200) .centerCrop() .placeholder(R.mipmap.ic_avatar) .error(R.mipmap.ic_avatar) .into(imageView); 
+6
Apr 23 '15 at 2:46
source share

Another option is to save the new image in a different file than the original. Since the Picasso cache is disconnected from the file path, loading a new image from another file will result in a cache miss. This also has the advantage that you do not need to clear the entire cache.

0
Feb 14 '15 at 17:50
source share

use shutdown() instead of According to the source code; shutdown will stop accepting further request, as well as clear the entire 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 cannot close the singleton instance. Therefore, you need to have an instance variable for Picasso . Remember to reinitialize the picasso instance every time you shutdown() it to reuse it

0
Dec 15 '15 at 13:59 on
source share
 File f = new File(path, name); Picasso.with(this).invalidate(Uri.fromFile(f)); 
0
Jul 06 '16 at 9:22
source share

There is a very important part to the accepted answer here. I found a trick from this: http://blogs.candoerz.com/question/124660/android-image-cache-is-not-clearing-in-picasso.aspx

Just calling the next line will not clear the photo cache when you use custom settings such as resizing, center cropping, etc. when displaying the original image. Picasso.with (getContext ()) is invalid (file).

Decision:

When displaying an image, use the stableKey () parameter.

Picasso.with (getContext ()). load (new file (fileUri)). skipMemoryCache (). placeholder (R.drawable.placeholder) .stableKey (fileUri) .into (imageview);

Then you can clear the cache of this file later by calling this: Picasso.with (getContext ()) is invalid (fileUri) ;.

Hope this helps.

0
Sep 28 '16 at 19:34
source share

You can skip skipMemoryCache() memory cache

see next

  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"

0
Mar 22 '17 at 5:48
source share



All Articles