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.ObjectThreadsafe 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 intDEFAULT_INITIAL_CAPACITYInitial capacitystatic floatDEFAULT_LOAD_FACTORLoad 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 voidclear()Removes all entries from cachebooleancontains(K key)Checks whether cache contains valid entry for keyprotected InterfaceLruCacheEntry<V>createEntry(V value, long ttl)Creates new LruCacheEntry. intforceClearOldest()Removes all oldest entries from cache (ttl based)Vget(K key)Returns value cached with key.Vget(K key, java.util.concurrent.Callable<V> callback)Tries to get element from cache.Vget(K key, java.util.concurrent.Callable<V> callback, long ttl)Tries to get element from cache.intgetCapacity()Returns cache capacityprotected InterfaceLruCacheEntry<V>getEntry(K key)Returns LruCacheEntry mapped by key or null if it does not existlonggetTtl()Returns cache TTLprotected VgetValue(K key)Tries to retrieve value by it's key.booleanisEmpty()Checks whether cache is empty.voidput(K key, V value)Puts value under key into cache.voidput(K key, V value, long ttl)Puts value under key into cache with desired TTLprotected voidputEntry(K key, InterfaceLruCacheEntry<V> entry)Puts entry into cacheVremove(K key)Removes entry from cache (if exists)voidsetNewTtl(long ttl)Set a new TTL (for newly set objects only, not changing old values).intsize()Returns number of entries stored in cache (including invalid ones)voidupdateTtl(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:InterfaceLruCacheRemoves all entries from cache
-
get
public V get(K key)
Description copied from interface:InterfaceLruCacheReturns value cached with key.- Specified by:
getin 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:InterfaceLruCacheReturns 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:InterfaceLruCacheReturns 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:InterfaceLruCachePuts value under key into cache with desired TTL- Specified by:
putin 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:InterfaceLruCacheRemoves entry from cache (if exists)- Returns:
- the value if it still exists
-
forceClearOldest
public int forceClearOldest()
Description copied from interface:InterfaceLruCacheRemoves all oldest entries from cache (ttl based)- Returns:
- the number of removed entries
-
contains
public boolean contains(K key)
Description copied from interface:InterfaceLruCacheChecks whether cache contains valid entry for key- Specified by:
containsin 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 VitamExceptionDescription copied from interface:InterfaceLruCacheTries 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:
getin interfaceInterfaceLruCache<K,V>- Returns:
- Value
- Throws:
VitamException
-
get
public V get(K key, java.util.concurrent.Callable<V> callback, long ttl) throws VitamExceptionDescription copied from interface:InterfaceLruCacheTries to get element from cache. If get fails callback is used to create element and returned value is stored in cache- Specified by:
getin interfaceInterfaceLruCache<K,V>ttl- time to live in milliseconds- Returns:
- Value
- Throws:
VitamException
-
getTtl
public long getTtl()
Description copied from interface:InterfaceLruCacheReturns cache TTL- Specified by:
getTtlin interfaceInterfaceLruCache<K,V>- Returns:
- ttl in milliseconds
-
setNewTtl
public void setNewTtl(long ttl)
Description copied from interface:InterfaceLruCacheSet a new TTL (for newly set objects only, not changing old values).- Specified by:
setNewTtlin interfaceInterfaceLruCache<K,V>
-
updateTtl
public void updateTtl(K key)
Description copied from interface:InterfaceLruCacheUpdate the TTL of the associated object if it still exists- Specified by:
updateTtlin 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:InterfaceLruCacheChecks whether cache is empty. If any entry exists (including invalid one) this method will return true- Specified by:
isEmptyin interfaceInterfaceLruCache<K,V>- Returns:
- true if no entries are stored in cache
-
put
public void put(K key, V value)Description copied from interface:InterfaceLruCachePuts value under key into cache. Default TTL is used- Specified by:
putin interfaceInterfaceLruCache<K,V>
-
-