com.bumptech.glide.util
Class LruCache<T,Y>

java.lang.Object
  extended by com.bumptech.glide.util.LruCache<T,Y>
Type Parameters:
T - The type of the keys.
Y - The type of the values.
Direct Known Subclasses:
LruResourceCache

public class LruCache<T,Y>
extends Object

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(Object)} to change the size on a per item basis.


Constructor Summary
LruCache(int size)
          Constructor for LruCache.
 
Method Summary
 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.
 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 set 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.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LruCache

public LruCache(int size)
Constructor for LruCache.

Parameters:
size - The maximum size of the cache, the units must match the units used in getSize(Object).
Method Detail

setSizeMultiplier

public void setSizeMultiplier(float multiplier)
Sets a size multiplier that will be applied to the size provided in the constructor to set 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

protected int getSize(Y item)
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.

onItemEvicted

protected void onItemEvicted(T key,
                             Y item)
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 int getMaxSize()
Returns the current maximum size of the cache in bytes.


getCurrentSize

public int getCurrentSize()
Returns the sum of the sizes of all items in the cache.


contains

public boolean contains(T key)
Returns true if there is a value for the given key in the cache.

Parameters:
key - The key to check.

get

public Y get(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

public 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.

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.

Parameters:
key - The key to add the item at.
item - The item to add.

remove

public Y remove(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

public void clearMemory()
Clears all items in the cache.


trimToSize

protected void trimToSize(int 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.