Class IntLruCache<E>

java.lang.Object
org.agrona.collections.IntLruCache<E>
Type Parameters:
E - the type of element that this cache holds.
All Implemented Interfaces:
AutoCloseable

public final class IntLruCache<E> extends Object implements AutoCloseable
A fixed capacity cache of int keyed values that evicts the least-recently-used element when it runs out of space.

When an element is evicted it is closed by calling the closer function with the element as an argument.

When a new key arrives the factory function is called in order to create the new element associated with that key.

  • Field Details

    • capacity

      private final int capacity
    • factory

      private final IntFunction<E> factory
    • closer

      private final Consumer<E> closer
    • keys

      private final int[] keys
    • values

      private final Object[] values
    • size

      private int size
  • Constructor Details

    • IntLruCache

      public IntLruCache(int capacity, IntFunction<E> factory, Consumer<E> closer)
      Constructor.
      Parameters:
      capacity - this is the fixed capacity of the cache.
      factory - a function for constructing new elements based upon keys.
      closer - a function for cleaning up resources associated with elements.
  • Method Details

    • lookup

      public E lookup(int key)
      Looks up an element in the cache, creating a new element if it doesn't exist and evicting the least recently used element if there's no space left in the cache.
      Parameters:
      key - the key to look up the element by.
      Returns:
      the element associated with this key.
    • makeMostRecent

      private void makeMostRecent(int key, Object value, int fromIndex)
    • capacity

      public int capacity()
      Returns cache capacity.
      Returns:
      cache capacity.
    • close

      public void close()
      Specified by:
      close in interface AutoCloseable