How Picasso actually caches images

I would like to know exactly how the Picasso library caches images inside the application. I know that he used HttpHeaders to check the weather to extract images from the network.

But is there a time frame for image caching?
Like an invalid cache every other day or something else?

The problem is that my project downloads a huge amount of small images from the network. Several times, new images are reflected in the next launch. But sometimes this is not so.

Worst of all, some images are reflected in the changes, while others even if the changes are made at the same time.
But when I uninstall the application, all the images received reflected the changes (of course.)

Picasso should be something like caching.

And please don't tell me to use OkHttp to manage the cache in Picasso.
My project uses AsyncHttpClient from Apache and it is too easy to upgrade.
(Not me, of course. I would just create a small network assistant with UrlConnection instead of implementing the entire AsyncHttpClient.)

In any case, any idea or pointer will be appreciated.
Bottom line: no OkHttp. I just want to know about the cache management mechanism on Picasso.

Hi

+8
android picasso
source share
2 answers

As far as I know, Picasso does not clear the cache on its own, so in our application we run it โ€œmanuallyโ€. Code for 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 run this cleaning person once a day / week, depending on what you need in your application.

+4
source share

Picasso only has a memory cache.

If the image is in the memory cache, it uses it. Otherwise, when the image is downloaded from its remote source (network, content provider, file system, etc.), it is placed in the memory cache for future searches.

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

Picasso has no disk cache. It uses an HTTP client (depending on what is used) for 100% of this function. The cache will be installed for both OkHttp and HttpUrlConnection (if used) automatically or if it is already in use.

If you are using a custom HTTP client, the burden of including the cache in you is the caller.

+14
source share

All Articles