Class LightWeightCache<K,E extends K>

java.lang.Object
org.apache.hadoop.util.LightWeightGSet<K,E>
org.apache.hadoop.util.LightWeightCache<K,E>
Type Parameters:
K - Key type for looking up the entries
E - Entry type, which must be (1) a subclass of K, and (2) implementing LightWeightCache.Entry interface, and
All Implemented Interfaces:
Iterable<E>, GSet<K,E>

@Private public class LightWeightCache<K,E extends K> extends LightWeightGSet<K,E>
A low memory footprint Cache which extends LightWeightGSet. An entry in the cache is expired if (1) it is added to the cache longer than the creation-expiration period, and (2) it is not accessed for the access-expiration period. When an entry is expired, it may be evicted from the cache. When the size limit of the cache is set, the cache will evict the entries with earliest expiration time, even if they are not expired. It is guaranteed that number of entries in the cache is less than or equal to the size limit. However, It is not guaranteed that expired entries are evicted from the cache. An expired entry may possibly be accessed after its expiration time. In such case, the expiration time may be updated. This class does not support null entry. This class is not thread safe.
  • Constructor Details

    • LightWeightCache

      public LightWeightCache(int recommendedLength, int sizeLimit, long creationExpirationPeriod, long accessExpirationPeriod)
      Parameters:
      recommendedLength - Recommended size of the internal array.
      sizeLimit - the limit of the size of the cache. The limit is disabled if it is <= 0.
      creationExpirationPeriod - the time period C > 0 in nanoseconds that the creation of an entry is expired if it is added to the cache longer than C.
      accessExpirationPeriod - the time period A >= 0 in nanoseconds that the access of an entry is expired if it is not accessed longer than A.
  • Method Details

    • get

      public E get(K key)
      Description copied from interface: GSet
      Return the stored element which is equal to the given key. This operation is similar to Map.get(Object).
      Specified by:
      get in interface GSet<K,E extends K>
      Overrides:
      get in class LightWeightGSet<K,E extends K>
      Parameters:
      key - The given key.
      Returns:
      The stored element if it exists. Otherwise, return null.
    • put

      public E put(E entry)
      Description copied from interface: GSet
      Add/replace an element. If the element does not exist, add it to the set. Otherwise, replace the existing element. Note that this operation is similar to Map.put(Object, Object) but is different from Set.add(Object) which does not replace the existing element if there is any.
      Specified by:
      put in interface GSet<K,E extends K>
      Overrides:
      put in class LightWeightGSet<K,E extends K>
      Parameters:
      entry - The element being put.
      Returns:
      the previous stored element if there is any. Otherwise, return null.
    • remove

      public E remove(K key)
      Description copied from interface: GSet
      Remove the element corresponding to the given key. This operation is similar to Map.remove(Object).
      Specified by:
      remove in interface GSet<K,E extends K>
      Overrides:
      remove in class LightWeightGSet<K,E extends K>
      Parameters:
      key - The key of the element being removed.
      Returns:
      If such element exists, return it. Otherwise, return null.
    • iterator

      public Iterator<E> iterator()
      Specified by:
      iterator in interface Iterable<K>
      Overrides:
      iterator in class LightWeightGSet<K,E extends K>