Search code examples
androidpicassoimage-loading

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


Solution

  • 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.