Android.Util.LruCache Class
A cache that holds strong references to a limited number of values.

See Also: LruCache Members

Syntax

[Android.Runtime.Register("android/util/LruCache", DoNotGenerateAcw=true)]
public class LruCache : Java.Lang.Object

Remarks

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

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

java Example

int cacheSize = 4 * 1024 * 1024; // 4MiB
   LruCache bitmapCache = new LruCache(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:

java Example

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 LruCache.get(K), LruCache.put(K, V) or LruCache.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 for earlier releases.

[Android Documentation]

Requirements

Namespace: Android.Util
Assembly: Mono.Android (in Mono.Android.dll)
Assembly Versions: 0.0.0.0
Since: Added in API level 12