Class EvictionCache<K,V>

java.lang.Object
org.naviqore.utils.cache.EvictionCache<K,V>
Type Parameters:
K - the type of keys maintained by this cache
V - the type of mapped values

public class EvictionCache<K,V> extends Object
A generic cache that supports LRU (Least Recently Used) and MRU (Most Recently Used) eviction strategies.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    static enum 
    Enum representing the eviction strategy.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a new EvictionCache with the specified size and eviction strategy.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears the cache, removing all key-value mappings.
    computeIfAbsent(K key, Supplier<V> supplier)
    If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given supplier and enters it into this cache.
    int
    Returns the current number of entries in the cache.
    boolean
    isCached(K key)
    Checks if the specified key is present in the cache.

    Methods inherited from class java.lang.Object

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

    • EvictionCache

      public EvictionCache(int size, EvictionCache.Strategy strategy)
      Constructs a new EvictionCache with the specified size and eviction strategy.
      Parameters:
      size - the maximum number of elements the cache can hold
      strategy - the eviction strategy to use (LRU or MRU)
  • Method Details

    • computeIfAbsent

      public V computeIfAbsent(K key, Supplier<V> supplier)
      If the specified key is not already associated with a value (or is mapped to null), attempts to compute its value using the given supplier and enters it into this cache.
      Parameters:
      key - the key with which the specified value is to be associated
      supplier - the supplier function to compute the value
      Returns:
      the current (existing or computed) value associated with the specified key
    • clear

      public void clear()
      Clears the cache, removing all key-value mappings.
    • isCached

      public boolean isCached(K key)
      Checks if the specified key is present in the cache.
      Parameters:
      key - the key whose presence in this cache is to be tested
      Returns:
      true if this cache contains a mapping for the specified key
    • getNumberOfEntries

      public int getNumberOfEntries()
      Returns the current number of entries in the cache.