How Picasso Actually Cache the Images

南楼画角 提交于 2019-11-30 05:13:45

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.

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.

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