0

I'm using the LruCache to cache several bitmaps in my Android app. But because I load those bitmaps according to data from more than one datatable from the database. For example: Object A is stored in table A and has an image while object B is stored in table B and has another image.

The ID for object A and B might be the same because they are stored in different tables. Therefore I have also two (or more) LruCache instances which I think is not that good. So I want to store all images in just one LruCache but then I need a key that can handle multiple occurences of the same ID. My guess is writing a key class. Any best practise for that? What about the hash-key?

private LruCache<MyKey, Bitmap> cache;

class MyKey {
    private final String className;
    private final int key;

    public MyKey(Object object, int key) {
        className = object.getClass().getCanonicalName();
        this.key = key;
    }

    @Override
    public int hashCode() {

        // TODO How to implement?

        return super.hashCode();
    }
}
Matthias
  • 5,574
  • 8
  • 61
  • 121
  • How about using TABLE_NAME_TAG + ID as key. Using LruCache is best. Define TABLE_NAME_TAG as you like and unique – BlackBeard May 26 '14 at 12:18
  • Yeah, that's what I currently do. Works too. – Matthias May 26 '14 at 12:37
  • Just an advice - Just to get a unique key dont complicate the flow. In a big picture you are playing against performance. DB as it is will affect speed and may be memory too. So look for optimization towards DB operations – BlackBeard May 26 '14 at 12:43
  • See http://stackoverflow.com/questions/27581/overriding-equals-and-hashcode-in-java – matiash May 26 '14 at 12:49

1 Answers1

0

Using a composite String as key is a solution that works and has low overhead.

TABLE_NAME_TAG + ID as key

Matthias
  • 5,574
  • 8
  • 61
  • 121