Package org.apache.jackrabbit.core.cache
Class ConcurrentCache<K,V>
- java.lang.Object
-
- org.apache.jackrabbit.core.cache.AbstractCache
-
- org.apache.jackrabbit.core.cache.ConcurrentCache<K,V>
-
- All Implemented Interfaces:
Cache
public class ConcurrentCache<K,V> extends AbstractCache
Concurrent cache implementation that uses cache segments to minimize the chance of lock contention. The LRU algorithm is used to evict excess entries from each cache segment separately, which makes the combined eviction algorithm similar but not exactly the same as LRU. None of the methods of this class are synchronized, but they are all thread-safe.
-
-
Constructor Summary
Constructors Constructor Description ConcurrentCache(String name)ConcurrentCache(String name, int numberOfSegments)
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclear()Clears all segments of the cache.booleancontainsKey(K key)Checks if the identified entry is cached.Vget(K key)Returns the identified cache entry.longgetElementCount()Get the number of elements/objects in the cache.booleanisEmpty()Checks if the cache size is zero.Vput(K key, V value, long size)Adds the given entry to the cache.Vremove(K key)Removes the identified entry from the cache.voidsetMaxMemorySize(long size)Sets the maximum size of the cache and evicts any excess items until the current size falls within the given limit.StringtoString()List<V>values()Returns all values in the cache.-
Methods inherited from class org.apache.jackrabbit.core.cache.AbstractCache
dispose, getAccessCount, getCacheInfoAsString, getMaxMemorySize, getMemoryUsed, getMissCount, getTotalAccessCount, isTooBig, recordCacheAccess, recordCacheMiss, recordSizeChange, resetAccessCount, resetMissCount, setAccessListener
-
-
-
-
Method Detail
-
containsKey
public boolean containsKey(K key)
Checks if the identified entry is cached.- Parameters:
key- entry key- Returns:
trueif the entry is cached,falseotherwise
-
get
public V get(K key)
Returns the identified cache entry.- Parameters:
key- entry key- Returns:
- entry value, or
nullif not found
-
values
public List<V> values()
Returns all values in the cache. Note that this method is not synchronized over the entire cache, so it is only guaranteed to return accurate results when there are no concurrent threads modifying the cache.- Returns:
- cached values
-
put
public V put(K key, V value, long size)
Adds the given entry to the cache.- Parameters:
key- entry keyvalue- entry valuesize- entry size- Returns:
- the previous value, or
null
-
remove
public V remove(K key)
Removes the identified entry from the cache.- Parameters:
key- entry key- Returns:
- removed entry, or
nullif not found
-
clear
public void clear()
Clears all segments of the cache. Note that even this method is not synchronized over the entire cache, so it needs to explicitly count the cache size changes and may return with a non-empty cache if other threads have concurrently been adding new entries.
-
isEmpty
public boolean isEmpty()
Checks if the cache size is zero.
-
setMaxMemorySize
public void setMaxMemorySize(long size)
Sets the maximum size of the cache and evicts any excess items until the current size falls within the given limit.- Specified by:
setMaxMemorySizein interfaceCache- Overrides:
setMaxMemorySizein classAbstractCache- Parameters:
size- the size in bytes
-
getElementCount
public long getElementCount()
Description copied from interface:CacheGet the number of elements/objects in the cache.- Returns:
- the number of elements
-
-