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:
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 Summary
Fields -
Constructor Summary
ConstructorsConstructorDescriptionIntLruCache
(int capacity, IntFunction<E> factory, Consumer<E> closer) Constructor. -
Method Summary
Modifier and TypeMethodDescriptionint
capacity()
Returns cache capacity.void
close()
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, Object value, int fromIndex)
-
Field Details
-
capacity
private final int capacity -
factory
-
closer
-
keys
private final int[] keys -
values
-
size
private int size
-
-
Constructor Details
-
IntLruCache
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
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
-
capacity
public int capacity()Returns cache capacity.- Returns:
- cache capacity.
-
close
public void close()- Specified by:
close
in interfaceAutoCloseable
-