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(long 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.
|
long |
getCurrentSize()
Returns the sum of the sizes of all items in the cache.
|
long |
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(long size)
Removes the least recently used items from the cache until the current size is less than the
given size.
|
public LruCache(long 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(@Nullable Y item)
item
- The item to get the size of.protected int getCount()
protected void onItemEvicted(@NonNull T key, @Nullable Y item)
key
- The key of the evicted item.item
- The evicted item.public long getMaxSize()
public long getCurrentSize()
public boolean contains(@NonNull T key)
key
- The key to check.@Nullable public Y get(@NonNull T key)
key
- The key to check.@Nullable public Y put(@NonNull T key, @Nullable 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(@NonNull T key)
key
- The key to remove the item at.public void clearMemory()
protected void trimToSize(long size)
size
- The size the cache should be less than.