Class LimitedCache<T>

  • Type Parameters:
    T -

    public class LimitedCache<T>
    extends java.lang.Object
    A simple cache limited by size and duration and backed by a HashMap.

    Poor man's version of some functionality in Guava's ConcurrentLinkedHashMap
    This one does not hold an access count or latest access time.

    Since:
    4.1.5
    • Constructor Summary

      Constructors 
      Constructor Description
      LimitedCache​(int maxItems)
      Create a cache with unlimited duration for items.
      LimitedCache​(int maxItems, int maxSeconds)
      Create a cache with limited number of items and limited duration for items.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void clear()
      Remove all data from cache.
      boolean containsKey​(java.lang.String key)
      Returns true if this map contains a mapping for the specified key.
      T get​(java.lang.String key)
      Retrieve a value.
      void put​(java.lang.String key, T value)
      Add a value.
      T remove​(java.lang.String key)
      Remove a value.
      int size()
      Returns the number of key-value mappings in this cache.
      • Methods inherited from class java.lang.Object

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

      • LimitedCache

        public LimitedCache​(int maxItems)
        Create a cache with unlimited duration for items.
        Parameters:
        maxItems -
      • LimitedCache

        public LimitedCache​(int maxItems,
                            int maxSeconds)
        Create a cache with limited number of items and limited duration for items.
        Parameters:
        maxItems - values smaller than 1 are interpreted as "no limit" for number.
        maxSeconds - values smaller than 1 are interpreted as "no limit" for duration.
    • Method Detail

      • put

        public void put​(java.lang.String key,
                        T value)
        Add a value.
        Parameters:
        key -
        value -
      • get

        public T get​(java.lang.String key)
        Retrieve a value.
        Parameters:
        key -
        Returns:
      • remove

        public T remove​(java.lang.String key)
        Remove a value.

        Non existing entries will be ignored and return NULL.

        Parameters:
        key -
        Returns:
        value of the remove entry
      • clear

        public void clear()
        Remove all data from cache.
      • containsKey

        public boolean containsKey​(java.lang.String key)
        Returns true if this map contains a mapping for the specified key.
        Returns:
      • size

        public int size()
        Returns the number of key-value mappings in this cache.
        Returns: