How Picasso Actually Cache the Images

て烟熏妆下的殇ゞ 提交于 2019-12-18 11:56:36

问题


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


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/24952627/how-picasso-actually-cache-the-images

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!