Package libcore.util

Class BasicLruCache<K,​V>

java.lang.Object
libcore.util.BasicLruCache<K,​V>
Direct Known Subclasses:
TimeZoneNames.ZoneStringsCache

public class BasicLruCache<K,​V>
extends Object
A minimal least-recently-used cache for libcore. Prefer android.util.LruCache where that is available.
  • Constructor Summary

    Constructors
    Constructor Description
    BasicLruCache​(int maxSize)  
  • Method Summary

    Modifier and Type Method Description
    protected V create​(K key)
    Called after a cache miss to compute a value for the corresponding key.
    protected void entryEvicted​(K key, V value)
    Called for entries that have reached the tail of the least recently used queue and are be removed.
    void evictAll()
    Clear the cache, calling entryEvicted(K, V) on each removed entry.
    V get​(K key)
    Returns the value for key if it exists in the cache or can be created by #create.
    V put​(K key, V value)
    Caches value for key.
    Map<K,​V> snapshot()
    Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.

    Methods inherited from class java.lang.Object

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

    • BasicLruCache

      public BasicLruCache​(int maxSize)
  • Method Details

    • get

      public final V get​(K key)
      Returns the value for key if it exists in the cache or can be created by #create. If a value was returned, it is moved to the head of the queue. This returns null if a value is not cached and cannot be created.
    • put

      public final V put​(K key, V value)
      Caches value for key. The value is moved to the head of the queue.
      Returns:
      the previous value mapped by key. Although that entry is no longer cached, it has not been passed to entryEvicted(K, V).
    • entryEvicted

      protected void entryEvicted​(K key, V value)
      Called for entries that have reached the tail of the least recently used queue and are be removed. The default implementation does nothing.
    • create

      protected V create​(K key)
      Called after a cache miss to compute a value for the corresponding key. Returns the computed value or null if no value can be computed. The default implementation returns null.
    • snapshot

      public final Map<K,​V> snapshot()
      Returns a copy of the current contents of the cache, ordered from least recently accessed to most recently accessed.
    • evictAll

      public final void evictAll()
      Clear the cache, calling entryEvicted(K, V) on each removed entry.