Class LruCacheLong<V>

java.lang.Object
gaiasky.util.LruCacheLong<V>
Type Parameters:
V - The type of values stored in the cache.

public class LruCacheLong<V> extends Object
A fixed-capacity cache that uses a least-recently-used (LRU) eviction policy. This implementation uses primitive long keys to avoid boxing overhead.
  • Constructor Summary

    Constructors
    Constructor
    Description
    LruCacheLong(int capacity)
    Constructs an LRU cache with the specified capacity.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Clears all entries from the cache.
    boolean
    containsKey(long key)
    Checks if the cache contains the specified key.
    get(long key)
    Retrieves the value associated with the specified key.
    Retrieves the least recently used entry without removing it.
    void
    put(long key, V value)
    Adds a key-value pair to the cache or updates the value if the key already exists.
    remove(long key)
    Removes the entry associated with the specified key.
    int
    Returns the current number of entries in the cache.

    Methods inherited from class Object

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

    • LruCacheLong

      public LruCacheLong(int capacity)
      Constructs an LRU cache with the specified capacity.
      Parameters:
      capacity - the maximum number of entries in the cache
  • Method Details

    • getLeastRecentlyUsed

      public V getLeastRecentlyUsed()
      Retrieves the least recently used entry without removing it.
      Returns:
      the value of the least recently used entry, or null if the cache is empty
    • get

      public V get(long key)
      Retrieves the value associated with the specified key.
      Parameters:
      key - the key whose associated value is to be returned
      Returns:
      the value associated with the specified key, or null if not found
    • put

      public void put(long key, V value)
      Adds a key-value pair to the cache or updates the value if the key already exists.
      Parameters:
      key - the key
      value - the value
    • containsKey

      public boolean containsKey(long key)
      Checks if the cache contains the specified key.
      Parameters:
      key - the key to check
      Returns:
      true if the key is present, false otherwise
    • remove

      public V remove(long key)
      Removes the entry associated with the specified key.
      Parameters:
      key - the key to remove
      Returns:
      the removed value, or null if the key was not present
    • size

      public int size()
      Returns the current number of entries in the cache.
      Returns:
      the size of the cache
    • clear

      public void clear()
      Clears all entries from the cache.