Added in API level 12

LruCache


open class LruCache<K : Any!, V : Any!>
kotlin.Any
   ↳ android.util.LruCache

A cache that holds strong references to a limited number of values. Each time a value is accessed, it is moved to the head of a queue. When a value is added to a full cache, the value at the end of that queue is evicted and may become eligible for garbage collection.

If your cached values hold resources that need to be explicitly released, override entryRemoved.

If a cache miss should be computed on demand for the corresponding keys, override create. This simplifies the calling code, allowing it to assume a value will always be returned, even when there's a cache miss.

By default, the cache size is measured in the number of entries. Override sizeOf to size the cache in different units. For example, this cache is limited to 4MiB of bitmaps:

<code>int cacheSize = 4 * 1024 * 1024; // 4MiB
    LruCache&lt;String, Bitmap&gt; bitmapCache = new LruCache&lt;String, Bitmap&gt;(cacheSize) {
        protected int sizeOf(String key, Bitmap value) {
            return value.getByteCount();
        }
    }</code>

This class is thread-safe. Perform multiple cache operations atomically by synchronizing on the cache:

<code>synchronized (cache) {
      if (cache.get(key) == null) {
          cache.put(key, value);
      }
    }</code>

This class does not allow null to be used as a key or value. A return value of null from get, put or remove is unambiguous: the key was not in the cache.

This class appeared in Android 3.1 (Honeycomb MR1); it's available as part of Android's Support Package for earlier releases.

Summary

Public constructors
LruCache(maxSize: Int)

Public methods
Int

Returns the number of times create(java.lang.Object) returned a value.

Unit

Clear the cache, calling entryRemoved on each removed entry.

Int

Returns the number of values that have been evicted.

V
get(key: K)

Returns the value for key if it exists in the cache or can be created by create.

Int

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

Int

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

Int

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

V
put(key: K, value: V)

Caches value for key.

Int

Returns the number of times put was called.

V
remove(key: K)

Removes the entry for key if it exists.

open Unit
resize(maxSize: Int)

Sets the size of the cache.

Int

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

MutableMap<K, V>!

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

String

open Unit
trimToSize(maxSize: Int)

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

Protected methods
open V
create(key: K)

Called after a cache miss to compute a value for the corresponding key.

open Unit
entryRemoved(evicted: Boolean, key: K, oldValue: V, newValue: V)

Called for entries that have been evicted or removed.

open Int
sizeOf(key: K, value: V)

Returns the size of the entry for key and value in user-defined units.

Public constructors

LruCache

Added in API level 12
LruCache(maxSize: Int)
Parameters
maxSize Int: 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.

Public methods

createCount

Added in API level 12
fun createCount(): Int

Returns the number of times create(java.lang.Object) returned a value.

evictAll

Added in API level 12
fun evictAll(): Unit

Clear the cache, calling entryRemoved on each removed entry.

evictionCount

Added in API level 12
fun evictionCount(): Int

Returns the number of values that have been evicted.

get

Added in API level 12
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.

hitCount

Added in API level 12
fun hitCount(): Int

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

maxSize

Added in API level 12
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.

missCount

Added in API level 12
fun missCount(): Int

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

put

Added in API level 12
fun put(
    key: K,
    value: V
): V

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

Return
V the previous value mapped by key.

putCount

Added in API level 12
fun putCount(): Int

Returns the number of times put was called.

remove

Added in API level 12
fun remove(key: K): V

Removes the entry for key if it exists.

Return
V the previous value mapped by key.

resize

Added in API level 21
open fun resize(maxSize: Int): Unit

Sets the size of the cache.

Parameters
maxSize Int: The new maximum size.

size

Added in API level 12
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.

snapshot

Added in API level 12
fun snapshot(): MutableMap<K, V>!

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

toString

Added in API level 12
fun toString(): String
Return
String a string representation of the object.

trimToSize

Added in API level 17
open fun trimToSize(maxSize: Int): Unit

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

Parameters
maxSize Int: the maximum size of the cache before returning. May be -1 to evict even 0-sized elements.

Protected methods

create

Added in API level 12
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.

The method is called without synchronization: other threads may access the cache while this method is executing.

If a value for key exists in the cache when this method returns, the created value will be released with entryRemoved and discarded. This can occur when multiple threads request the same key at the same time (causing multiple values to be created), or when one thread calls put while another is creating a value for the same key.

entryRemoved

Added in API level 12
protected open fun entryRemoved(
    evicted: Boolean,
    key: K,
    oldValue: V,
    newValue: V
): Unit

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.

The method is called without synchronization: other threads may access the cache while this method is executing.

Parameters
evicted Boolean: true if the entry is being removed to make space, false if the removal was caused by a put or remove.
newValue V: the new value for key, if it exists. If non-null, this removal was caused by a put or a get. Otherwise it was caused by an eviction or a remove.

sizeOf

Added in API level 12
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.

An entry's size must not change while it is in the cache.