T
- The type of the keys.Y
- The type of the values.public class LruCache<T,Y> extends Object
getSize(Object)
} to
change the size on a per item basis.Constructor and Description |
---|
LruCache(int size)
Constructor for LruCache.
|
Modifier and Type | Method and Description |
---|---|
void |
clearMemory()
Clears all items in the cache.
|
boolean |
contains(T key)
Returns true if there is a value for the given key in the cache.
|
Y |
get(T key)
Returns the item in the cache for the given key or null if no such item exists.
|
protected int |
getCount()
Returns the number of entries stored in cache.
|
int |
getCurrentSize()
Returns the sum of the sizes of all items in the cache.
|
int |
getMaxSize()
Returns the current maximum size of the cache in bytes.
|
protected int |
getSize(Y item)
Returns the size of a given item, defaulting to one.
|
protected void |
onItemEvicted(T key,
Y item)
A callback called whenever an item is evicted from the cache.
|
Y |
put(T key,
Y item)
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.
|
Y |
remove(T key)
Removes the item at the given key and returns the removed item if present, and null otherwise.
|
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.
|
protected void |
trimToSize(int size)
Removes the least recently used items from the cache until the current size is less than the
given size.
|
public LruCache(int size)
size
- The maximum size of the cache, the units must match the units used in getSize(Object)
.public void setSizeMultiplier(float multiplier)
multiplier
- The multiplier to apply.protected int getSize(Y item)
item
- The item to get the size of.protected int getCount()
protected void onItemEvicted(T key, Y item)
key
- The key of the evicted item.item
- The evicted item.public int getMaxSize()
public int getCurrentSize()
public boolean contains(T key)
key
- The key to check.@Nullable public Y get(T key)
key
- The key to check.public Y put(T key, Y item)
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.
key
- The key to add the item at.item
- The item to add.@Nullable public Y remove(T key)
key
- The key to remove the item at.public void clearMemory()
protected void trimToSize(int size)
size
- The size the cache should be less than.