-
public class LruCache<T, Y>A general purpose size limited cache that evicts items using an LRU algorithm. By default every item is assumed to have a size of one. Subclasses can override getSize} to change the size on a per item basis.
-
-
Field Summary
Fields Modifier and Type Field Description private longmaxSizeprivate longcurrentSize
-
Constructor Summary
Constructors Constructor Description LruCache(long size)Constructor for LruCache.
-
Method Summary
Modifier and Type Method Description synchronized longgetMaxSize()Returns the current maximum size of the cache in bytes. synchronized longgetCurrentSize()Returns the sum of the sizes of all items in the cache. synchronized voidsetSizeMultiplier(float multiplier)Sets a size multiplier that will be applied to the size provided in the constructor to put thenew size of the cache. synchronized booleancontains(@NonNull() T key)Returns true if there is a value for the given key in the cache. synchronized Yget(@NonNull() T key)Returns the item in the cache for the given key or null if no such item exists. synchronized Yput(@NonNull() T key, @Nullable() Y item)Adds the given item to the cache with the given key and returns any previous entry for thegiven key that may have already been in the cache. synchronized Yremove(@NonNull() T key)Removes the item at the given key and returns the removed item if present, and null otherwise. voidclearMemory()Clears all items in the cache. -
-
Method Detail
-
getMaxSize
synchronized long getMaxSize()
Returns the current maximum size of the cache in bytes.
-
getCurrentSize
synchronized long getCurrentSize()
Returns the sum of the sizes of all items in the cache.
-
setSizeMultiplier
synchronized void setSizeMultiplier(float multiplier)
Sets a size multiplier that will be applied to the size provided in the constructor to put thenew size of the cache. If the new size is less than the current size, entries will be evicteduntil the current size is less than or equal to the new size.
- Parameters:
multiplier- The multiplier to apply.
-
contains
synchronized boolean contains(@NonNull() T key)
Returns true if there is a value for the given key in the cache.
- Parameters:
key- The key to check.
-
get
@Nullable() synchronized Y get(@NonNull() T key)
Returns the item in the cache for the given key or null if no such item exists.
- Parameters:
key- The key to check.
-
put
@Nullable() synchronized Y put(@NonNull() T key, @Nullable() Y item)
Adds the given item to the cache with the given key and returns any previous entry for thegiven 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 tothe cache and instead onItemEvicted will be called synchronously withthe given key and item.
The size of the item is determined by the getSize method. To avoid errorswhere getSize returns different values for the same object when called atdifferent times, the size value is acquired in
{@code put}and retained until the item isevicted, replaced or removed.If
{@code item}is null the behavior here is a little odd. For the most part it's similar tosimply calling remove with the given key. The difference is that calling thismethod with a null{@code item}will result in an entry remaining in the cache with a nullvalue and 0 size. The only real consequence is that at some point onItemEvicted may be called with the given{@code key}and a null value. Ideally we'd make callingthis method with a null{@code item}identical to remove but we're preservingthis odd behavior to match older versions :(.- Parameters:
key- The key to add the item at.item- The item to add.
-
remove
@Nullable() synchronized Y remove(@NonNull() T key)
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
void clearMemory()
Clears all items in the cache.
-
-
-
-