Package org.cache2k.core
Class WiredCache<K,V>
- java.lang.Object
-
- org.cache2k.core.BaseCache<K,V>
-
- org.cache2k.core.WiredCache<K,V>
-
- All Implemented Interfaces:
AutoCloseable
,org.cache2k.Cache<K,V>
,InternalCache<K,V>
,InternalCacheCloseContext
,InternalEvictionListener<K,V>
,TimerEventListener<K,V>
,org.cache2k.DataAware<K,V>
,org.cache2k.KeyValueSource<K,V>
public class WiredCache<K,V> extends BaseCache<K,V> implements InternalEvictionListener<K,V>
A cache implementation that builds on a heap cache and coordinates with additional attachments like storage, listeners and a writer.- Author:
- Jens Wilke
-
-
Field Summary
-
Fields inherited from interface org.cache2k.core.eviction.InternalEvictionListener
NO_OPERATION
-
-
Constructor Summary
Constructors Constructor Description WiredCache()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
cancelTimerJobs()
used from the cache manager for shutdownvoid
checkIntegrity()
Cache checks its internal integrity.void
clear()
void
close()
V
computeIfAbsent(K key, Function<? super K,? extends V> function)
boolean
containsAndRemove(K key)
boolean
containsKey(K key)
protected <R> EntryAction<K,V,R>
createEntryAction(K key, Entry<K,V> e, Semantic<K,V,R> op)
protected <R> org.cache2k.core.WiredCache.MyEntryAction<R>
createFireAndForgetAction(Entry<K,V> e, Semantic<K,V,R> op)
V
get(K key)
Map<K,V>
getAll(Iterable<? extends K> requestedKeys)
This takes four different execution paths depending on cache setup and state: no loader and/or all data present in heap, async or async bulk, parallel single load, bulk load.org.cache2k.CacheManager
getCacheManager()
The cache managerorg.cache2k.operation.TimeReference
getClock()
Time reference for the cache.CommonMetrics
getCommonMetrics()
InternalCacheInfo
getConsistentInfo()
Generate fresh statistics within a global cache lock.org.cache2k.CacheEntry<K,V>
getEntry(K key)
String
getEntryState(K key)
Eviction
getEviction()
Executor
getExecutor()
HeapCache
getHeapCache()
For testingInternalCacheInfo
getInfo()
Return cache statistic counters.org.cache2k.config.CacheType
getKeyType()
Log
getLog()
used from the cache managerString
getName()
Designation of target for logging and exceptions.org.cache2k.operation.TimeReference
getTimeReference()
Timing
getTiming()
long
getTotalEntryCount()
This method is used forConcurrentMapWrapper.size()
org.cache2k.Cache<K,V>
getUserCache()
Cache used by user, eventually wrapped.org.cache2k.config.CacheType
getValueType()
void
init()
<@Nullable R>
Map<K,org.cache2k.processor.EntryProcessingResult<R>>invokeAll(Iterable<? extends K> keys, org.cache2k.processor.EntryProcessor<K,V,@Nullable R> entryProcessor)
boolean
isClosed()
boolean
isDisabled()
boolean
isLoaderPresent()
boolean
isNullValuePermitted()
boolean
isWeigherPresent()
Iterator<org.cache2k.CacheEntry<K,V>>
iterator()
CompletableFuture<Void>
loadAll(Iterable<? extends K> keys)
void
logAndCountInternalException(String s, Throwable t)
void
onEvictionFromHeap(Entry<K,V> e)
Calls eviction listeners.V
peek(K key)
Map<K,V>
peekAll(Iterable<? extends K> keys)
We need to deal with possible null values and exceptions.V
peekAndPut(K key, V value)
V
peekAndRemove(K key)
V
peekAndReplace(K key, V value)
org.cache2k.CacheEntry<K,V>
peekEntry(K key)
void
put(K key, V value)
void
putAll(Map<? extends K,? extends V> m)
boolean
putIfAbsent(K key, V value)
CompletableFuture<Void>
reloadAll(Iterable<? extends K> keys)
void
remove(K key)
boolean
removeIfEquals(K key, V value)
boolean
replace(K key, V newValue)
boolean
replaceIfEquals(K key, V oldValue, V newValue)
org.cache2k.CacheEntry<K,V>
returnCacheEntry(ExaminationEntry<K,V> e)
void
setDisabled(boolean f)
void
timerEventExpireEntry(Entry<K,V> e, Object task)
If not expired yet, negate time to enforce time checks, schedule task for expiry otherwise.void
timerEventProbationTerminated(Entry<K,V> e, Object task)
void
timerEventRefresh(Entry<K,V> e, Object task)
Starts a refresh operation or expires if no threads in the loader thread pool are available.-
Methods inherited from class org.cache2k.core.BaseCache
asMap, closeCustomization, entries, execute, execute, expireAt, getQualifiedName, invoke, keys, removeAll, removeAll, requestInterface, toString
-
-
-
-
Method Detail
-
getLog
public Log getLog()
Description copied from interface:InternalCache
used from the cache manager- Specified by:
getLog
in interfaceInternalCache<K,V>
-
getHeapCache
public HeapCache getHeapCache()
For testing
-
getClock
public org.cache2k.operation.TimeReference getClock()
Description copied from interface:InternalCache
Time reference for the cache.- Specified by:
getClock
in interfaceInternalCache<K,V>
-
isNullValuePermitted
public boolean isNullValuePermitted()
- Specified by:
isNullValuePermitted
in interfaceInternalCache<K,V>
-
getName
public String getName()
Description copied from interface:TimerEventListener
Designation of target for logging and exceptions.- Specified by:
getName
in interfaceorg.cache2k.Cache<K,V>
- Specified by:
getName
in interfaceInternalCacheCloseContext
- Specified by:
getName
in interfaceTimerEventListener<K,V>
-
getKeyType
public org.cache2k.config.CacheType getKeyType()
- Specified by:
getKeyType
in interfaceInternalCache<K,V>
-
getValueType
public org.cache2k.config.CacheType getValueType()
- Specified by:
getValueType
in interfaceInternalCache<K,V>
-
getCacheManager
public org.cache2k.CacheManager getCacheManager()
Description copied from interface:InternalCacheCloseContext
The cache manager- Specified by:
getCacheManager
in interfaceorg.cache2k.Cache<K,V>
- Specified by:
getCacheManager
in interfaceInternalCacheCloseContext
-
containsKey
public boolean containsKey(K key)
-
containsAndRemove
public boolean containsAndRemove(K key)
-
loadAll
public CompletableFuture<Void> loadAll(Iterable<? extends K> keys)
-
reloadAll
public CompletableFuture<Void> reloadAll(Iterable<? extends K> keys)
-
createFireAndForgetAction
protected <R> org.cache2k.core.WiredCache.MyEntryAction<R> createFireAndForgetAction(Entry<K,V> e, Semantic<K,V,R> op)
- Specified by:
createFireAndForgetAction
in classBaseCache<K,V>
-
getExecutor
public Executor getExecutor()
- Specified by:
getExecutor
in classBaseCache<K,V>
-
isWeigherPresent
public boolean isWeigherPresent()
- Specified by:
isWeigherPresent
in interfaceInternalCache<K,V>
-
isLoaderPresent
public boolean isLoaderPresent()
- Specified by:
isLoaderPresent
in interfaceInternalCache<K,V>
-
isDisabled
public boolean isDisabled()
- Specified by:
isDisabled
in interfaceInternalCache<K,V>
-
setDisabled
public void setDisabled(boolean f)
- Specified by:
setDisabled
in interfaceInternalCache<K,V>
-
getAll
public Map<K,V> getAll(Iterable<? extends K> requestedKeys)
This takes four different execution paths depending on cache setup and state: no loader and/or all data present in heap, async or async bulk, parallel single load, bulk load.
-
getTotalEntryCount
public long getTotalEntryCount()
Description copied from interface:InternalCache
This method is used forConcurrentMapWrapper.size()
- Specified by:
getTotalEntryCount
in interfaceInternalCache<K,V>
-
invokeAll
public <@Nullable R> Map<K,org.cache2k.processor.EntryProcessingResult<R>> invokeAll(Iterable<? extends K> keys, org.cache2k.processor.EntryProcessor<K,V,@Nullable R> entryProcessor)
-
peekAll
public Map<K,V> peekAll(Iterable<? extends K> keys)
We need to deal with possible null values and exceptions. This is a simple placeholder implementation that covers it all by working on the entry.
-
getConsistentInfo
public InternalCacheInfo getConsistentInfo()
Description copied from interface:InternalCache
Generate fresh statistics within a global cache lock. This version is used by internal consistency tests. This method is not intended to be called at high frequencies or for attaching monitoring or logging. Use theInternalCache.getInfo()
method for requesting information for monitoring.- Specified by:
getConsistentInfo
in interfaceInternalCache<K,V>
-
getInfo
public InternalCacheInfo getInfo()
Description copied from interface:InternalCache
Return cache statistic counters. This method is intended for regular statistics polling. No extensive locking is performed to extract a consistent set of counters.- Specified by:
getInfo
in interfaceInternalCache<K,V>
-
getCommonMetrics
public CommonMetrics getCommonMetrics()
- Specified by:
getCommonMetrics
in interfaceInternalCache<K,V>
-
logAndCountInternalException
public void logAndCountInternalException(String s, Throwable t)
- Specified by:
logAndCountInternalException
in interfaceInternalCache<K,V>
-
checkIntegrity
public void checkIntegrity()
Description copied from interface:InternalCache
Cache checks its internal integrity. This is a expansive operation because it may traverse all cache entries. Used for testing.- Specified by:
checkIntegrity
in interfaceInternalCache<K,V>
-
init
public void init()
-
cancelTimerJobs
public void cancelTimerJobs()
Description copied from interface:InternalCache
used from the cache manager for shutdown- Specified by:
cancelTimerJobs
in interfaceInternalCache<K,V>
-
close
public void close()
- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceorg.cache2k.Cache<K,V>
-
returnCacheEntry
public org.cache2k.CacheEntry<K,V> returnCacheEntry(ExaminationEntry<K,V> e)
- Specified by:
returnCacheEntry
in interfaceInternalCache<K,V>
-
getEviction
public Eviction getEviction()
- Specified by:
getEviction
in interfaceInternalCache<K,V>
-
onEvictionFromHeap
public void onEvictionFromHeap(Entry<K,V> e)
Calls eviction listeners.- Specified by:
onEvictionFromHeap
in interfaceInternalEvictionListener<K,V>
-
createEntryAction
protected <R> EntryAction<K,V,R> createEntryAction(K key, Entry<K,V> e, Semantic<K,V,R> op)
- Specified by:
createEntryAction
in classBaseCache<K,V>
-
getEntryState
public String getEntryState(K key)
- Specified by:
getEntryState
in interfaceInternalCache<K,V>
-
timerEventExpireEntry
public void timerEventExpireEntry(Entry<K,V> e, Object task)
If not expired yet, negate time to enforce time checks, schedule task for expiry otherwise. Semantics double withHeapCache.timerEventExpireEntry(Entry, Object)
- Specified by:
timerEventExpireEntry
in interfaceTimerEventListener<K,V>
task
- timer task as returned byEntry.getTask()
to check whether the timer task is still valid after we obtained the entry lock
-
timerEventRefresh
public void timerEventRefresh(Entry<K,V> e, Object task)
Starts a refresh operation or expires if no threads in the loader thread pool are available. If no async loader is available we execute the synchronous loader via the loader thread pool.- Specified by:
timerEventRefresh
in interfaceTimerEventListener<K,V>
- Parameters:
e
- seeTimerEventListener.timerEventExpireEntry(Entry, Object)
task
- seeTimerEventListener.timerEventExpireEntry(Entry, Object)
-
timerEventProbationTerminated
public void timerEventProbationTerminated(Entry<K,V> e, Object task)
- Specified by:
timerEventProbationTerminated
in interfaceTimerEventListener<K,V>
- Parameters:
e
- seeTimerEventListener.timerEventExpireEntry(Entry, Object)
task
- seeTimerEventListener.timerEventExpireEntry(Entry, Object)
-
getUserCache
public org.cache2k.Cache<K,V> getUserCache()
Description copied from interface:InternalCache
Cache used by user, eventually wrapped. Only available in WiredCache for event processing.- Specified by:
getUserCache
in interfaceInternalCache<K,V>
-
getTiming
public Timing getTiming()
- Specified by:
getTiming
in interfaceInternalCache<K,V>
-
getTimeReference
public org.cache2k.operation.TimeReference getTimeReference()
- Specified by:
getTimeReference
in interfaceInternalCache<K,V>
-
-