suparnatural-cache / com.suparnatural.core.cache / CacheReplacementPolicy


interface CacheReplacementPolicy

A Cache Replacement policy maintains a registry of cached objects to free space when cache is full. The object removed when cache fills up depends on the implementation of CacheReplacementPolicy.evictUnsafe method.

For example, FifoCacheReplacementPolicy replaces the first cached object when cache runs out of space. Therefore, its FifoCacheReplacementPolicy.evictUnsafe removes the oldest object from the registry and returns it.

All methods are assumed to be thread unsafe and expect that the caller has already acquired the locks.

A custom replacement policy (Like LRU) can be implemented as long as it conforms to this interface.


A custom cache replacement policy

class LRUCacheReplacementPolicy(override val cacheSize: Int): CacheReplacementPolicy {



abstract val cacheSize: Int

Size of the cache



abstract fun clearRegistryUnsafe(): Unit

Clear any state and reset to initial. For example, clearRegistryUnsafe any linked list state for LRU or FIFO cache.


abstract fun evictUnsafe(): CacheableContainer?

Prune the cache by 1 entry. Returns the removed object. Call this method when cache runs out of space.


abstract fun registerObjectUnsafe(obj: CacheableContainer): CacheableContainer?

Adds a cache entry for the object and returns the entry of a pruned object. Pruning occurs when the cache space is full.


abstract fun unregisterObjectUnsafe(obj: CacheableContainer): Unit

Remove obj from cache registry.



class FifoCacheReplacementPolicy : CacheReplacementPolicy

A cache replacement policy which evicts the oldest object from cache to make space when cache is full.