I was going through Guava Cache examples and wondered is there a way to prevent some entries from getting evicted from the cache on some condition.
Like, if an entry is being used by an application, it's unlikely you would like it to get evicted!
One way, I could think of is using CacheBuilder.weigher()
method with size-based eviction, as Javadoc states:
When the weight of an entry is zero it will not be considered for size-based eviction (though it still may be evicted by other means).
But weights are calculated in the beginning and are static during the lifetime of a cache entry. So, when an entry is no longer being used by application its weight cannot be set to zero.
Is there any other efficient way to achieve this? Like, a method would be called by Guava Cache before evicting a specific entry whether it should be evicted or not!
Also, is it a bad idea to cache Presenter instances in an MVP application? As, I am going to use Guava Cache to cache Presenter instances in my application!