Using Picasso with Custom Disk Cache

Using Picasso with custom disk cache

Picasso doesn't have a disk cache. It delegates to whatever HTTP client you are using for that functionality (relying on HTTP cache semantics for cache control). Because of this, the behavior you seek comes for free.

The underlying HTTP client will only download an image over the network if one does not exist in its local cache (and that image isn't expired).

That said, you can create custom cache implementation for java.net.HttpUrlConnection (via ResponseCache or OkHttp (via ResponseCache or OkResponseCache) which stores files in the format you desire. I would strongly advise against this, however.

Let Picasso and the HTTP client do the work for you!

You can call setIndicatorsEnabled(true) on the Picasso instance to see an indicator from where images are being loaded. It looks like this:

Sample Image

If you never see a blue indicator, it's likely that your remote images do not include proper cache headers to enable caching to disk.

How to implement my own disk cache with picasso library - Android?

Picasso uses the HTTP client for disk caching and if one is already configured it will use that instead of installing its own.

For the built-in UrlConnection the docs for installing a cache are here: https://developer.android.com/reference/android/net/http/HttpResponseCache.html

If you are using OkHttp then you just call setCache:
http://square.github.io/okhttp/2.x/okhttp/com/squareup/okhttp/OkHttpClient.html#setCache-com.squareup.okhttp.Cache-

Picasso doesn't cache image on disk

Ah since this is happening when you change headers, you are most probably not setting the Cache-Control header

According to Jake wharton (One of the developer of Picasso)

Picasso doesn't have a disk cache. It delegates to whatever HTTP
client you are using for that functionality (relying on HTTP cache
semantics for cache control). Because of this, the behavior you seek
comes for free

Taken from Jake Wharton's answer here

Also,

If you never see a blue indicator, it's likely that your remote images
do not include proper cache headers to enable caching to disk

Picasso - keeping images on disk

You can iterate the elements in OkHttp's disk cache, and call Iterator.remove() to get rid of the ones you don't want.

http://square.github.io/okhttp/javadoc/com/squareup/okhttp/Cache.html#urls--



Related Topics



Leave a reply



Submit