2

I use an LRU cache (LruCache class from android.util) in my Android app. which is generally working fine.

Now I have a special requirement for this LRU cache: I would like that some objects are never removed. Explaining the situation: I have an array of objects (named mymetadata objects) that should be never removed and I have a lot of other objects (named dynamicdata objects) that should be removed with the LRU rule. I would like to store mymetadata objects in the LRU cache, because the array of objects can also grow and using an LRU cache helps avoiding running out of memory.

Is there any trick to guarantee that mymetadata objects are never removed from the LRU cache? Or should I simply access an object from the array so that it is marked as last used?

user3146587
  • 4,250
  • 1
  • 16
  • 25
mcfly soft
  • 11,289
  • 26
  • 98
  • 202

1 Answers1

1

Is there any trick to guarantee that mymetadata is never removed from the LRU cache? Or should I simply access a object of the array and it is marked as last used?

Besides regularly touching the objects you want to keep in the LRU cache (to force raising their ranks), I don't see what else could be done. One problem with this might be when should these objects be touched and what is the performance impact of this operation?

A different approach would be to split the storage of your objects depending on their persistence. Keep a standard map for your persistent objects and an LRU cache for objects that can expire. This mix of two data structures can then be hidden behind a single interface similar to the ones of Map or LruCache (each query is directed to the right internal storage).

I would like to put the mymetadata objects into the LRU cache, because the array of objects can also grow

This seems to be in conflict with your "never removed" requirement for some object. How do you decide when a persistent object is allowed to expire?

Anyway, yet another approach would consist in reimplementing the LRU cache data structure, keeping two separate ordered lists of objects instead of a single one: one for mymetadata objects and one for dynamicdata objects. Each query to this data structure is then directed to the right list and both kind of objects can expire independently (the size of the cache can also be chosen independently for each set of objects). But both kind of objects are stored in the same hash table / map.

user3146587
  • 4,250
  • 1
  • 16
  • 25
  • Thanks a lot for the answer. Yes the performance is also q question. I checked the lrucache.get operation is very fast ( accessing the objects through hashcode ? ), so perhabs touching an object of the array will do the job. My objects in the array or "master data" but do also have lists of data ( which can a bit growth). But after growing, they should also never be removed. – mcfly soft Feb 08 '14 at 15:41