Implementing LruCache with Objects

658 views Asked by At

I am looking into implementing offline mode for android but to get there I need to implement a caching system. I read and people suggest LruCache and saw an example for images here LruCache Example. Now i am not caching images, I would like to cache objects. So for example I have an object called "Customer" and customer has an Id and a Name, so as I understand it, i would add it to the cache as

Id being the key and the Name the value.

My question is what if I have another object called "Courses" I would need a separate cache for that correct?. Because if they are in the same cache there may be a Customer.Id that is the same as Courses.Id.

Or what would be the best way to implement this?. (first time implementing cache and also new to android).

1

There are 1 answers

0
telkins On BEST ANSWER

Generally you create a cache for objects of the same type. So you would have a cache for Customer and a cache for Courses. If you find that you need to group objects of different types into the same cache, then perhaps you can find a common ancestor that they both subclass to use as the cache type. For example, you could make a cache for Animal and then both Dog and Cat could go in. Or you could make it completely generic and just use Object. Be careful though, as you said, you would need to keep track of how the different id values for each object behave with other object types if you are using that as your key.