Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How Picasso Actually Cache the Images

I would like to know how exactly Picasso Library is caching the images inside the application. I know it used HttpHeaders to check weather to fetch images from network.

But, is there any time frame it is caching the images ?
Like invaliding the cache after a day or something ?

The problem is my project is loading huge number of small images from network. Some times, the new images got reflected in next launch. But, some times, it doesn't.

The worst part is, some images got reflected the changes while others are not even though the changes are made at the same time.
But, when I uninstall the App, all the images got reflected the changes (of course.)

There has to be something about caching in Picasso.

And please don't tell me to use OkHttp for controlling the cache in Picasso.
My project is using AsyncHttpClient from Apache and it is too damn big to update.
(not by me, of course. I would just create a small network helper with UrlConnection instead of implementing the whole AsyncHttpClient.)

Anyway, any idea or pointer would be appreciated.
Bottom Line : No OkHttp. Just want to know about cache controlling mechanism on Picasso.

Regards

like image 805
Aung Pyae Avatar asked Jul 25 '14 09:07

Aung Pyae


People also ask

Which is better glide or Picasso?

Glide is faster and the results of Picasso and Coil are similar. But what about when we are loading from the cache. As you can see in the images below we have the best times for Glide in most of the cases.


2 Answers

Picasso only has a memory cache.

If the image is in the memory cache it uses it. Otherwise, when the image is loaded from its remote source (network, content provider, filesystem, etc.) it is placed in the memory cache for future lookups.

The memory cache is an LRU so the more an image is used the more likely it will remain in the cache. Images that are not requested often will be evicted over time. There is no eviction for time and the memory cache does not honor the caching semantics of any HTTP headers (if the image was from the network).

Picasso does not have a disk cache. It relies on the HTTP client (whichever is being used) for 100% of this functionality. A cache will be installed for both OkHttp or HttpUrlConnection (if either is used) automatically or if one is already that will be used.

If you are using a custom HTTP client the burden of enabling the cache is on you the caller.

like image 98
Jake Wharton Avatar answered Oct 12 '22 06:10

Jake Wharton


As far as I know Picasso doesn't clear the cache by itself, therefore in our app we're triggering that "manually". The code to do it is this:

private static final String PICASSO_CACHE = "picasso-cache";

public static void clearCache(Context context) {
    final File cache = new File(
            context.getApplicationContext().getCacheDir(),
            PICASSO_CACHE);
    if (cache.exists()) {
        deleteFolder(cache);
    }
}

private static void deleteFolder(File fileOrDirectory) {
    if (fileOrDirectory.isDirectory()) {
        for (File child : fileOrDirectory.listFiles())
            deleteFolder(child);
    }
    fileOrDirectory.delete();
}

You can trigger this cleanup worker once a day/week, depending on what you need in your app.

like image 45
Ciprian Avatar answered Oct 12 '22 06:10

Ciprian