A cache that holds strong references to a limited number of values. More...
Public Member Functions | |
LruCache (int maxSize) | |
synchronized final V | get (K key) |
Returns the value for. More... | |
synchronized final V | put (K key, V value) |
Caches. More... | |
synchronized final V | remove (K key) |
Removes the entry for. More... | |
synchronized final void | evictAll () |
Clear the cache, calling entryEvicted on each removed entry. | |
synchronized final int | size () |
For caches that do not override sizeOf, this returns the number of entries in the cache. More... | |
synchronized final int | maxSize () |
For caches that do not override sizeOf, this returns the maximum number of entries in the cache. More... | |
synchronized final int | hitCount () |
Returns the number of times get returned a value. | |
synchronized final int | missCount () |
Returns the number of times get returned null or required a new value to be created. | |
synchronized final int | createCount () |
Returns the number of times create(Object) returned a value. | |
synchronized final int | putCount () |
Returns the number of times put was called. | |
synchronized final int | evictionCount () |
Returns the number of values that have been evicted. | |
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 | toString () |
Protected Member Functions | |
void | trimToSize (int maxSize) |
void | entryEvicted (K key, V value) |
Called for entries that have reached the tail of the least recently used queue and are be removed. More... | |
void | entryRemoved (K key, V value) |
Called for entries that have been removed using the method remove. More... | |
V | create (K key) |
Called after a cache miss to compute a value for the corresponding key. More... | |
int | sizeOf (K key, V value) |
Returns the size of the entry for. More... | |
Protected Attributes | |
final LinkedHashMap< K, V > | map |
int | size |
Size of this cache in units. More... | |
int | evictionCount |
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 entryEvicted. This method is only invoked when values are evicted. Values replaced by calls to put must be released manually.
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:
int cacheSize = 4 * 1024 * 1024; // 4MiBLruCache<String, Bitmap> bitmapCache = new LruCache<String, Bitmap>(cacheSize) {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, put or remove is unambiguous: the key was not in the cache.
com.beyondar.android.util.cache.LruCache< K, V >.LruCache | ( | int | maxSize | ) |
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. |
|
protected |
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.
|
protected |
Called for entries that have reached the tail of the least recently used queue and are be removed.
The default implementation does nothing.
|
protected |
Called for entries that have been removed using the method remove.
The default implementation does nothing.
synchronized final V com.beyondar.android.util.cache.LruCache< K, V >.get | ( | K | key | ) |
Returns the value for.
if it exists in the cache or can be created by
. 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.
synchronized final int com.beyondar.android.util.cache.LruCache< K, V >.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.
synchronized final V com.beyondar.android.util.cache.LruCache< K, V >.put | ( | K | key, |
V | value | ||
) |
Caches.
for
. The value is moved to the head of the queue.
synchronized final V com.beyondar.android.util.cache.LruCache< K, V >.remove | ( | K | key | ) |
Removes the entry for.
if it exists.
synchronized final int com.beyondar.android.util.cache.LruCache< K, V >.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.
|
protected |
Returns the size of the entry for.
and
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.
|
protected |
Size of this cache in units.
Not necessarily the number of elements.