Class ConcurrentLruCache<T>

  • Type Parameters:
    T - the type of entries stored in the cache

    public class ConcurrentLruCache<T>
    extends java.lang.Object
    A thread-safe LRU (Least Recently Used) cache with a configurable size limit. Entries beyond the limit are evicted in LRU order.
    • Constructor Summary

      Constructors 
      Constructor Description
      ConcurrentLruCache​(int limit)
      Creates a new LRU cache with the specified size limit.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void add​(T entry)
      Adds an entry to the cache.
      void clear()
      Removes all entries from the cache.
      boolean contains​(T entry)
      Checks whether the cache contains the given entry.
      int getLimit()
      Returns the current maximum number of entries allowed in the cache.
      void setLimit​(int limit)
      Sets the maximum number of entries in the cache.
      • Methods inherited from class java.lang.Object

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

      • ConcurrentLruCache

        public ConcurrentLruCache​(int limit)
        Creates a new LRU cache with the specified size limit.
        Parameters:
        limit - the maximum number of entries in the cache
    • Method Detail

      • add

        public void add​(T entry)
        Adds an entry to the cache. If the cache is at its limit, the least recently used entry will be evicted.
        Parameters:
        entry - the entry to add
      • contains

        public boolean contains​(T entry)
        Checks whether the cache contains the given entry. Accessing an entry through this method updates its recency.
        Parameters:
        entry - the entry to check
        Returns:
        true if the entry is in the cache
      • clear

        public void clear()
        Removes all entries from the cache.
      • setLimit

        public void setLimit​(int limit)
        Sets the maximum number of entries in the cache. If the new limit is smaller than the current size, the least recently used entries will be evicted. A limit of 0 or less disables the cache.
        Parameters:
        limit - the new maximum number of entries
      • getLimit

        public int getLimit()
        Returns the current maximum number of entries allowed in the cache.
        Returns:
        the cache limit, or -1 if the cache is disabled