LruCache

open class LruCache<K : Any, V : Any>(maxSize: Int)(source)

Static library version of android.util.LruCache. Used to write apps that run on API levels prior to 12. When running on API level 12 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.

Parameters

maxSize

for caches that do not override sizeOf, this is the maximum number of entries in the cache. For all other caches, this is the maximum sum of the sizes of the entries in this cache.

Constructors

Link copied to clipboard
fun LruCache(maxSize: Int)

Creates a new LruCache

Functions

Link copied to clipboard
protected open fun create(key: K): V?

Called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.

Link copied to clipboard

Returns the number of times create returned a value.

Link copied to clipboard
protected open fun entryRemoved(    evicted: Boolean,     key: K,     oldValue: V,     newValue: V?)

Called for entries that have been evicted or removed. This method is invoked when a value is evicted to make space, removed by a call to remove, or replaced by a call to put. The default implementation does nothing.

Link copied to clipboard
fun evictAll()

Clear the cache, calling entryRemoved on each removed entry.

Link copied to clipboard

Returns the number of values that have been evicted.

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

Returns the value for key if it exists in the cache or can be created by create. If a value was returned, it is moved to the head of the queue. This returns null if a value is not cached and cannot be created.

Link copied to clipboard
fun hitCount(): Int

Returns the number of times get returned a value that was already present in the cache.

Link copied to clipboard
fun maxSize(): Int

For caches that do not override sizeOf, this returns the maximum number of entries in the cache. For all other caches, this returns the maximum sum of the sizes of the entries in this cache.

Link copied to clipboard
fun missCount(): Int

Returns the number of times get returned null or required a new value to be created.

Link copied to clipboard
fun put(key: K, value: V): V?

Caches value for key. The value is moved to the head of the queue.

Link copied to clipboard
fun putCount(): Int

Returns the number of times put was called.

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

Removes the entry for key if it exists.

Link copied to clipboard
open fun resize(maxSize: Int)

Sets the size of the cache.

Link copied to clipboard
fun size(): Int

For caches that do not override sizeOf, this returns the number of entries in the cache. For all other caches, this returns the sum of the sizes of the entries in this cache.

Link copied to clipboard
protected open fun sizeOf(key: K, value: V): Int

Returns the size of the entry for key and value in user-defined units. The default implementation returns 1 so that size is the number of entries and max size is the maximum number of entries.

Link copied to clipboard

Returns a mutable copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.

Link copied to clipboard
open override fun toString(): String
Link copied to clipboard
open fun trimToSize(maxSize: Int)

Remove the eldest entries until the total of remaining entries is at or below the requested size.