Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android LRUCache Retrieval

I have implemented a standard LRUCache in Android that stores Objects. Each key is a unique ObjectId associated with the Object stored. My problem is that the only way to retrieve an Object from cache is by the ObjectId (no iterator). What would be the best way to implement a getAll() method? Another option would be to store all the ObjectIds in a list somewhere, so I can iterate over the lists and get all of the Objects - but what would be the best way of holding all of the ObjectIds?

Thanks!

like image 726
Nelson.b.austin Avatar asked Dec 18 '13 21:12

Nelson.b.austin


2 Answers

If you're using (or extending) the LruCache that Android provides, it has a snapshot method that returns a map of keys (your ObjectIds) and values (your Objects). You can do something like this:

Map<ObjectIds, Object> snapshot = lruCache.snapshot();
for (ObjectIds id : snapshot.keySet()) {
  Object myObject = lruCache.get(id);
}

If you're not using Android's LruCache, then I imagine it would depend on your implementation. (I'd also be curious what motivated you to implement your own instead of subclassing the provided one!)

like image 124
Michiyo Avatar answered Oct 27 '22 16:10

Michiyo


Using snapshot to get current collection at the moment

lruCache.snapshot().values()
like image 44
rocketspacer Avatar answered Oct 27 '22 14:10

rocketspacer