Android memory and disk cache http client using Picasso - android

I am using Picasso to load images in a container. I want to cache in memory and cache in disk.
What is the best way to cache a image in disk? What http client is the best option?
These are my requirements:
First check cache in memory
Check cache in disk
Get the image from the server.
If the image is on disk -> check cache interval(max-age) and validate with server if the image is stale. The server needs in the header if_none_match: eTag.
I've read that this feature is built in the OkHttp api, but I don't know how to use it. Please I would appreciate somebody to send me some link, documentation or if any other different way of doing this that you could consider.

Related

SharedPreferences/Cache using retrofit

I have an app where I take data from API (some points on map with properties like descriptions, lan, lat, and list of photos) because of offline mode. I am not sure if I should use sharedPreferences or some okHttp cache (or some ORM database). SharedPref is good for small values, not for list of objects. Do you have suggestions/best practices?
Thanks
Store your data in db with image URIs. Store Images in memory cache and retrieve them from their URI. Retrofit doesn't comes with support for image loading from network by itself. If you don't want to go into depth of all this, you can use Glide or Picasso.
Picasso saves full image and can be resized at the time of loading. Glide caches images after resizing. See what fits your case.
Storing and retreiving images directly from database will require too much processing and slow down loading of images especially if you need large images. For more information read check out developers note on Caching Bitmaps and Display Bitmaps Efficiently.
I would suggest you to use response cache if you require the response only to display. Retrofit provides a nice and handy response caching method, you can make use of Interceptors to cache response. hope you are using retrofit latest version 2.1.0. check out this link to get more.
If you wanted to perform some operations like marking favourites etc. you can go for database.

Does Picasso cache only original downloaded image's size?

I read something about this issues but I didnt understand as well:
1: if I .resize(x,y) an image in my code, then Picasso caches only the original size or the resized one too?
2: memory and disk cache are storing with different cache-key?
Yes and no.
Picasso caches resized images. If you call resize(50, 50) on a URL twice the second request will use the resized image which was cached in memory. The HTTP client will never see the URL a second time in this case.
The HTTP client that Picasso uses will cache the original image (if configured to do so and the headers allow it). If you call resize(50, 50) on a URL the original will be cached on disk by the HTTP client so that if you call resize(100, 100) on that URL the HTTP client does not need to fetch the image again.
The HTTP client cache is completely opaque to Picasso. It doesn't see or control its contents. Picasso makes an HTTP request for every URL that isn't available in the memory cache and whether the HTTP client downloads it or serves it from the local cache is completely controlled inside the HTTP client.
1 - Yes, picasso only caches the original size.
2 - Yes.
The global default Picasso instance returned from with() is automatically initialized with defaults that are suitable to most implementations.
LRU memory cache of 15% the available application RAM
Disk cache of 2% storage space up to 50MB but no less than 5MB.
More information about caching in Picasso.
How do I use disk caching in Picasso?
Comparission with glide (also have infomation about caching)
http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

Saving and retrieving images from internal or external memory in android

I want to save the images fetched from server for once and from next time i want to check first whether images are stored or not in device, if not then again it should fetch from server and store in user's device again, and if yes then application will use images directly rather than fetching from server again and again. It will be useful for enhancing the speed of application. Basically my application is fetching multiple images from server so i want to save those images on user's android device and from next time application should fetch from device. I think you got my question.
The simple way:
You can use Picasso.
It is a simple lib which provides image downloading and caching.
In my opinion it might not be the fastest, but it is pretty simple and intuitive. It does its job well and none who I asked complained about it.
Picasso
Other libs:
UIL
Volley
Glide
fresco
To make it short. There are lots of other libs. An awesome comparision of the most Populat ones can be found here and here
The do it yourself way:
You can also write you own caching logic with a LRUCache. Which is also pretty simple.
Take a look at:
https://developer.android.com/topic/performance/graphics/cache-bitmap.html
The LRUCache is just a Memory Cache so you might also want to use a DiskLRUCache

Can Volley(Google IO 2013) have level1 LruBitmapImageCache and level2 DiskCache?

Can Android Volley (Google IO 2013) have a Bitmap cache for level one of cache and also a disk cache for level2. I am not clear on if this is an or choice or an either/or choice. Also wondering about performance of Disk cache vs Bitmap cache for images. I notice that the ImageLoader seems to take either a disk cache or a bitmap cache, but I have also read somewhere about it having a level1 and level2 caching ...
Volley, by default, caches everything on disk (L2) based http headers. If there are no cache or TTL headers available, no disk caching will occure.
You asked a question regarding caching which has an answer that'll help you understand here.
About the Bitmap Cache. In fact, the ImageLoader class expects an implementation of the interface ImageCache which should be a memory cache (L1). See this question.
Out of the box Volley has only disk cache (DiskBasedCache class), but you can provide yours (implement com.android.volley.Cache interface). There is no such term as "Bitmap cache" in Volley. All the data (bitmaps, texts, etc.) is cached on disk by default.
I know it is an old question, but I had the same problem and took me couple of days reading blogs, watching videos and scrolling up and down in Volley source code to finally figure it out. So for the future reference here is the thing in the clearest way to explain:
Volley caches EVERY response unless the response explicitly says "no-cache" or "no-store" in its header. if so, it won't be cached otherwise it will be cached according to "max-age" of the response header.
The caching system above makes sense 100% since a response wont be valid if server says so that makes you capable of setting expiration date for each response from server side (It is awesome).
if you don't like the above caching method you can override the corresponding part of code in volley source code, but it is highly NOT-recommended
All mentioned above is cached on Disk using LRU algorithm which makes it L2. so volley has built in L2 caching for EVERY request (including images)
Disk cache is blocking I/O. so lets say user swipes your ListView very fast and images should load very quickly, but the I/O blocks your main thread and you see some annoying jumping while scrolling. Then a non-blocking (in-memory) L1 cache comes handy and as you can see it is just useful when dealing with images. OK then, there is no built in L1 cache in Volley but IF you want to use the ImageLoader you can code your own and bind it to the ImageLoader. (Don't bother, thousands are already available under LruBitmapCache name, just copy one)
Conclusion: Set your server to send proper caching data for your responses (which is very easy) and define an LruBitmapCache for your ImageLoader in Volley and all is done. You will have L1 and L2 caches. if L1 failes volley checks L2 (disk) and if fails again it goes for the RPC.
Hope it helps

Standard method for caching data in Android?

I'm a bit confused about how to cache data in Android. I've seen many people implementing their own cache (eg, in droidfu project), but Android seems to have its own caching system with ResponseCache.
Is there any reason for not using Android cache?
What's the standard way to cache URLConnection response (text, data, json...), and where can I found examples?
Thanks
You have no control over the size of the cache or when objects get cleared (to my knowledge)
You cannot set the cache to the SD card
For small web-service requests the ResponseCache will suffice, and is the standard way of caching the raw response. It is only really when dealing with larger objects that you will need your own cache.
Alternatively you could
Serialize the data and save it to a local file
For basic data you could save it in SharedPreferences if it is long-lived

Categories

Resources