How Picasso Actually Cache the Images
Asked Answered
S

2

9

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

Supposed answered 25/7, 2014 at 9:39 Comment(0)
A
4

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.

Aleta answered 25/7, 2014 at 10:14 Comment(2)
This only works if you are using OkHttp or HttpUrlConnection which the post explicitly states he is not.Gristede
Fair enough, we're using OkHttp.Aleta
G
15

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.

Gristede answered 25/7, 2014 at 16:5 Comment(2)
Thanks Jake. It's quite explanatory itself. But, can you a bit more elaborate about "memory cache does not honour the caching semantics of any HTTP headers" ? And also about "A cache will be installed for both OkHttp & HttpUrlConneciton" ? Frankly, I don't wanna touch that AsyncHttpClient mechanism at all. And I see there is "Cache" interface in Picasso Source and LruCache is implementing that. Would it be wise to implement my own cache-mechanism by forking Picasso ? If I implement my own cache, can I put a trigger for clearing it by time frame like the way Ciprian mentioned in below ?Supposed
@JakeWharton, you rock, you may be interested in my "additional" question in braces at the end of .. stackoverflow.com/questions/25746356 It's a real mystery to me. Cheers!!!! thanks as always from your screaming fansMajormajordomo
A
4

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.

Aleta answered 25/7, 2014 at 10:14 Comment(2)
This only works if you are using OkHttp or HttpUrlConnection which the post explicitly states he is not.Gristede
Fair enough, we're using OkHttp.Aleta

© 2022 - 2024 — McMap. All rights reserved.