Android Guides | Samples

Android.Util.LruCache Class

A cache that holds strong references to a limited number of values.

See Also: LruCache

Syntax

[Android.Runtime.Register("android/util/LruCache", DoNotGenerateAcw=true)]
public class LruCache : 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

The members of Android.Util.LruCache are listed below.

See Also: Object

Public Constructors

Protected Constructors

A constructor used when creating managed representations of JNI objects; called by the runtime.

Protected Properties

[read-only]
override
ThresholdClassIntPtr. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.
[read-only]
override
ThresholdTypeType. This API supports the Mono for Android infrastructure and is not intended to be used directly from your code.

Public Methods

CreateCount() : Int32
Returns the number of times LruCache.create(K) returned a value.
EvictAll()
Clear the cache, calling LruCache.entryRemoved(boolean, K, V, V) on each removed entry.
EvictionCount() : Int32
Returns the number of values that have been evicted.
Get(Object) : Object
HitCount() : Int32
Returns the number of times LruCache.get(K) returned a value that was already present in the cache.
MaxSize() : Int32
For caches that do not override LruCache.sizeOf(K, V), this returns the maximum number of entries in the cache.
MissCount() : Int32
Returns the number of times LruCache.get(K) returned null or required a new value to be created.
Put(Object, Object) : Object
PutCount() : Int32
Returns the number of times LruCache.put(K, V) was called.
Remove(Object) : Object
Resize(Int32)
Sets the size of the cache.
Size() : Int32
For caches that do not override LruCache.sizeOf(K, V), this returns the number of entries in the cache.
Snapshot() : IDictionary
Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.
override
ToString() : String
Returns a string containing a concise, human-readable description of this object.
TrimToSize(Int32)
Remove the eldest entries until the total of remaining entries is at or below the requested size.

Protected Methods