Package org.eclipse.jgit.util
Class SimpleLruCache<K,V>
- java.lang.Object
-
- org.eclipse.jgit.util.SimpleLruCache<K,V>
-
- Type Parameters:
K
- the type of keys maintained by this cacheV
- the type of mapped values
public class SimpleLruCache<K,V> extends Object
Simple limited size cache based on ConcurrentHashMap purging entries in LRU order when reaching size limit- Since:
- 5.1.9
-
-
Constructor Summary
Constructors Constructor Description SimpleLruCache(int maxSize, float purgeFactor)
Create a new cache
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
configure(int maxSize, float purgeFactor)
Reconfigures the cache.V
get(Object key)
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.V
put(K key, V value)
Maps the specified key to the specified value in this cache.int
size()
Returns the current size of this cache
-
-
-
Constructor Detail
-
SimpleLruCache
public SimpleLruCache(int maxSize, float purgeFactor)
Create a new cache- Parameters:
maxSize
- maximum size of the cache, to reduce need for synchronization this is not a hard limit. The real size of the cache could be slightly above this maximum if multiple threads put new values concurrentlypurgeFactor
- when the size of the map reaches maxSize the oldest entries will be purged to free up some space for new entries,purgeFactor
is the fraction ofmaxSize
to purge when this happens
-
-
Method Detail
-
get
public V get(Object key)
Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.More formally, if this cache contains a mapping from a key
k
to a valuev
such thatkey.equals(k)
, then this method returnsv
; otherwise it returnsnull
. (There can be at most one such mapping.)- Parameters:
key
- the key- Returns:
- value mapped for this key, or
null
if no value is mapped - Throws:
NullPointerException
- if the specified key is null
-
put
public V put(@NonNull K key, @NonNull V value)
Maps the specified key to the specified value in this cache. Neither the key nor the value can be null.The value can be retrieved by calling the
get
method with a key that is equal to the original key.- Parameters:
key
- key with which the specified value is to be associatedvalue
- value to be associated with the specified key- Returns:
- the previous value associated with
key
, ornull
if there was no mapping forkey
- Throws:
NullPointerException
- if the specified key or value is null
-
size
public int size()
Returns the current size of this cache- Returns:
- the number of key-value mappings in this cache
-
configure
public void configure(int maxSize, float purgeFactor)
Reconfigures the cache. IfmaxSize
is reduced some entries will be purged.- Parameters:
maxSize
- maximum size of the cachepurgeFactor
- when the size of the map reaches maxSize the oldest entries will be purged to free up some space for new entries,purgeFactor
is the fraction ofmaxSize
to purge when this happens
-
-