Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement caching in android app for REST API results?

My android app gets its data using REST API. I want to have client side caching implemented. Do we have any inbuilt classes for this?

if not, is these any code that i can reuse? I remember coming across such code sometime back. However I cant find it.

If nothing else works, i will write my own. following is basic structure

public class MyCacheManager {

static Map<String, Object> mycache;

public static Object getData(String cacheid) {
    return mycache.get(cacheid);
}

public static void putData(String cacheid, Object obj, int time) {
    mycache.put(cacheid, obj);
}

}

how do i enable time for cached objects? also - whats the best way to serialize? cache should be intact even if app is closed and reopened later (if time has not expired).

Thanks Ajay

like image 968
Thoughtful Monkey Avatar asked Oct 05 '12 12:10

Thoughtful Monkey


People also ask

How does REST API implement caching?

Caching in REST APIs POST requests are not cacheable by default but can be made cacheable if either an Expires header or a Cache-Control header with a directive, to explicitly allows caching, is added to the response. Responses to PUT and DELETE requests are not cacheable at all.

Can we cache in REST API?

Caching stores and retrieves data from a software or hardware component. When a client (such as a browser) makes a REST API request, it can save the API response in a cache. The next time the client initiates that request, it gets a faster response because the server doesn't have to process it all over again.


2 Answers

Now awesome library Volley released on Google I/O 2013 which helps for improve over all problems of calling REST API:

Volley is a library,it is library called Volley from the Android dev team. that makes networking for Android apps easier and most importantly, faster. It manages the processing and caching of network requests and it saves developers valuable time from writing the same network call/cache code again and again. And one more benefit of having less code is less number of bugs and that’s all developers want and aim for.

Example for volley: technotalkative

like image 164
LOG_TAG Avatar answered Sep 23 '22 18:09

LOG_TAG


One of the best ways is to use Matthias Käppler's ignited librarys to make http requests that caches the responses in memory (weak reference) and on file. Its really configurable to do one or the other or both.

The library is located here : https://github.com/mttkay/ignition with examples located here : https://github.com/mttkay/ignition/wiki/Sample-applications

Personally, I love this lib from when it was called Droidfu

Hope this helps you as much as it did me Ajay!

like image 20
petey Avatar answered Sep 22 '22 18:09

petey