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
Simple limited size cache based on ConcurrentHashMap purging entries in LRU
order when reaching size limit
- Since:
- 5.1.9
-
Nested Class Summary
Nested Classes -
Field Summary
Fields -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprivate static void
checkPurgeFactor
(float purgeFactor) void
configure
(int maxSize, float purgeFactor) Reconfigures the cache.Returns the value to which the specified key is mapped, ornull
if this map contains no mapping for the key.private void
purge()
private static int
purgeSize
(int maxSize, float purgeFactor) Maps the specified key to the specified value in this cache.int
size()
Returns the current size of this cacheprivate long
tick()
-
Field Details
-
lock
-
map
-
maximumSize
private volatile int maximumSize -
purgeSize
private int purgeSize -
time
private volatile long time
-
-
Constructor Details
-
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 Details
-
checkPurgeFactor
private static void checkPurgeFactor(float purgeFactor) -
purgeSize
private static int purgeSize(int maxSize, float purgeFactor) -
get
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
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
-
tick
private long tick() -
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
-
purge
private void purge()
-