Class 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 java.lang.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

      Constructors 
      Constructor Description
      LruCache​(long size)
      Constructor for LruCache.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method 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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • LruCache

        public LruCache​(long 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 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

        protected int getSize​(@Nullable
                              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.
      • getCount

        protected int getCount()
        Returns the number of entries stored in cache.
      • onItemEvicted

        protected void onItemEvicted​(@NonNull
                                     T key,
                                     @Nullable
                                     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 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

        public 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
        public 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
        public 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 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 where getSize(Object) returns different values for the same object when called at different times, the size value is acquired in put and retained until the item is evicted, replaced or removed.

        If item is null the behavior here is a little odd. For the most part it's similar to simply calling remove(Object) with the given key. The difference is that calling this method with a null item will result in an entry remaining in the cache with a null value and 0 size. The only real consequence is that at some point onItemEvicted(Object, Object) may be called with the given key and a null value. Ideally we'd make calling this method with a null item identical to remove(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

        @Nullable
        public 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

        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.