Reputation: 2011
I have a data structure where I have objects, and they have targets. and they have a many to many relationship. For eg: object1 has targets: t1, t2, t3 and object2 has targets: t2, t3.
I want to write a Guava LoadingCache in order to store each of these objects by target. Now, the problem with this is: Objects 1 and 2 both will be stored under t2, and t3. And this is wastage.
So, one solution I thought of was to have another map of ids. And in the target cache, I'll store the object by their id.
Now, the question is: Since in a LoadingCache we dont have methods exposed that take care of clearing the cache(or a way to listen to when the cache gets cleared.), I have no way of keeping the id map upto date, when the cache actually gets updated.
Is there a good way of keeping these two maps in sync?
Upvotes: 0
Views: 612
Reputation: 198014
I question your premises, specifically that "this is wastage." You're storing multiple references to objects, not multiple copies of the objects themselves, and references are cheap. Having another map of ids is likely to be strictly more expensive than your original approach in the first place.
Upvotes: 3