LruCache

public class LruCache 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(boolean, K, V, V).

If a cache miss should be computed on demand for the corresponding keys, override create(K). 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(K, V) to size the cache in different units. For example, this cache is limited to 4MiB of bitmaps:

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

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

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

This class does not allow null to be used as a key or value. A return value of null from get(K), put(K, V) or remove(K) 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.

Public Constructor Summary

LruCache(int maxSize)

Public Method Summary

synchronized final int
createCount()
Returns the number of times create(Object) returned a value.
final void
evictAll()
Clear the cache, calling entryRemoved(boolean, K, V, V) on each removed entry.
synchronized final int
evictionCount()
Returns the number of values that have been evicted.
final V
get(K key)
Returns the value for key if it exists in the cache or can be created by #create.
synchronized final int
hitCount()
Returns the number of times get(K) returned a value that was already present in the cache.
synchronized final int
maxSize()
For caches that do not override sizeOf(K, V), this returns the maximum number of entries in the cache.
synchronized final int
missCount()
Returns the number of times get(K) returned null or required a new value to be created.
final V
put(K key, V value)
Caches value for key.
synchronized final int
putCount()
Returns the number of times put(K, V) was called.
final V
remove(K key)
Removes the entry for key if it exists.
synchronized final int
size()
For caches that do not override sizeOf(K, V), this returns the number of entries in the cache.
synchronized final Map<K, V>
snapshot()
Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.
synchronized final String
void
trimToSize(int maxSize)
Remove the eldest entries until the total of remaining entries is at or below the requested size.

Protected Method Summary

V
create(K key)
Called after a cache miss to compute a value for the corresponding key.
void
entryRemoved(boolean evicted, K key, V oldValue, V newValue)
Called for entries that have been evicted or removed.
int
sizeOf(K key, V value)
Returns the size of the entry for key and value in user-defined units.

Inherited Method Summary

Public Constructors

public LruCache (int maxSize)

Parameters
maxSize for caches that do not override sizeOf(K, V), 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

public final synchronized int createCount ()

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

public final void evictAll ()

Clear the cache, calling entryRemoved(boolean, K, V, V) on each removed entry.

public final synchronized int evictionCount ()

Returns the number of values that have been evicted.

public final V get (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.

Parameters
key

public final synchronized int hitCount ()

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

public final synchronized int maxSize ()

For caches that do not override sizeOf(K, V), 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.

public final synchronized int missCount ()

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

public final V put (K key, V value)

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

Parameters
key
value
Returns
  • the previous value mapped by key.

public final synchronized int putCount ()

Returns the number of times put(K, V) was called.

public final V remove (K key)

Removes the entry for key if it exists.