We have the following requirements to a cache (java).
- Cache-entries have different priority regarding eviction - the following properties of an entry will be a factor in this priority
- When it was inserted or last used (LRU)
- The resources needed to recalculate the entry (when evicted). This factor will change, because from time to time we get an entry from the cache and "add information to it", making it require more resources to recalculate after eviction. On this parameter the eviction priority of entries is very discrete - do not have to support e.g. any possible long/double-value. Lets just say than any entry have a priority in the range of natural numbers 1-10 (only 10 possible eviction priority values)
Guess it could be done using a cache-implementation supporting eviction-policy plugin. EHCache seem to support that. Unfortunately Guava cache does not.
But I am afraid of performance and flexibility if the implementation is actually only using one inner cache that tries to find the entry with lowest stay-priority when it has to evict. If it is implemented in a way so that an entry registers (with the cache) its new priority, when its priority changes, and that the cache maintains a priority queue I would not be that afraid. Anyone know of a cache-implementation doing it this way? Anyone know what EHCache is actually doing?
It is also hard for us to actually calculate a combined priority from the two factors mentioned above. It is hard to calculate a fair balance between "how recently is was used" and "how resource-consuming it is to recalculate it".
Currently we have made our own cache-implementation having a list of inner caches (Guava caches). Each inner cache just uses LRU eviction strategy. When an entry changes on "how resource-consuming it is to recalculate it" it moves to "the next" inner cache. This way we do not have to calculate a combined eviction priority value, but can have different max-sizes etc on each inner cache. Actually we like the flexibility this gives, but we would prefer not to do and maintain the cache-implementation ourselves. We would rather use a cache-implementation from some open-source project. Anyone know an open-source cache-implementation that supports this multi-level-inner-cache functionality? Or maybe an open-source project that would like to adopt our implementation?