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 ExceptionPropagator |
DEFAULT_EXCEPTION_PROPAGATOR |
protected ExceptionPropagator |
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 CacheType |
keyType |
protected 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 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,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) |
<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) |
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.
|
CacheManager |
getCacheManager() |
InternalClock |
getClock()
Time reference for the cache.
|
CommonMetrics |
getCommonMetrics() |
String |
getCompleteName()
Returns name of the cache with manager name.
|
CacheEntry<K,V> |
getEntry(K key) |
protected Entry |
getEntryInternal(K key) |
String |
getEntryState(K key) |
InternalCacheInfo |
getInfo()
Generate cache statistics.
|
InternalCacheInfo |
getInfo(InternalCache _userCache) |
protected IntegrityState |
getIntegrityState() |
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() |
SimpleCacheEntry<K,V> |
getSimpleEntry(K key) |
int |
getTotalEntryCount() |
CacheType |
getValueType() |
protected boolean |
hasBackgroundRefresh() |
protected boolean |
hasKeepAfterExpired() |
protected boolean |
hasRejectNullValues() |
void |
init() |
protected void |
initializeHeapCache() |
void |
initWithoutTimerHandler() |
protected void |
insert(Entry<K,V> e,
V _value,
long t0,
long t,
byte _updateStatistics,
long _nextRefreshTime) |
protected Entry<K,V> |
insertNewEntry(K key,
int hc)
Insert new entry in all structures (hash and eviction).
|
protected void |
insertOrUpdateAndCalculateExpiry(Entry<K,V> e,
V v,
long t0,
long t,
byte _updateStatistics)
Calculate the next refresh time if a timer / expiry is needed and call insert.
|
<R> R |
invoke(K key,
EntryProcessor<K,V,R> entryProcessor)
Simply the
EntryAction based code to provide the entry processor. |
boolean |
isClosed() |
protected boolean |
isNoLastModificationTime() |
boolean |
isNullValuePermitted() |
ConcurrentEntryIterator<K,V> |
iterateAllHeapEntries()
Returns all cache entries within the heap cache.
|
Iterator<CacheEntry<K,V>> |
iterator() |
protected void |
load(Entry<K,V> e) |
void |
loadAll(Iterable<? extends K> _keys,
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) |
protected Entry |
lookupEntryNoHitRecord(K key) |
protected Entry |
lookupEntryNoHitRecord(K key,
int hc) |
protected Entry<K,V> |
lookupOrNewEntry(K key)
Lookup or create a new entry.
|
protected Entry<K,V> |
lookupOrNewEntry(K key,
int hc) |
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) |
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.
|
SimpleCacheEntry<K,V> |
peekSimpleEntry(K key) |
void |
prefetch(K key) |
void |
prefetchAll(Iterable<? extends K> _keys,
CacheOperationCompletionListener l) |
void |
put(K key,
V value) |
void |
putAll(Map<? extends K,? extends V> valueMap) |
boolean |
putIfAbsent(K key,
V value) |
protected void |
recordHit(Entry e)
Increment the hit counter, because entry was accessed.
|
void |
reloadAll(Iterable<? extends K> _keys,
CacheOperationCompletionListener l) |
void |
remove(K key) |
protected boolean |
removeEntry(Entry e)
Remove the entry from the hash and the replacement list.
|
boolean |
removeEntryForEviction(Entry<K,V> e)
Remove the entry from the hash table.
|
boolean |
removeIfEquals(K key,
V _value) |
boolean |
removeWithFlag(K key,
boolean _checkValue,
V _value)
Remove the object from the cache.
|
protected Entry<K,V> |
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) |
CacheEntry<K,V> |
replaceOrGet(K key,
V _oldValue,
V _newValue,
CacheEntry<K,V> _dummyEntry)
Used by JCache impl, since access needs to trigger the TTI maybe use EP instead?
|
static <K,V> CacheEntry<K,V> |
returnCacheEntry(ExaminationEntry<K,V> _entry) |
static <K,V> CacheEntry<K,V> |
returnCacheEntry(K _key,
V _valueOrException,
long _lastModification) |
protected CacheEntry<K,V> |
returnEntry(ExaminationEntry<K,V> e)
Wrap entry in a separate object instance.
|
RuntimeException |
returnNullValueDetectedException() |
static <K,V> SimpleCacheEntry<K,V> |
returnSimpleEntry(Entry<K,V> _entry)
Wrap the cache entry into a new entry object and return it.
|
protected V |
returnValue(Entry<K,V> e) |
protected V |
returnValue(V v) |
void |
setAdvancedLoader(AdvancedCacheLoader<K,V> al) |
void |
setCacheClosedListeners(Collection<CustomizationSupplier<CacheClosedListener>> l) |
void |
setCacheConfig(Cache2kConfiguration c)
called from CacheBuilder
|
void |
setCacheManager(CacheManagerImpl cm) |
void |
setClock(InternalClock _clock) |
void |
setExceptionPropagator(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) |
String |
toString()
Return status information.
|
asMap, clearAndClose, closeCustomization, createCustomization, entries, execute, execute, execute, getStorage, getStorageMetrics, invokeAll, keys, removeAll, removeAll, requestInterface
public static final HeapCache.Tunable TUNABLE
public static final ExceptionPropagator DEFAULT_EXCEPTION_PROPAGATOR
protected final int hashSeed
protected String name
public CacheManagerImpl manager
protected AdvancedCacheLoader<K,V> loader
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 CacheType keyType
protected CacheType valueType
protected ExceptionPropagator exceptionPropagator
protected final boolean hasKeepAfterExpired()
protected final boolean hasRejectNullValues()
public final boolean isNullValuePermitted()
protected final boolean hasBackgroundRefresh()
protected final boolean isNoLastModificationTime()
protected final void setFeatureBit(int _bitmask, boolean _flag)
public String getCompleteName()
public Log getLog()
public void setCacheConfig(Cache2kConfiguration c)
public void setTiming(TimingHandler<K,V> rh)
public void setClock(InternalClock _clock)
public void setExceptionPropagator(ExceptionPropagator ep)
public void setAdvancedLoader(AdvancedCacheLoader<K,V> al)
public void setName(String n)
public String getName()
public void setCacheManager(CacheManagerImpl cm)
public CacheType getKeyType()
public 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<CustomizationSupplier<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 CacheEntry<K,V> returnEntry(ExaminationEntry<K,V> e)
public static <K,V> CacheEntry<K,V> returnCacheEntry(ExaminationEntry<K,V> _entry)
public static <K,V> CacheEntry<K,V> returnCacheEntry(K _key, V _valueOrException, long _lastModification)
public CacheEntry<K,V> getEntry(K key)
public static <K,V> SimpleCacheEntry<K,V> returnSimpleEntry(Entry<K,V> _entry)
public SimpleCacheEntry<K,V> getSimpleEntry(K key)
getSimpleEntry
in interface Cache<K,V>
getSimpleEntry
in class BaseCache<K,V>
public SimpleCacheEntry<K,V> peekSimpleEntry(K key)
peekSimpleEntry
in interface Cache<K,V>
peekSimpleEntry
in class BaseCache<K,V>
protected void finishLoadOrEviction(Entry e, long _nextRefreshTime)
protected boolean removeEntry(Entry e)
public CacheEntry<K,V> replaceOrGet(K key, V _oldValue, V _newValue, CacheEntry<K,V> _dummyEntry)
InternalCache
protected Entry<K,V> 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 CacheEntry<K,V> peekEntry(K key)
public V computeIfAbsent(K key, Callable<V> callable)
Cache.get(Object)
public boolean containsAndRemove(K key)
public boolean removeWithFlag(K key, boolean _checkValue, V _value)
public void remove(K key)
public Executor getPrefetchExecutor()
public void prefetch(K key)
public void prefetchAll(Iterable<? extends K> _keys, CacheOperationCompletionListener l)
public void loadAll(Iterable<? extends K> _keys, CacheOperationCompletionListener l)
public void reloadAll(Iterable<? extends K> _keys, CacheOperationCompletionListener l)
protected void loadAndReplace(K key)
protected Entry<K,V> lookupOrNewEntry(K key)
protected Entry<K,V> insertNewEntry(K key, int hc)
public boolean 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, byte _updateStatistics)
public RuntimeException returnNullValueDetectedException()
protected final void insert(Entry<K,V> e, V _value, long t0, long t, 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)
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, 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()
protected IntegrityState getIntegrityState()
public final void checkIntegrity()
public final InternalCacheInfo getInfo()
InternalCache
public final InternalCacheInfo getLatestInfo()
InternalCache
InternalCache.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()
InternalCache
public CacheManager getCacheManager()
public String toString()
public final int modifiedHash(int h)
cache2k API documentation. Copyright © 2000–2018 headissue GmbH, Munich.