LruCache


public class LruCache<K extends Object, V extends Object>


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:

val cacheSize = 4 * 1024 * 1024; // 4MiB
val bitmapCache = LruCache<String, Bitmap>(cacheSize) {
override fun sizeOf(key: String, value: Bitmap) = value.byteCount
}

This class is thread-safe.

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.

Summary

Public constructors

<K extends Object, V extends Object> LruCache(@IntRange(from = 1, to = 2147483647) int maxSize)

Creates a new LruCache

Public methods

final int

Returns the number of times create returned a value.

final void

Clear the cache, calling entryRemoved on each removed entry.

final int

Returns the number of values that have been evicted.

final V
get(@NonNull K key)

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

final int

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

final int

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

final int

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

final V
put(@NonNull K key, @NonNull V value)

Caches value for key.

final int

Returns the number of times put was called.

final V
remove(@NonNull K key)

Removes the entry for key if it exists.

void
resize(@IntRange(from = 1, to = 2147483647) int maxSize)

Sets the size of the cache.

final int

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

final @NonNull Map<@NonNull K, @NonNull V>

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

@NonNull String
void
trimToSize(int maxSize)

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

Protected methods

V
create(@NonNull K key)

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

void
entryRemoved(
    boolean evicted,
    @NonNull K key,
    @NonNull V oldValue,
    V newValue
)

Called for entries that have been evicted or removed.

int
sizeOf(@NonNull K key, @NonNull V value)

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

Public constructors

LruCache

public <K extends Object, V extends Object> LruCache(@IntRange(from = 1, to = 2147483647) int maxSize)

Creates a new LruCache

Parameters
@IntRange(from = 1, to = 2147483647) int 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.

Public methods

createCount

Added in 1.0.0
public final int createCount()

Returns the number of times create returned a value.

evictAll

Added in 1.0.0
public final void evictAll()

Clear the cache, calling entryRemoved on each removed entry.

evictionCount

Added in 1.0.0
public final int evictionCount()

Returns the number of values that have been evicted.

get

Added in 1.0.0
public final V get(@NonNull K key)

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 1.0.0
public final int hitCount()

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

maxSize

Added in 1.0.0
public final int maxSize()

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 1.0.0
public final int missCount()

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

put

Added in 1.0.0
public final V put(@NonNull K key, @NonNull V value)

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

Returns
V

the previous value mapped by key.

putCount

Added in 1.0.0
public final int putCount()

Returns the number of times put was called.

remove

Added in 1.0.0
public final V remove(@NonNull K key)

Removes the entry for key if it exists.

Returns
V

the previous value mapped by key.

resize

Added in 1.0.0
public void resize(@IntRange(from = 1, to = 2147483647) int maxSize)

Sets the size of the cache.

Parameters
@IntRange(from = 1, to = 2147483647) int maxSize

The new maximum size.

size

Added in 1.0.0
public final int size()

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 1.0.0
public final @NonNull Map<@NonNull K, @NonNull V> snapshot()

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

toString

Added in 1.0.0
public @NonNull String toString()

trimToSize

Added in 1.0.0
public void trimToSize(int maxSize)

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

Parameters
int maxSize

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

Protected methods

create

Added in 1.0.0
protected V create(@NonNull K key)

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 1.0.0
protected void entryRemoved(
    boolean evicted,
    @NonNull K key,
    @NonNull V oldValue,
    V newValue
)

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
boolean evicted

true if the entry is being removed to make space, false if the removal was caused by a put or remove.

@NonNull K key

key of the entry that was evicted or removed.

@NonNull V oldValue

the original value of the entry that was evicted removed.

V newValue

the new value for key, if it exists. If non-null, this removal was caused by a put. Otherwise it was caused by an eviction or a remove.

sizeOf

Added in 1.0.0
protected int sizeOf(@NonNull K key, @NonNull V value)

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.