public class HeapCache<K,V> extends BaseCache<K,V>
Locking: The cache has a single structure lock obtained via lock and also
locks on each entry for operations on it. Though, mutation operations that happen on a
single entry get serialized.
| Modifier and Type | Class and Description |
|---|---|
static class |
HeapCache.RunWithCatch |
static class |
HeapCache.Tunable |
| Modifier and Type | Field and Description |
|---|---|
protected long |
clearCnt
Number of clear operations.
|
protected long |
clearedTime |
protected long |
clearRemovedCnt
Number of entries removed by clear.
|
protected InternalClock |
clock |
static org.cache2k.integration.ExceptionPropagator |
DEFAULT_EXCEPTION_PROPAGATOR |
protected org.cache2k.integration.ExceptionPropagator<K> |
exceptionPropagator |
protected Hash2<K,V> |
hash |
protected int |
hashSeed |
protected org.cache2k.core.CacheBaseInfo |
info
Statistics
|
protected long |
internalExceptionCnt
Number of internal exceptions.
|
protected long |
keyMutationCnt
Counts the number of key mutations.
|
protected org.cache2k.configuration.CacheType |
keyType |
protected org.cache2k.integration.AdvancedCacheLoader<K,V> |
loader |
protected Executor |
loaderExecutor |
Object |
lock
Structure lock of the cache.
|
CacheManagerImpl |
manager |
protected String |
name |
protected Executor |
prefetchExecutor |
protected long |
startedTime |
protected TimingHandler<K,V> |
timing |
static HeapCache.Tunable |
TUNABLE |
protected org.cache2k.configuration.CacheType |
valueType |
| Constructor and Description |
|---|
HeapCache() |
| Modifier and Type | Method and Description |
|---|---|
void |
cancelTimerJobs()
Preparation for shutdown.
|
Set<K> |
checkAllPresent(Iterable<? extends K> keys) |
void |
checkClosed() |
void |
checkIntegrity()
Check internal data structures and throw and exception if something is wrong, used for unit testing
|
void |
clear() |
void |
clearLocalCache() |
void |
close() |
void |
closePart1() |
void |
closePart2(InternalCache _userCache) |
V |
computeIfAbsent(K key,
Callable<V> callable)
Code duplicates with
Cache.get(Object) |
boolean |
containsAndRemove(K key) |
boolean |
containsKey(K key) |
Map<K,V> |
convertCacheEntry2ValueMap(Map<K,org.cache2k.CacheEntry<K,V>> _map) |
Map<K,V> |
convertValueMap(Map<K,ExaminationEntry<K,V>> _map) |
protected <R> EntryAction<K,V,R> |
createEntryAction(K key,
Entry<K,V> e,
Semantic<K,V,R> op) |
Hash2<K,V> |
createHashTable() |
<T> T |
executeWithGlobalLock(Job<T> job)
Execute job while making sure that no other operations are going on.
|
void |
expireAt(K key,
long _millis) |
protected void |
expireEntry(Entry e) |
void |
expireOrScheduleFinalExpireEvent(Entry<K,V> e) |
K |
extractIntKeyObj(K key)
The key object or null, for integer keyed caches
|
int |
extractIntKeyValue(K key,
int hc)
Modified hash code or integer value for integer keyed caches
|
K |
extractKeyObj(Entry<K,V> e) |
int |
extractModifiedHash(Entry e) |
protected void |
finishLoadOrEviction(Entry e,
long _nextRefreshTime) |
Set<K> |
generateKeySet(Iterable<? extends K> _keys) |
V |
get(K key) |
Map<K,V> |
getAll(Iterable<? extends K> _inputKeys)
JSR107 bulk interface.
|
org.cache2k.CacheManager |
getCacheManager() |
InternalClock |
getClock()
Time reference for the cache.
|
CommonMetrics |
getCommonMetrics() |
String |
getCompleteName()
Returns name of the cache with manager name.
|
org.cache2k.CacheEntry<K,V> |
getEntry(K key) |
protected Entry |
getEntryInternal(K key) |
protected Entry<K,V> |
getEntryInternal(K key,
int hc,
int val) |
String |
getEntryState(K key) |
InternalCacheInfo |
getInfo()
Generate cache statistics.
|
InternalCacheInfo |
getInfo(InternalCache _userCache) |
protected IntegrityState |
getIntegrityState() |
org.cache2k.configuration.CacheType |
getKeyType() |
InternalCacheInfo |
getLatestInfo()
Generate fresh statistics.
|
InternalCacheInfo |
getLatestInfo(InternalCache _userCache) |
long |
getLocalSize() |
Log |
getLog()
Normally a cache itself logs nothing, so just construct when needed.
|
String |
getName() |
Executor |
getPrefetchExecutor() |
int |
getTotalEntryCount()
This method is used for
ConcurrentMapWrapper.size() |
org.cache2k.configuration.CacheType |
getValueType() |
void |
init() |
protected void |
initializeHeapCache() |
void |
initWithoutTimerHandler() |
protected void |
insert(Entry<K,V> e,
V _value,
long t0,
long t,
long _refreshTime,
byte _updateStatistics,
long _nextRefreshTime) |
protected Entry<K,V> |
insertNewEntry(K key,
int hc,
int val)
Insert new entry in all structures (hash and eviction).
|
protected void |
insertOrUpdateAndCalculateExpiry(Entry<K,V> e,
V v,
long t0,
long t,
long _refreshTime,
byte _updateStatistics)
Calculate the next refresh time if a timer / expiry is needed and call insert.
|
<R> R |
invoke(K key,
org.cache2k.processor.EntryProcessor<K,V,R> entryProcessor)
Simply the
EntryAction based code to provide the entry processor. |
boolean |
isClosed() |
protected boolean |
isKeepAfterExpired() |
boolean |
isNullValuePermitted() |
protected boolean |
isRecordRefreshTime() |
protected boolean |
isRefreshAhead() |
protected boolean |
isRejectNullValues() |
protected boolean |
isUpdateTimeNeeded()
Don't update the entry last modification time and no expiry calculations are needed.
|
ConcurrentEntryIterator<K,V> |
iterateAllHeapEntries()
Returns all cache entries within the heap cache.
|
Iterator<org.cache2k.CacheEntry<K,V>> |
iterator() |
protected void |
load(Entry<K,V> e) |
void |
loadAll(Iterable<? extends K> _keys,
org.cache2k.CacheOperationCompletionListener l) |
protected void |
loadAndReplace(K key)
Always fetch the value from the source.
|
void |
logAndCountInternalException(String _text,
Throwable _exception) |
protected Entry<K,V> |
lookupEntry(K key) |
protected Entry<K,V> |
lookupEntry(K key,
int hc,
int val) |
protected Entry<K,V> |
lookupEntryNoHitRecord(K key) |
protected Entry<K,V> |
lookupEntryNoHitRecord(K key,
int hc,
int val) |
protected Entry<K,V> |
lookupOrNewEntry(K key)
Lookup or create a new entry.
|
protected Entry<K,V> |
lookupOrNewEntry(K key,
int hc,
int val) |
protected Entry<K,V> |
lookupOrNewEntryNoHitRecord(K key) |
int |
modifiedHash(int h)
This function calculates a modified hash code.
|
V |
peek(K key) |
Map<K,V> |
peekAll(Iterable<? extends K> _inputKeys) |
V |
peekAndPut(K key,
V _value) |
V |
peekAndRemove(K key) |
V |
peekAndReplace(K key,
V _value) |
org.cache2k.CacheEntry<K,V> |
peekEntry(K key) |
protected Entry<K,V> |
peekEntryInternal(K key)
Return the entry, if it is in the cache, without invoking the
cache source.
|
protected Entry<K,V> |
peekEntryInternal(K key,
int hc,
int val) |
void |
prefetch(K key) |
void |
prefetchAll(Iterable<? extends K> _keys,
org.cache2k.CacheOperationCompletionListener l) |
void |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> valueMap) |
boolean |
putIfAbsent(K key,
V value) |
protected void |
putValue(Entry e,
V _value)
Update the value directly within entry lock.
|
protected void |
recordHit(Entry e)
Increment the hit counter, because entry was accessed.
|
void |
reloadAll(Iterable<? extends K> _keys,
org.cache2k.CacheOperationCompletionListener l) |
void |
remove(K key) |
protected boolean |
removeEntry(Entry e)
Remove the entry from the hash and the replacement list.
|
void |
removeEntryForEviction(Entry<K,V> e)
Remove the entry from the hash table.
|
boolean |
removeIfEquals(K key,
V _value)
Remove the object from the cache.
|
protected boolean |
replace(K key,
boolean _compare,
V _oldValue,
V _newValue)
replace if value matches.
|
boolean |
replace(K key,
V _newValue) |
boolean |
replaceIfEquals(K key,
V _oldValue,
V _newValue) |
org.cache2k.CacheEntry<K,V> |
returnCacheEntry(ExaminationEntry<K,V> _entry) |
org.cache2k.CacheEntry<K,V> |
returnCacheEntry(K key,
V _valueOrException) |
protected org.cache2k.CacheEntry<K,V> |
returnEntry(ExaminationEntry<K,V> e)
Wrap entry in a separate object instance.
|
RuntimeException |
returnNullValueDetectedException() |
protected V |
returnValue(Entry<K,V> e) |
protected V |
returnValue(V v) |
void |
setAdvancedLoader(org.cache2k.integration.AdvancedCacheLoader<K,V> al) |
void |
setCacheClosedListeners(Collection<org.cache2k.configuration.CustomizationSupplier<org.cache2k.event.CacheClosedListener>> l) |
void |
setCacheConfig(org.cache2k.configuration.Cache2kConfiguration c)
called from CacheBuilder
|
void |
setCacheManager(CacheManagerImpl cm) |
void |
setClock(InternalClock _clock) |
void |
setExceptionPropagator(org.cache2k.integration.ExceptionPropagator ep) |
protected void |
setFeatureBit(int _bitmask,
boolean _flag) |
void |
setName(String n)
Set the name and configure a logging, used within cache construction.
|
void |
setTiming(TimingHandler<K,V> rh) |
void |
startRefreshProbationTimer(Entry<K,V> e,
long _nextRefreshTime) |
void |
timerEventExpireEntry(Entry<K,V> e) |
void |
timerEventProbationTerminated(Entry<K,V> e) |
void |
timerEventRefresh(Entry<K,V> e) |
asMap, clearAndClose, closeCustomization, createCustomization, entries, execute, execute, execute, getStorage, invokeAll, keys, nameQualifier, removeAll, removeAll, requestInterface, toStringpublic static final HeapCache.Tunable TUNABLE
public static final org.cache2k.integration.ExceptionPropagator DEFAULT_EXCEPTION_PROPAGATOR
protected final int hashSeed
protected String name
public CacheManagerImpl manager
protected InternalClock clock
protected TimingHandler<K,V> timing
public final Object lock
protected org.cache2k.core.CacheBaseInfo info
protected volatile long keyMutationCnt
protected long clearedTime
protected long startedTime
protected long clearRemovedCnt
protected long clearCnt
protected long internalExceptionCnt
protected volatile Executor loaderExecutor
protected volatile Executor prefetchExecutor
protected org.cache2k.configuration.CacheType keyType
protected org.cache2k.configuration.CacheType valueType
protected org.cache2k.integration.ExceptionPropagator<K> exceptionPropagator
protected final boolean isKeepAfterExpired()
protected final boolean isRejectNullValues()
public final boolean isNullValuePermitted()
protected final boolean isRefreshAhead()
protected final boolean isUpdateTimeNeeded()
protected final boolean isRecordRefreshTime()
protected final void setFeatureBit(int _bitmask,
boolean _flag)
public String getCompleteName()
similarpublic Log getLog()
public void setCacheConfig(org.cache2k.configuration.Cache2kConfiguration c)
public void setTiming(TimingHandler<K,V> rh)
public void setClock(InternalClock _clock)
public void setExceptionPropagator(org.cache2k.integration.ExceptionPropagator ep)
public void setAdvancedLoader(org.cache2k.integration.AdvancedCacheLoader<K,V> al)
public void setName(String n)
public String getName()
public void setCacheManager(CacheManagerImpl cm)
public org.cache2k.configuration.CacheType getKeyType()
public org.cache2k.configuration.CacheType getValueType()
public void init()
public void initWithoutTimerHandler()
public void checkClosed()
public final void clear()
public final void clearLocalCache()
protected void initializeHeapCache()
public void cancelTimerJobs()
public boolean isClosed()
public void closePart1()
throws CacheClosedException
CacheClosedException - if cache is closed or closing is initiated by another thread.public void close()
public void closePart2(InternalCache _userCache)
public void setCacheClosedListeners(Collection<org.cache2k.configuration.CustomizationSupplier<org.cache2k.event.CacheClosedListener>> l)
protected void recordHit(Entry e)
The hit counter is a dirty counter. In case of multiple CPU cores incrementing the same entry counter, increments will be lost. For the functionality of the eviction algorithm this is not a real loss, since still the most accessed entries will have more counts then the others. On 32 bit systems word tearing may occur. This will also have no real observable negative impact on the eviction, so we do not compensate for it.
The hit count is also used for access statistics. The dirty counting will effect the exact correctness of the access statistics.
Using a 64 bit counter per entry is basically a big waste of memory. When reducing to a 32 bit value is has approximately a negative performance impact of 30%.
protected org.cache2k.CacheEntry<K,V> returnEntry(ExaminationEntry<K,V> e)
public org.cache2k.CacheEntry<K,V> returnCacheEntry(ExaminationEntry<K,V> _entry)
protected void finishLoadOrEviction(Entry e, long _nextRefreshTime)
protected boolean removeEntry(Entry e)
protected final void putValue(Entry e, V _value)
protected boolean replace(K key, boolean _compare, V _oldValue, V _newValue)
protected final Entry<K,V> peekEntryInternal(K key)
The cache storage is asked whether the entry is present. If the entry is not present, this result is cached in the local cache.
public boolean containsKey(K key)
public V computeIfAbsent(K key, Callable<V> callable)
Cache.get(Object)public boolean containsAndRemove(K key)
public void remove(K key)
public Executor getPrefetchExecutor()
public void prefetch(K key)
public void prefetchAll(Iterable<? extends K> _keys, org.cache2k.CacheOperationCompletionListener l)
public void loadAll(Iterable<? extends K> _keys, org.cache2k.CacheOperationCompletionListener l)
public void reloadAll(Iterable<? extends K> _keys, org.cache2k.CacheOperationCompletionListener l)
protected void loadAndReplace(K key)
protected Entry<K,V> lookupOrNewEntry(K key)
protected Entry<K,V> insertNewEntry(K key, int hc, int val)
public void removeEntryForEviction(Entry<K,V> e)
With completion of the method the entry content is no more visible. "Nulling" out the key or value of the entry is incorrect, since there can be another thread which is just about to return the entry contents.
protected final void insertOrUpdateAndCalculateExpiry(Entry<K,V> e, V v, long t0, long t, long _refreshTime, byte _updateStatistics)
public RuntimeException returnNullValueDetectedException()
protected final void insert(Entry<K,V> e, V _value, long t0, long t, long _refreshTime, byte _updateStatistics, long _nextRefreshTime)
public void startRefreshProbationTimer(Entry<K,V> e, long _nextRefreshTime)
public void logAndCountInternalException(String _text, Throwable _exception)
protected void expireEntry(Entry e)
public final ConcurrentEntryIterator<K,V> iterateAllHeapEntries()
public Map<K,V> getAll(Iterable<? extends K> _inputKeys)
public Map<K,V> convertCacheEntry2ValueMap(Map<K,org.cache2k.CacheEntry<K,V>> _map)
protected <R> EntryAction<K,V,R> createEntryAction(K key, Entry<K,V> e, Semantic<K,V,R> op)
createEntryAction in class BaseCache<K,V>public <R> R invoke(K key, org.cache2k.processor.EntryProcessor<K,V,R> entryProcessor)
EntryAction based code to provide the entry processor. If we code it directly this
might be a little bit more efficient, but it gives quite a code bloat which has lots of
corner cases for loader and exception handling.public void expireAt(K key, long _millis)
public final long getLocalSize()
public final int getTotalEntryCount()
InternalCacheConcurrentMapWrapper.size()protected IntegrityState getIntegrityState()
public final void checkIntegrity()
public final InternalCacheInfo getInfo()
InternalCachepublic final InternalCacheInfo getLatestInfo()
InternalCacheInternalCache.getInfo() method for requesting information for monitoring.public final InternalCacheInfo getInfo(InternalCache _userCache)
public final InternalCacheInfo getLatestInfo(InternalCache _userCache)
public CommonMetrics getCommonMetrics()
public <T> T executeWithGlobalLock(Job<T> job)
public final InternalClock getClock()
InternalCachepublic org.cache2k.CacheManager getCacheManager()
public int modifiedHash(int h)
public int extractIntKeyValue(K key, int hc)
public int extractModifiedHash(Entry e)
cache2k API documentation. Copyright © 2000–2019 headissue GmbH, Munich.