Package fr.gouv.vitam.common.lru
Class SynchronizedLruCache<K,V>
- java.lang.Object
-
- fr.gouv.vitam.common.lru.SynchronizedLruCache<K,V>
-
- Type Parameters:
K
- KeyV
- Value
- All Implemented Interfaces:
InterfaceLruCache<K,V>
public class SynchronizedLruCache<K,V> extends java.lang.Object
Threadsafe synchronized implementation of LruCache based on LinkedHashMap. Threadsafety is provided by method synchronization. This cache implementation should be used with low number of threads. author Damian Momot
-
-
Field Summary
Fields Modifier and Type Field Description static int
DEFAULT_INITIAL_CAPACITY
Initial capacitystatic float
DEFAULT_LOAD_FACTOR
Load factor
-
Constructor Summary
Constructors Constructor Description SynchronizedLruCache(int capacity, long ttl)
Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR and DEFAULT_INITIAL_CAPACITYSynchronizedLruCache(int capacity, long ttl, int initialCapacity)
Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTORSynchronizedLruCache(int capacity, long ttl, int initialCapacity, float loadFactor)
Creates new SynchronizedLruCache
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
clear()
Removes all entries from cacheboolean
contains(K key)
Checks whether cache contains valid entry for keyprotected InterfaceLruCacheEntry<V>
createEntry(V value, long ttl)
Creates new LruCacheEntry. int
forceClearOldest()
Removes all oldest entries from cache (ttl based)V
get(K key)
Returns value cached with key.V
get(K key, java.util.concurrent.Callable<V> callback)
Tries to get element from cache.V
get(K key, java.util.concurrent.Callable<V> callback, long ttl)
Tries to get element from cache.int
getCapacity()
Returns cache capacityprotected InterfaceLruCacheEntry<V>
getEntry(K key)
Returns LruCacheEntry mapped by key or null if it does not existlong
getTtl()
Returns cache TTLprotected V
getValue(K key)
Tries to retrieve value by it's key.boolean
isEmpty()
Checks whether cache is empty.void
put(K key, V value)
Puts value under key into cache.void
put(K key, V value, long ttl)
Puts value under key into cache with desired TTLprotected void
putEntry(K key, InterfaceLruCacheEntry<V> entry)
Puts entry into cacheV
remove(K key)
Removes entry from cache (if exists)void
setNewTtl(long ttl)
Set a new TTL (for newly set objects only, not changing old values).int
size()
Returns number of entries stored in cache (including invalid ones)void
updateTtl(K key)
Update the TTL of the associated object if it still exists
-
-
-
Field Detail
-
DEFAULT_INITIAL_CAPACITY
public static final int DEFAULT_INITIAL_CAPACITY
Initial capacity- See Also:
- Constant Field Values
-
DEFAULT_LOAD_FACTOR
public static final float DEFAULT_LOAD_FACTOR
Load factor- See Also:
- Constant Field Values
-
-
Constructor Detail
-
SynchronizedLruCache
public SynchronizedLruCache(int capacity, long ttl, int initialCapacity, float loadFactor)
Creates new SynchronizedLruCache- Parameters:
capacity
- max cache capacityttl
- time to live in millisecondsinitialCapacity
- initial cache capacityloadFactor
-
-
SynchronizedLruCache
public SynchronizedLruCache(int capacity, long ttl, int initialCapacity)
Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR- Parameters:
capacity
- max cache capacityttl
- time to live in millisecondsinitialCapacity
- initial cache capacity
-
SynchronizedLruCache
public SynchronizedLruCache(int capacity, long ttl)
Creates new SynchronizedLruCache with DEFAULT_LOAD_FACTOR and DEFAULT_INITIAL_CAPACITY- Parameters:
capacity
- max cache capacityttl
- time to live in milliseconds
-
-
Method Detail
-
clear
public void clear()
Description copied from interface:InterfaceLruCache
Removes all entries from cache
-
get
public V get(K key)
Description copied from interface:InterfaceLruCache
Returns value cached with key.- Specified by:
get
in interfaceInterfaceLruCache<K,V>
- Returns:
- value or null if key doesn't exist or entry is not valid
-
getCapacity
public int getCapacity()
Description copied from interface:InterfaceLruCache
Returns cache capacity- Returns:
- capacity of cache
-
getEntry
protected InterfaceLruCacheEntry<V> getEntry(K key)
Returns LruCacheEntry mapped by key or null if it does not exist- Returns:
- LruCacheEntry
-
size
public int size()
Description copied from interface:InterfaceLruCache
Returns number of entries stored in cache (including invalid ones)- Returns:
- number of entries
-
put
public void put(K key, V value, long ttl)
Description copied from interface:InterfaceLruCache
Puts value under key into cache with desired TTL- Specified by:
put
in interfaceInterfaceLruCache<K,V>
ttl
- time to live in milliseconds
-
putEntry
protected void putEntry(K key, InterfaceLruCacheEntry<V> entry)
Puts entry into cache
-
remove
public V remove(K key)
Description copied from interface:InterfaceLruCache
Removes entry from cache (if exists)- Returns:
- the value if it still exists
-
forceClearOldest
public int forceClearOldest()
Description copied from interface:InterfaceLruCache
Removes all oldest entries from cache (ttl based)- Returns:
- the number of removed entries
-
contains
public boolean contains(K key)
Description copied from interface:InterfaceLruCache
Checks whether cache contains valid entry for key- Specified by:
contains
in interfaceInterfaceLruCache<K,V>
- Returns:
- true if cache contains key and entry is valid
-
createEntry
protected InterfaceLruCacheEntry<V> createEntry(V value, long ttl)
Creates new LruCacheEntry. It can be used to change implementation of LruCacheEntry - Parameters:
value
-ttl
-- Returns:
- LruCacheEntry
-
get
public V get(K key, java.util.concurrent.Callable<V> callback) throws VitamException
Description copied from interface:InterfaceLruCache
Tries to get element from cache. If get fails callback is used to create element and returned value is stored in cache. Default TTL is used- Specified by:
get
in interfaceInterfaceLruCache<K,V>
- Returns:
- Value
- Throws:
VitamException
-
get
public V get(K key, java.util.concurrent.Callable<V> callback, long ttl) throws VitamException
Description copied from interface:InterfaceLruCache
Tries to get element from cache. If get fails callback is used to create element and returned value is stored in cache- Specified by:
get
in interfaceInterfaceLruCache<K,V>
ttl
- time to live in milliseconds- Returns:
- Value
- Throws:
VitamException
-
getTtl
public long getTtl()
Description copied from interface:InterfaceLruCache
Returns cache TTL- Specified by:
getTtl
in interfaceInterfaceLruCache<K,V>
- Returns:
- ttl in milliseconds
-
setNewTtl
public void setNewTtl(long ttl)
Description copied from interface:InterfaceLruCache
Set a new TTL (for newly set objects only, not changing old values).- Specified by:
setNewTtl
in interfaceInterfaceLruCache<K,V>
-
updateTtl
public void updateTtl(K key)
Description copied from interface:InterfaceLruCache
Update the TTL of the associated object if it still exists- Specified by:
updateTtl
in interfaceInterfaceLruCache<K,V>
-
getValue
protected V getValue(K key)
Tries to retrieve value by it's key. Automatically removes entry if it's not valid (LruCacheEntry.getValue() returns null)- Parameters:
key
-- Returns:
- Value
-
isEmpty
public boolean isEmpty()
Description copied from interface:InterfaceLruCache
Checks whether cache is empty. If any entry exists (including invalid one) this method will return true- Specified by:
isEmpty
in interfaceInterfaceLruCache<K,V>
- Returns:
- true if no entries are stored in cache
-
put
public void put(K key, V value)
Description copied from interface:InterfaceLruCache
Puts value under key into cache. Default TTL is used- Specified by:
put
in interfaceInterfaceLruCache<K,V>
-
-