类 LruCache<K,V>
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.
-
构造器概要
构造器 -
方法概要
修饰符和类型方法说明protected VCalled after a cache miss to compute a value for the corresponding key.final intReturns the number of timescreate(Object)returned a value.protected voidentryRemoved(boolean evicted, K key, V oldValue, V newValue) Called for entries that have been evicted or removed.final voidevictAll()Clear the cache, callingentryRemoved(boolean, K, V, V)on each removed entry.final intReturns the number of values that have been evicted.final VReturns the value forkeyif it exists in the cache or can be created by#create.final inthitCount()Returns the number of timesget(K)returned a value that was already present in the cache.final intmaxSize()For caches that do not overridesizeOf(K, V), this returns the maximum number of entries in the cache.final intReturns the number of timesget(K)returned null or required a new value to be created.final VCachesvalueforkey.final intputCount()Returns the number of timesput(K, V)was called.final VRemoves the entry forkeyif it exists.voidresize(int maxSize) Sets the size of the cache.final intsize()For caches that do not overridesizeOf(K, V), this returns the number of entries in the cache.protected intReturns the size of the entry forkeyandvaluein user-defined units.snapshot()Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.final StringtoString()
-
构造器详细资料
-
LruCache
public LruCache(int maxSize) - 参数:
maxSize- for caches that do not overridesizeOf(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.
-
-
方法详细资料
-
resize
public void resize(int maxSize) Sets the size of the cache.- 参数:
maxSize- The new maximum size.
-
get
Returns the value forkeyif 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. -
put
Cachesvalueforkey. The value is moved to the head of the queue.- 返回:
- the previous value mapped by
key.
-
remove
Removes the entry forkeyif it exists.- 返回:
- the previous value mapped by
key.
-
entryRemoved
Called for entries that have been evicted or removed. This method is invoked when a value is evicted to make space, removed by a call toremove(K), or replaced by a call toput(K, V). The default implementation does nothing.The method is called without synchronization: other threads may access the cache while this method is executing.
-
create
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.The method is called without synchronization: other threads may access the cache while this method is executing.
If a value for
keyexists in the cache when this method returns, the created value will be released withentryRemoved(boolean, K, V, V)and discarded. This can occur when multiple threads request the same key at the same time (causing multiple values to be created), or when one thread callsput(K, V)while another is creating a value for the same key. -
sizeOf
Returns the size of the entry forkeyandvaluein 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.
-
evictAll
public final void evictAll()Clear the cache, callingentryRemoved(boolean, K, V, V)on each removed entry. -
size
public final int size()For caches that do not overridesizeOf(K, V), 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. -
maxSize
public final int maxSize()For caches that do not overridesizeOf(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. -
hitCount
public final int hitCount()Returns the number of timesget(K)returned a value that was already present in the cache. -
missCount
public final int missCount()Returns the number of timesget(K)returned null or required a new value to be created. -
createCount
public final int createCount()Returns the number of timescreate(Object)returned a value. -
putCount
public final int putCount()Returns the number of timesput(K, V)was called. -
evictionCount
public final int evictionCount()Returns the number of values that have been evicted. -
snapshot
Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed. -
toString
-