I\'m implementing a cache for Objects stored persistently. The idea is:
getObjectFromPersistence(long id); ///Takes about 3 seconds
A WeakReference
is added to its ReferenceQueue supplied at the construction time when its reference is collected.
You could poll
the ReferenceQueue
whenever you access the cache, and hold a HashMap
to know which entry to remove if a reference is found in the queue.
Alternatively, if the cache is not frequently used, you can watch the queue in a separate thread.