public class LruCache
extends Object
| java.lang.Object | |
| android.util.LruCache<K, V> | |
一个缓存,它拥有对有限数量值的强引用。 每次访问一个值时,它都被移动到队列头部。 将值添加到完整缓存时,该队列末尾的值将被逐出,并可能有资格进行垃圾回收。
如果您的缓存值包含需要明确释放的资源,请覆盖 entryRemoved(boolean, K, V, V) 。
如果需要为相应的键计算缓存未命中,请覆盖create(K) 。 这简化了调用代码,允许它假定一个值总是会被返回,即使有一个缓存未命中。
默认情况下,高速缓存大小是根据条目数衡量的。 覆盖sizeOf(K, V)以按不同单位调整缓存大小。 例如,该缓存仅限于4MiB的位图:
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();
}
}
这个类是线程安全的。 通过在缓存上同步来自动执行多个缓存操作:
synchronized (cache) {
if (cache.get(key) == null) {
cache.put(key, value);
}
}
该类不允许将null用作键或值。 从返回null值get(K) , put(K, V)或者remove(K)是明确的:关键并不在缓存中。
这个类出现在Android 3.1(Honeycomb MR1)中; 它可作为早期版本的一部分Android's Support Package 。
Public constructors |
|
|---|---|
LruCache(int maxSize) |
|
公共方法(Public methods) |
|
|---|---|
final int |
createCount() 返回 |
final void |
evictAll() 清除缓存,在每个移除的条目上调用 |
final int |
evictionCount() 返回已被驱逐的值的数量。 |
final V |
get(K key) 返回 |
final int |
hitCount() 返回 |
final int |
maxSize() 对于不覆盖 |
final int |
missCount() 返回 |
final V |
put(K key, V value) 缓存 |
final int |
putCount() 返回被调用的次数 |
final V |
remove(K key) 删除 |
void |
resize(int maxSize) 设置缓存的大小。 |
final int |
size() 对于不覆盖 |
final Map<K, V> |
snapshot() 返回缓存中当前内容的副本,从最近最少访问到最近访问的顺序排列。 |
final String |
toString() 返回对象的字符串表示形式。 |
void |
trimToSize(int maxSize) 删除最老的条目,直到剩余条目的总数达到或低于请求的大小。 |
Protected methods |
|
|---|---|
V |
create(K key) 在缓存未命中后调用以计算相应键的值。 |
void |
entryRemoved(boolean evicted, K key, V oldValue, V newValue) 呼叫已被驱逐或删除的条目。 |
int |
sizeOf(K key, V value) 以用户定义的单位返回 |
继承方法(Inherited methods) |
|
|---|---|
java.lang.Object
|
|
LruCache (int maxSize)
| 参数(Parameters) | |
|---|---|
maxSize |
int: for caches that do not override sizeOf(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. |
V get (K key)
返回key的值(如果它存在于缓存中或可由#create创建)。 如果返回值,则将其移至队列头部。 如果值没有被缓存并且不能被创建,则返回null。
| 参数(Parameters) | |
|---|---|
key |
K
|
| 返回(Returns) | |
|---|---|
V |
|
int maxSize ()
对于不覆盖sizeOf(K, V)缓存,这将返回缓存中的最大条目数。 对于所有其他缓存,这将返回此缓存中条目大小的最大总和。
| 返回(Returns) | |
|---|---|
int |
|
V put (K key,
V value)
缓存value为key 。 该值被移动到队列的头部。
| 参数(Parameters) | |
|---|---|
key |
K
|
value |
V
|
| 返回(Returns) | |
|---|---|
V |
the previous value mapped by key. |
V remove (K key)
删除条目 key如果存在)。
| 参数(Parameters) | |
|---|---|
key |
K
|
| 返回(Returns) | |
|---|---|
V |
the previous value mapped by key. |
void resize (int maxSize)
设置缓存的大小。
| 参数(Parameters) | |
|---|---|
maxSize |
int: The new maximum size. |
int size ()
对于不覆盖sizeOf(K, V)缓存,这将返回缓存中的条目数。 对于所有其他缓存,这将返回此缓存中条目的大小总和。
| 返回(Returns) | |
|---|---|
int |
|
Map<K, V> snapshot ()
返回缓存中当前内容的副本,从最近最少访问到最近访问的顺序排列。
| 返回(Returns) | |
|---|---|
Map<K, V> |
|
String toString ()
返回对象的字符串表示形式。 一般来说, toString方法返回一个“文本表示”这个对象的字符串。 结果应该是一个简洁但内容丰富的表述,对于一个人来说很容易阅读。 建议所有子类重写此方法。
类Object的toString方法返回一个字符串,其中包含对象为实例的类的名称,符号字符“ @ ”以及对象的哈希代码的无符号十六进制表示形式。 换句话说,这个方法返回一个字符串,其值等于:
getClass().getName() + '@' + Integer.toHexString(hashCode())
| 返回(Returns) | |
|---|---|
String |
a string representation of the object. |
void trimToSize (int maxSize)
删除最老的条目,直到剩余条目的总数达到或低于请求的大小。
| 参数(Parameters) | |
|---|---|
maxSize |
int: the maximum size of the cache before returning. May be -1 to evict even 0-sized elements. |
V create (K key)
在缓存未命中后调用以计算相应键的值。 返回计算值,如果不能计算值,则返回null。 默认实现返回null。
该方法在没有同步的情况下被调用:其他线程可以在该方法执行时访问缓存。
如果此方法返回时缓存中存在key的值,则创建的值将与entryRemoved(boolean, K, V, V)一起释放并丢弃。 当多个线程同时请求同一个键(导致创建多个值)时,或者当另一个线程调用put(K, V)而另一个线程为同一个键创建值时,可能会发生这种情况。
| 参数(Parameters) | |
|---|---|
key |
K
|
| 返回(Returns) | |
|---|---|
V |
|
void entryRemoved (boolean evicted,
K key,
V oldValue,
V newValue)
呼叫已被驱逐或删除的条目。 当一个值被驱逐以腾出空间时调用此方法,通过调用remove(K)将其remove(K) ,或者通过调用put(K, V)来put(K, V) 。 默认实现什么都不做。
该方法在没有同步的情况下被调用:其他线程可以在该方法执行时访问缓存。
| 参数(Parameters) | |
|---|---|
evicted |
boolean: true if the entry is being removed to make space, false if the removal was caused by a put(K, V) or remove(K). |
key |
K
|
oldValue |
V
|
newValue |
V: the new value for key, if it exists. If non-null, this removal was caused by a put(K, V). Otherwise it was caused by an eviction or a remove(K). |
int sizeOf (K key,
V value)
以用户定义的单位返回key和value的条目大小。 默认实现返回1,以便size是条目数量,max size是条目的最大数量。
条目的大小在缓存中时不能更改。
| 参数(Parameters) | |
|---|---|
key |
K
|
value |
V
|
| 返回(Returns) | |
|---|---|
int |
|