LRUCache

class LRUCache<K, V> @JvmOverloads constructor(    softCapacity: Int,     strongCapacity: Int,     transformer: (K) -> V,     retirementAction: (K, V) -> Unit? = null)

LRUCache implements a memory-sensitive least-recently-used cache. All public operations support concurrent access. It avoids redundant simultaneous computation of values by racing threads that present the same keys.

Author

Todd L Smith

Parameters

K

The type of the keys.

V

The type of the values.

softCapacity

The capacity of the cache. This is the maximum number of cached values that will ever be retained simultaneously. Must be greater than zero (0).

strongCapacity

The maximum number of cached values that will be strongly retained to prevent garbage collection. Must be less than or equal to the capacity.

transformer

The function responsible for producing new values from user-supplied keys. Must not produce null.

retirementAction

The action responsible for retiring a binding expired from the cache, or null if no such action should be performed.

Constructors

Link copied to clipboard
fun <K, V> LRUCache(    softCapacity: Int,     strongCapacity: Int,     transformer: (K) -> V,     retirementAction: (K, V) -> Unit? = null)

Construct a new LRUCache.

Types

Link copied to clipboard
object Companion

Functions

Link copied to clipboard
fun clear()

Completely clear the cache, forcing retirement of the contents before removing them.

Link copied to clipboard
operator fun get(key: K): V

Answer the value associated with the specified key, computing the value from the user-supplied Function if the value is not already present in the cache.

Link copied to clipboard
fun poll(key: K): V?

Immediately answer the value already associated with the specified key. Do not execute the user-supplied transformer under any circumstances. That is, only answer an already cached value.

Link copied to clipboard
fun remove(key: K): V?

Remove the specified key and any value associated with it. If the key is present, and the softly held corresponding value has not been reclaimed by the garbage collector, then perform the retirement action, if any.

Properties

Link copied to clipboard
val size: Int

The number of values currently in cache.