Class LimitedCache<T>

java.lang.Object
com.priint.pubserver.util.LimitedCache<T>
Type Parameters:
T -

public class LimitedCache<T> extends 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

    Modifier and Type
    Method
    Description
    void
    Remove all data from cache.
    boolean
    Returns true if this map contains a mapping for the specified key.
    get(String key)
    Retrieve a value.
    void
    put(String key, T value)
    Add a value.
    Remove a value.
    int
    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 Details

    • 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 Details

    • put

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

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

      public T remove(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(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: