Package org.agrona.collections
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:
java.lang.AutoCloseable
public final class IntLruCache<E> extends java.lang.Object implements java.lang.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.
-
-
Constructor Summary
Constructors Constructor Description IntLruCache(int capacity, java.util.function.IntFunction<E> factory, java.util.function.Consumer<E> closer)
Constructor.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description int
capacity()
Returns cache capacity.void
close()
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.private void
makeMostRecent(int key, java.lang.Object value, int fromIndex)
-
-
-
Constructor Detail
-
IntLruCache
public IntLruCache(int capacity, java.util.function.IntFunction<E> factory, java.util.function.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 Detail
-
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, java.lang.Object value, int fromIndex)
-
capacity
public int capacity()
Returns cache capacity.- Returns:
- cache capacity.
-
close
public void close()
- Specified by:
close
in interfacejava.lang.AutoCloseable
-
-