Top Description Inners Fields Constructors Methods
org.eclipse.jgit.util

public Class SimpleLruCache<K, V>

extends Object
Class Inheritance
Type Parameters
<K>
the type of keys maintained by this cache
<V>
the type of mapped values
Imports
java.text.MessageFormat, java.util.ArrayList, .Collections, .Comparator, .List, .Map, java.util.concurrent.ConcurrentHashMap, java.util.concurrent.locks.Lock, .ReentrantLock, org.eclipse.jgit.annotations.NonNull, org.eclipse.jgit.internal.JGitText

Simple limited size cache based on ConcurrentHashMap purging entries in LRU order when reaching size limit
Since
5.1.9

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Field Summary

Modifier and TypeField and Description
private Lock
private Map<K, SimpleLruCache.Entry<K, V>>
private volatile int
private int
private volatile long

Constructor Summary

AccessConstructor and Description
public
SimpleLruCache(int
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 concurrently
maxSize
,
float
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 of maxSize to purge when this happens
purgeFactor
)

Create a new cache

Method Summary

Modifier and TypeMethod and Description
private static void
checkPurgeFactor(float purgeFactor)

public void
configure(int
maximum size of the cache
maxSize
,
float
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 of maxSize to purge when this happens
purgeFactor
)

Reconfigures the cache.

public V

Returns:

value mapped for this key, or null if no value is mapped
get
(Object
the key
key
)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

private void
private static int
purgeSize(int maxSize, float purgeFactor)

public V

Returns:

the previous value associated with key, or null if there was no mapping for key
put
(K
key with which the specified value is to be associated
key
,
V
value to be associated with the specified key
value
)

Maps the specified key to the specified value in this cache.

public int

Returns:

the number of key-value mappings in this cache
size
()

Returns the current size of this cache

private long
tick()

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

lockback to summary
private Lock lock
mapback to summary
private Map<K, SimpleLruCache.Entry<K, V>> map
maximumSizeback to summary
private volatile int maximumSize
purgeSizeback to summary
private int purgeSize
timeback to summary
private volatile long time

Constructor Detail

SimpleLruCacheback to summary
public SimpleLruCache(int maxSize, float purgeFactor)

Create a new cache

Parameters
maxSize:int

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 concurrently

purgeFactor:float

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 of maxSize to purge when this happens

Method Detail

checkPurgeFactorback to summary
private static void checkPurgeFactor(float purgeFactor)
configureback to summary
public void configure(int maxSize, float purgeFactor)

Reconfigures the cache. If maxSize is reduced some entries will be purged.

Parameters
maxSize:int

maximum size of the cache

purgeFactor:float

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 of maxSize to purge when this happens

getback to summary
public V get(Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key.

More formally, if this cache contains a mapping from a key k to a value v such that key.equals(k), then this method returns v; otherwise it returns null. (There can be at most one such mapping.)

Parameters
key:Object

the key

Returns:V

value mapped for this key, or null if no value is mapped

Annotations
@SuppressWarnings:NonAtomicVolatileUpdate
Exceptions
NullPointerException:
if the specified key is null
purgeback to summary
private void purge()
purgeSizeback to summary
private static int purgeSize(int maxSize, float purgeFactor)
putback to summary
public V put(K key, 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:K

key with which the specified value is to be associated

value:V

value to be associated with the specified key

Returns:V

the previous value associated with key, or null if there was no mapping for key

Annotations
@SuppressWarnings:NonAtomicVolatileUpdate
Exceptions
NullPointerException:
if the specified key or value is null
sizeback to summary
public int size()

Returns the current size of this cache

Returns:int

the number of key-value mappings in this cache

tickback to summary
private long tick()
Annotations
@SuppressWarnings:NonAtomicVolatileUpdate
org.eclipse.jgit.util back to summary

private Class SimpleLruCache.Entry<K, V>

extends Object
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final K
private volatile long
private long
private final V

Constructor Summary

AccessConstructor and Description
pack-priv
Entry(K key, V value, long lastAccessed)

Method Summary

Modifier and TypeMethod and Description
pack-priv void
public String
toString()

Overrides java.lang.Object.toString.

Returns a string representation of the object.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

keyback to summary
private final K key
lastAccessedback to summary
private volatile long lastAccessed
lastAccessedSortingback to summary
private long lastAccessedSorting
valueback to summary
private final V value

Constructor Detail

Entryback to summary
pack-priv Entry(K key, V value, long lastAccessed)

Method Detail

copyAccessTimeback to summary
pack-priv void copyAccessTime()
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Doc from java.lang.Object.toString.

Returns a string representation of the object. Satisfying this method's contract implies a non-null result must be returned.

Returns:String

a string representation of the object

Annotations
@SuppressWarnings:nls
@Override