Fork me
BeyondAR
Augmented Reality Framework for Android
 All Classes Functions Variables
com.beyondar.android.util.cache.LruCache< K, V > Class Reference

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

Collaboration diagram for com.beyondar.android.util.cache.LruCache< K, V >:

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...
 
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
 

Detailed Description

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; // 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, put or remove is unambiguous: the key was not in the cache.

Constructor & Destructor Documentation

com.beyondar.android.util.cache.LruCache< K, V >.LruCache ( int  maxSize)
Parameters
maxSizefor 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.

Member Function Documentation

V com.beyondar.android.util.cache.LruCache< K, V >.create ( key)
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.

void com.beyondar.android.util.cache.LruCache< K, V >.entryEvicted ( key,
value 
)
protected

Called for entries that have reached the tail of the least recently used queue and are be removed.

The default implementation does nothing.

void com.beyondar.android.util.cache.LruCache< K, V >.entryRemoved ( key,
value 
)
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 ( 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.

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 ( key,
value 
)

Caches.

value

for

key

. The value is moved to the head of the queue.

Returns
the previous value mapped by
key
. Although that entry is no longer cached, it has not been passed to entryEvicted
synchronized final V com.beyondar.android.util.cache.LruCache< K, V >.remove ( key)

Removes the entry for.

key

if it exists.

Returns
the previous value mapped by
key
. Although that entry is no longer cached, it has not been passed to entryEvicted
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.

int com.beyondar.android.util.cache.LruCache< K, V >.sizeOf ( key,
value 
)
protected

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.

Member Data Documentation

int com.beyondar.android.util.cache.LruCache< K, V >.size
protected

Size of this cache in units.

Not necessarily the number of elements.


The documentation for this class was generated from the following file: