Class LruCache<T,Y>
- Type Parameters:
T- The type of the keys.Y- The type of the values.
- Direct Known Subclasses:
LruResourceCache
getSize(Object)} to
change the size on a per item basis.-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoidClears all items in the cache.booleanReturns true if there is a value for the given key in the cache.Returns the item in the cache for the given key or null if no such item exists.protected intgetCount()Returns the number of entries stored in cache.longReturns the sum of the sizes of all items in the cache.longReturns the current maximum size of the cache in bytes.protected intReturns the size of a given item, defaulting to one.protected voidonItemEvicted(T key, Y item) A callback called whenever an item is evicted from the cache.Adds the given item to the cache with the given key and returns any previous entry for the given key that may have already been in the cache.Removes the item at the given key and returns the removed item if present, and null otherwise.voidsetSizeMultiplier(float multiplier) Sets a size multiplier that will be applied to the size provided in the constructor to put the new size of the cache.protected voidtrimToSize(long size) Removes the least recently used items from the cache until the current size is less than the given size.
-
Constructor Details
-
LruCache
public LruCache(long size) Constructor for LruCache.- Parameters:
size- The maximum size of the cache, the units must match the units used ingetSize(Object).
-
-
Method Details
-
setSizeMultiplier
public void setSizeMultiplier(float multiplier) Sets a size multiplier that will be applied to the size provided in the constructor to put the new size of the cache. If the new size is less than the current size, entries will be evicted until the current size is less than or equal to the new size.- Parameters:
multiplier- The multiplier to apply.
-
getSize
Returns the size of a given item, defaulting to one. The units must match those used in the size passed in to the constructor. Subclasses can override this method to return sizes in various units, usually bytes.- Parameters:
item- The item to get the size of.
-
getCount
protected int getCount()Returns the number of entries stored in cache. -
onItemEvicted
A callback called whenever an item is evicted from the cache. Subclasses can override.- Parameters:
key- The key of the evicted item.item- The evicted item.
-
getMaxSize
public long getMaxSize()Returns the current maximum size of the cache in bytes. -
getCurrentSize
public long getCurrentSize()Returns the sum of the sizes of all items in the cache. -
contains
Returns true if there is a value for the given key in the cache.- Parameters:
key- The key to check.
-
get
Returns the item in the cache for the given key or null if no such item exists.- Parameters:
key- The key to check.
-
put
Adds the given item to the cache with the given key and returns any previous entry for the given key that may have already been in the cache.If the size of the item is larger than the total cache size, the item will not be added to the cache and instead
onItemEvicted(Object, Object)will be called synchronously with the given key and item.The size of the item is determined by the
getSize(Object)method. To avoid errors wheregetSize(Object)returns different values for the same object when called at different times, the size value is acquired inputand retained until the item is evicted, replaced or removed.If
itemis null the behavior here is a little odd. For the most part it's similar to simply callingremove(Object)with the given key. The difference is that calling this method with a nullitemwill result in an entry remaining in the cache with a null value and 0 size. The only real consequence is that at some pointonItemEvicted(Object, Object)may be called with the givenkeyand a null value. Ideally we'd make calling this method with a nullitemidentical toremove(Object)but we're preserving this odd behavior to match older versions :(.- Parameters:
key- The key to add the item at.item- The item to add.
-
remove
Removes the item at the given key and returns the removed item if present, and null otherwise.- Parameters:
key- The key to remove the item at.
-
clearMemory
public void clearMemory()Clears all items in the cache. -
trimToSize
protected void trimToSize(long size) Removes the least recently used items from the cache until the current size is less than the given size.- Parameters:
size- The size the cache should be less than.
-