public abstract class BaseCache<E extends Entry,K,T> extends Object implements Cache<K,T>, CanCheckIntegrity, Iterable<CacheEntry<K,T>>
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 |
|---|---|
class |
BaseCache.Info
Stable interface to request information from the cache, the object
safes values that need a longer calculation time, other values are
requested directly.
|
protected class |
BaseCache.MyTimerTask |
static class |
BaseCache.Tunable |
| Modifier and Type | Field and Description |
|---|---|
protected BulkCacheSource<K,T> |
bulkCacheSource |
protected long |
bulkGetCnt |
protected long |
clearedTime |
protected EntryExpiryCalculator<K,T> |
entryExpiryCalculator |
protected int |
evictedButInHashCnt
Needed to correct the counter invariants, because during eviction the entry
might be removed from the replacement list, but still in the hash.
|
protected long |
evictedCnt |
protected boolean |
evictionNeeded
Flag during operation that indicates, that the cache is full and eviction needs
to be done.
|
protected ExceptionExpiryCalculator<K> |
exceptionExpiryCalculator |
protected long |
exceptionMaxLinger |
protected ExperimentalBulkCacheSource<K,T> |
experimentalBulkCacheSource |
protected long |
expiredKeptCnt |
protected long |
expiredRemoveCnt |
protected long |
fetchButHitCnt |
protected long |
fetchCnt |
protected long |
fetchExceptionCnt |
protected long |
fetchMillis |
protected int |
hashSeed |
protected BaseCache.Info |
info |
protected long |
internalExceptionCnt
An exception that should not have happened and was not thrown to the
application.
|
protected long |
keyMutationCount |
protected Class |
keyType |
protected long |
loadHitCnt
Entry was loaded from storage and fresh.
|
protected long |
loadMissCnt
Storage did not contain the requested entry.
|
protected long |
loadNonFreshAndFetchedCnt
Separate counter for loaded entries that needed a fetch.
|
protected long |
loadNonFreshCnt
Loaded from storage, but the entry was not fresh and cannot be returned.
|
protected Object |
lock
Structure lock of the cache.
|
protected E[] |
mainHash |
protected Hash<E> |
mainHashCtrl |
protected CacheManagerImpl |
manager |
protected int |
maximumBulkFetchSize |
protected long |
maxLinger
Time in milliseconds we keep an element
|
protected int |
maxSize
Maximum amount of elements in cache
|
protected String |
name |
protected long |
newEntryCnt |
protected long |
peekHitNotFreshCnt |
protected long |
peekMissCnt |
protected long |
putCnt |
protected long |
putNewEntryCnt |
protected long |
refreshCnt |
protected E[] |
refreshHash |
protected Hash<E> |
refreshHashCtrl |
protected long |
refreshHitCnt |
protected CacheRefreshThreadPool |
refreshPool |
protected long |
refreshSubmitFailedCnt |
protected long |
removedCnt |
protected boolean |
shutdownInitiated |
protected Futures.WaitForAllFuture<?> |
shutdownWaitFuture
Stuff that we need to wait for before shutdown may complete
|
protected CacheSourceWithMetaInfo<K,T> |
source |
protected long |
startedTime |
protected StorageAdapter |
storage |
protected long |
suppressedExceptionCnt |
protected Timer |
timer |
protected int |
timerCancelCount |
protected TimerService |
timerService |
protected long |
touchedTime |
protected static BaseCache.Tunable |
TUNABLE |
protected Class |
valueType |
protected long |
virginEvictCnt
A newly inserted entry was removed by the eviction without the fetch to complete.
|
protected boolean |
waitForClear
Stops creation of new entries when clear is ongoing.
|
| Constructor and Description |
|---|
BaseCache() |
| Modifier and Type | Method and Description |
|---|---|
protected long |
calcNextRefreshTime(K _key,
T _newObject,
long now,
Entry _entry) |
protected int |
calculateHashEntryCount() |
protected void |
checkClosed() |
protected static boolean |
checkCyclicListIntegrity(Entry e) |
protected E |
checkForGhost(K key,
int hc)
Check whether we have an entry in the ghost table
remove it from ghost and insert it into the replacement list.
|
void |
checkIntegrity()
Check internal data structures and throw and exception if something is wrong, used for unit testing
|
void |
clear() |
protected void |
clearLocalCache() |
void |
clearTimingStatistics() |
void |
close() |
protected boolean |
conditionallyStartProcess(E e) |
boolean |
contains(K key) |
void |
destroy() |
protected void |
evictEventually() |
protected void |
expireEntry(E e) |
protected long |
fetch(E e) |
protected long |
fetchFromSource(E e) |
protected long |
fetchWithStorage(E e,
boolean _needsFetch) |
protected E |
findEvictionCandidate()
Find an entry that should be evicted.
|
void |
flush() |
T |
get(K key) |
Map<K,T> |
getAll(K[] _keys)
JSR107 convenience getAll from array
|
Map<K,T> |
getAll(Set<? extends K> _keys)
JSR107 bulk interface
|
void |
getBulk(K[] _keys,
T[] _result,
BitSet _fetched,
int s,
int e)
Retrieve
|
protected String |
getCompleteName()
Returns name of the cache with manager name.
|
protected static int |
getCyclicListEntryCount(Entry e) |
CacheEntry<K,T> |
getEntry(K key) |
protected E |
getEntryInternal(K key) |
long |
getExpiredCnt() |
protected String |
getExtraStatistics() |
long |
getFetchesBecauseOfNewEntries()
For peek no fetch is counted if there is a storage miss, hence the extra counter.
|
protected int |
getFetchesInFlight() |
abstract long |
getHitCnt() |
BaseCache.Info |
getInfo() |
protected IntegrityState |
getIntegrityState() |
BaseCache.Info |
getLatestInfo() |
protected static int |
getListEntryCount(Entry _head) |
protected int |
getLocalSize() |
protected Log |
getLog()
Normally a cache itself logs nothing, so just construct when needed.
|
String |
getName() |
StorageAdapter |
getStorage() |
int |
getTotalEntryCount() |
protected boolean |
hasBackgroundRefreshAndServesExpiredValues()
Enabling background refresh means also serving expired values.
|
protected boolean |
hasKeepAfterExpired() |
protected boolean |
hasSharpTimeout() |
protected boolean |
hasSuppressExceptions() |
void |
init()
Registers the cache in a global set for the clearAllCaches function and
registers it with the resource monitor.
|
protected void |
initializeHeapCache() |
protected long |
insert(E e,
T v,
long t0,
long t,
byte _updateStatistics)
Calculate the next refresh time if a timer / expiry is needed and call insert.
|
protected long |
insert(E e,
T _value,
long t0,
long t,
byte _updateStatistics,
long _nextRefreshTime) |
protected static <E extends Entry> |
insertAfterHeadCyclicList(E _head,
E e)
Insert X into A B C, yields: A X B C.
|
protected Entry |
insertEntryFromStorage(StorageEntry se,
boolean _needsFetch)
Insert the storage entry in the heap cache and return it.
|
protected long |
insertEntryFromStorage(StorageEntry se,
E e,
boolean _needsFetch) |
protected long |
insertFetched(E e,
T v,
long t0,
long t) |
protected static void |
insertInList(Entry _head,
Entry e) |
protected static <E extends Entry> |
insertIntoHeadCyclicList(E _head,
E e)
Insert element at the head of the list
|
protected abstract void |
insertIntoReplacementList(E e)
New cache entry, put it in the replacement algorithm structure
|
protected static <E extends Entry> |
insertIntoTailCyclicList(E _head,
E e) |
protected long |
insertOnPut(E e,
T v,
long t0,
long t) |
protected boolean |
isClosed() |
protected ClosableConcurrentHashEntryIterator<Entry> |
iterateAllHeapEntries()
Returns all cache entries within the heap cache.
|
protected ClosableIterator<Entry> |
iterateLocalAndStorage()
Complete iteration of all entries in the cache, including
storage / persisted entries.
|
ClosableIterator<CacheEntry<K,T>> |
iterator() |
protected void |
lockAndRunForPurge(Object key,
PurgeableStorage.PurgeAction _action)
Insert a cache entry for the given key and run action under the entry
lock.
|
protected E |
lookupEntry(K key,
int hc) |
protected E |
lookupEntrySynchronized(K key) |
protected E |
lookupEntryUnsynchronized(K key,
int hc)
Implement unsynchronized lookup if it is supported by the eviction.
|
protected E |
lookupOrNewEntrySynchronized(K key)
Lookup or create a new entry.
|
protected int |
modifiedHash(int h)
This function calculates a modified hash code.
|
protected static <E extends Entry> |
moveToFront(E _head,
E e) |
protected abstract E |
newEntry()
Entry object factory.
|
protected E |
newEntry(K key,
int hc)
Insert new entry in all structures (hash and replacement list).
|
T |
peek(K key) |
CacheEntry<K,T> |
peekEntry(K key) |
protected E |
peekEntryInternal(K key)
Return the entry, if it is in the cache, without invoking the
cache source.
|
void |
prefetch(K key) |
void |
prefetch(List<K> keys,
int _startIndex,
int _endIndexExclusive) |
void |
prefetch(Set<K> keys) |
void |
purge() |
void |
put(K key,
T value) |
boolean |
putIfAbsent(K key,
T value) |
protected abstract void |
recordHit(E e)
Record an entry hit.
|
void |
remove(K key) |
void |
removeAllAtOnce(Set<K> _keys) |
protected boolean |
removeEntry(E e)
Remove the entry from the hash and the replacement list.
|
protected void |
removeEntryFromReplacementList(E e) |
protected static <E extends Entry> |
removeFromCyclicList(E _head,
E e) |
protected static Entry |
removeFromCyclicList(Entry e) |
protected static void |
removeFromList(Entry e) |
boolean |
removeWithFlag(K key)
Remove the object mapped to a key from the cache.
|
void |
resetStorage(StorageAdapter _from,
StorageAdapter to)
Change the storage implementation to another one or null for a disconnect
|
protected CacheEntry<K,T> |
returnEntry(Entry<E,K,T> e)
Wrap entry in a separate object instance.
|
protected T |
returnValue(Entry<E,K,T> e) |
void |
setBulkCacheSource(BulkCacheSource<K,T> s) |
void |
setCacheConfig(CacheConfig c)
called via reflection from CacheBuilder
|
void |
setCacheManager(CacheManagerImpl cm) |
void |
setEntryExpiryCalculator(EntryExpiryCalculator<K,T> v) |
void |
setExceptionExpiryCalculator(ExceptionExpiryCalculator<K> v) |
void |
setExperimentalBulkCacheSource(ExperimentalBulkCacheSource<K,T> g) |
void |
setExpirySeconds(int s)
Set the time in seconds after which the cache does an refresh of the
element.
|
protected void |
setFeatureBit(int _bitmask,
boolean _flag) |
void |
setName(String n)
Set the name and configure a logging, used within cache construction.
|
void |
setRefreshController(RefreshController<T> lc)
called via reflection from CacheBuilder
|
void |
setSource(CacheSource<K,T> g) |
void |
setSource(CacheSourceWithMetaInfo<K,T> eg) |
protected long |
stopStartTimer(long _nextRefreshTime,
E e,
long now) |
protected void |
timerEvent(E e,
long _executionTime)
When the time has come remove the entry from the cache.
|
String |
toString()
Return status information.
|
protected void |
updateShutdownWaitFuture(Future<?> f) |
protected static final BaseCache.Tunable TUNABLE
protected int hashSeed
protected int maxSize
protected CacheManagerImpl manager
protected CacheSourceWithMetaInfo<K,T> source
protected long maxLinger
protected long exceptionMaxLinger
protected EntryExpiryCalculator<K,T> entryExpiryCalculator
protected ExceptionExpiryCalculator<K> exceptionExpiryCalculator
protected BaseCache.Info info
protected long clearedTime
protected long startedTime
protected long touchedTime
protected int timerCancelCount
protected long keyMutationCount
protected long putCnt
protected long putNewEntryCnt
protected long removedCnt
protected long expiredKeptCnt
protected long expiredRemoveCnt
protected long evictedCnt
protected long refreshCnt
protected long suppressedExceptionCnt
protected long fetchExceptionCnt
protected long peekHitNotFreshCnt
protected long peekMissCnt
protected long fetchCnt
protected long fetchButHitCnt
protected long bulkGetCnt
protected long fetchMillis
protected long refreshHitCnt
protected long newEntryCnt
protected long loadNonFreshCnt
protected long loadHitCnt
protected long loadNonFreshAndFetchedCnt
protected long refreshSubmitFailedCnt
protected long internalExceptionCnt
protected int evictedButInHashCnt
protected long loadMissCnt
protected long virginEvictCnt
protected int maximumBulkFetchSize
protected final Object lock
protected CacheRefreshThreadPool refreshPool
protected Hash<E extends Entry> mainHashCtrl
protected Hash<E extends Entry> refreshHashCtrl
protected E extends Entry[] refreshHash
protected ExperimentalBulkCacheSource<K,T> experimentalBulkCacheSource
protected BulkCacheSource<K,T> bulkCacheSource
protected Futures.WaitForAllFuture<?> shutdownWaitFuture
protected boolean shutdownInitiated
protected boolean evictionNeeded
protected StorageAdapter storage
protected TimerService timerService
protected boolean waitForClear
public BaseCache()
protected final boolean hasSharpTimeout()
protected final boolean hasKeepAfterExpired()
protected final boolean hasSuppressExceptions()
protected final void setFeatureBit(int _bitmask, boolean _flag)
protected final boolean hasBackgroundRefreshAndServesExpiredValues()
protected String getCompleteName()
public void resetStorage(StorageAdapter _from, StorageAdapter to)
public void setCacheConfig(CacheConfig c)
public void setEntryExpiryCalculator(EntryExpiryCalculator<K,T> v)
public void setExceptionExpiryCalculator(ExceptionExpiryCalculator<K> v)
public void setRefreshController(RefreshController<T> lc)
public void setSource(CacheSourceWithMetaInfo<K,T> eg)
public void setSource(CacheSource<K,T> g)
public void setExperimentalBulkCacheSource(ExperimentalBulkCacheSource<K,T> g)
public void setBulkCacheSource(BulkCacheSource<K,T> s)
public void setName(String n)
public void setExpirySeconds(int s)
public void setCacheManager(CacheManagerImpl cm)
public StorageAdapter getStorage()
public void init()
protected void updateShutdownWaitFuture(Future<?> f)
protected void checkClosed()
protected final void clearLocalCache()
protected void initializeHeapCache()
public void clearTimingStatistics()
protected boolean isClosed()
public void close()
protected ClosableIterator<Entry> iterateLocalAndStorage()
public ClosableIterator<CacheEntry<K,T>> iterator()
protected static void removeFromList(Entry e)
protected static void insertInList(Entry _head, Entry e)
protected static final int getListEntryCount(Entry _head)
protected static final <E extends Entry> void moveToFront(E _head, E e)
protected static final <E extends Entry> E insertIntoTailCyclicList(E _head, E e)
protected static final <E extends Entry> E insertAfterHeadCyclicList(E _head, E e)
protected static final <E extends Entry> E insertIntoHeadCyclicList(E _head, E e)
protected static <E extends Entry> E removeFromCyclicList(E _head, E e)
protected static Entry removeFromCyclicList(Entry e)
protected static int getCyclicListEntryCount(Entry e)
protected static boolean checkCyclicListIntegrity(Entry e)
protected abstract void insertIntoReplacementList(E e)
protected abstract E newEntry()
protected E findEvictionCandidate()
removeEntryFromReplacementList(Entry)
if this entry will be really evicted. Pinned entries may be skipped. A
good eviction algorithm returns another candidate on sequential calls, even
if the candidate was not removed.
Rationale: Within the structure lock we can check for an eviction candidate
and may remove it from the list. However, we cannot process additional operations or
events which affect the entry. For this, we need to acquire the lock on the entry
first.protected void removeEntryFromReplacementList(E e)
protected E checkForGhost(K key, int hc)
protected E lookupEntryUnsynchronized(K key, int hc)
protected CacheEntry<K,T> returnEntry(Entry<E,K,T> e)
protected E getEntryInternal(K key)
protected Entry insertEntryFromStorage(StorageEntry se, boolean _needsFetch)
getEntryInternal(Object)
except we do not need to retrieve the data from the storage again._needsFetch - if true the entry is fetched from CacheSource when expired.protected void lockAndRunForPurge(Object key, PurgeableStorage.PurgeAction _action)
protected final void evictEventually()
protected boolean removeEntry(E e)
protected E 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 putIfAbsent(K key, T value)
putIfAbsent in interface Cache<K,T>public boolean removeWithFlag(K key)
Operation with storage: If there is no entry within the cache there may be one in the storage, so we need to send the remove to the storage. However, if a remove() and a get() is going on in parallel it may happen that the entry gets removed from the storage and added again by the tail part of get(). To keep the cache and the storage consistent it must be ensured that this thread is the only one working on the entry.
protected E lookupOrNewEntrySynchronized(K key)
protected E lookupEntrySynchronized(K key)
protected E lookupEntry(K key, int hc)
protected E newEntry(K key, int hc)
protected long calcNextRefreshTime(K _key, T _newObject, long now, Entry _entry)
protected boolean conditionallyStartProcess(E e)
protected long fetchWithStorage(E e, boolean _needsFetch)
e - _needsFetch - true if value needs to be fetched from the cache source.
This is false, when the we only need to peek for an value already mapped.protected long insertEntryFromStorage(StorageEntry se, E e, boolean _needsFetch)
protected long fetchFromSource(E e)
protected final long insertFetched(E e, T v, long t0, long t)
protected final long insertOnPut(E e, T v, long t0, long t)
protected final long insert(E e, T v, long t0, long t, byte _updateStatistics)
protected final long insert(E e, T _value, long t0, long t, byte _updateStatistics, long _nextRefreshTime)
_nextRefreshTime - -1/MAXVAL: eternal, 0: expires immediatelyprotected long stopStartTimer(long _nextRefreshTime, E e, long now)
protected void timerEvent(E e, long _executionTime)
protected void expireEntry(E e)
protected final ClosableConcurrentHashEntryIterator<Entry> iterateAllHeapEntries()
public void removeAllAtOnce(Set<K> _keys)
removeAllAtOnce in interface Cache<K,T>public abstract long getHitCnt()
protected final int calculateHashEntryCount()
protected final int getLocalSize()
public final int getTotalEntryCount()
getTotalEntryCount in interface Cache<K,T>public long getExpiredCnt()
public long getFetchesBecauseOfNewEntries()
protected int getFetchesInFlight()
protected IntegrityState getIntegrityState()
public final void checkIntegrity()
checkIntegrity in interface CanCheckIntegritypublic final BaseCache.Info getInfo()
public final BaseCache.Info getLatestInfo()
protected String getExtraStatistics()
public String toString()
protected final int modifiedHash(int h)
cache2k API documentation. Copyright © 2000–2015 headissue GmbH, Munich.