I have a class which extends LinkedHashMap
which I'm using as a cache. This cache has a property which defines the maximum size in bytes that the cache is allowed to store. I use a rough estimate for the size of the objects that I store.
I override put
, so that I update the total size that the cache is currently storing.
I also override remove
to subtract the size of the object I've removed from the total.
At the moment I'm checking whether or not I need to remove stale entries from the cache to free up space on adding a new value in the put
method, however I would like to move this functionality to an implementation of the removeEldestEntry
method.
The problem I've encountered is that it seems if my overridden removeEldestEntry
returns true
, my overridden remove
method is not called. This leads to a problem where my current stored size variable is not updated when the removeEldestEntry
removes stale entries.
Does anyone know what code path is taken to remove the eldest entry when removeEldestEntry
returns true
. Is it possible for me to override the method that this uses to remove entries?
Is it possible for me to do my update calculation when LinkedHashMap
triggers a removal on removeEldestEntry
.
I know the api allows you to do the actual removal in the removeEldestEntry
method as long as you return false afterwards, however I want to explore other options before doing this. I view this as a last case scenario.