Class LruCache<K,V>
- java.lang.Object
-
- com.ss.android.ugc.bytex.common.utils.LruCache<K,V>
-
public class LruCache<K,V> extends java.lang.ObjectStatic library version ofandroid.util.LruCache. Used to write apps that run on API levels prior to 12. When running on API level 12 or above, this implementation is still used; it does not try to switch to the framework's implementation. See the framework SDK documentation for a class overview.
-
-
Constructor Summary
Constructors Constructor Description LruCache(int maxSize)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()protected Vcreate(K key)Called after a cache miss to compute a value for the corresponding key.intcreateCount()Returns 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.voidevictAll()Clear the cache, callingentryRemoved(boolean, K, V, V)on each removed entry.intevictionCount()Returns the number of values that have been evicted.Vget(K key)Returns the value forkeyif it exists in the cache or can be created by#create.inthitCount()Returns the number of timesget(K)returned a value that was already present in the cache.intmaxSize()For caches that do not overridesizeOf(K, V), this returns the maximum number of entries in the cache.intmissCount()Returns the number of timesget(K)returned null or required a new value to be created.Vput(K key, V value)Cachesvalueforkey.intputCount()Returns the number of timesput(K, V)was called.Vremove(K key)Removes the entry forkeyif it exists.voidresize(int maxSize)Sets the size of the cache.intsize()For caches that do not overridesizeOf(K, V), this returns the number of entries in the cache.protected intsizeOf(K key, V value)Returns the size of the entry forkeyandvaluein user-defined units.java.util.Map<K,V>snapshot()Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.java.lang.StringtoString()voidtrimToSize(int maxSize)Remove the eldest entries until the total of remaining entries is at or below the requested size.
-
-
-
Constructor Detail
-
LruCache
public LruCache(int maxSize)
- Parameters:
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.
-
-
Method Detail
-
resize
public void resize(int maxSize)
Sets the size of the cache.- Parameters:
maxSize- The new maximum size.
-
get
public final V get(K key)
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
public final V put(K key, V value)
Cachesvalueforkey. The value is moved to the head of the queue.- Returns:
- the previous value mapped by
key.
-
trimToSize
public void trimToSize(int maxSize)
Remove the eldest entries until the total of remaining entries is at or below the requested size.- Parameters:
maxSize- the maximum size of the cache before returning. May be -1 to evict even 0-sized elements.
-
remove
public final V remove(K key)
Removes the entry forkeyif it exists.- Returns:
- the previous value mapped by
key.
-
entryRemoved
protected void entryRemoved(boolean evicted, K key, V oldValue, V newValue)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
protected V create(K key)
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
protected int sizeOf(K key, V value)
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
public final java.util.Map<K,V> snapshot()
Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.
-
clear
public void clear()
-
toString
public final java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
-