public class SelfPopulatingCache extends BlockingCache
Ehcache
that creates entries on demand.
Clients of the cache simply call it without needing knowledge of whether the entry exists in the cache.
The cache is designed to be refreshed. Refreshes operate on the backing cache, and do not
degrade performance of BlockingCache.get(java.io.Serializable)
calls.
Thread safety depends on the factory being used. The UpdatingCacheEntryFactory should be made thread safe. In addition users of returned values should not modify their contents.
Modifier and Type | Field and Description |
---|---|
protected CacheEntryFactory |
factory
A factory for creating entries, given a key
|
timeoutMillis
underlyingCache
Constructor and Description |
---|
SelfPopulatingCache(Ehcache cache,
CacheEntryFactory factory)
Creates a SelfPopulatingCache.
|
SelfPopulatingCache(Ehcache cache,
int numberOfStripes,
CacheEntryFactory factory)
Create a SelfPopulatingCache, with a specific number of stripes passed to the
underlying
BlockingCache . |
Modifier and Type | Method and Description |
---|---|
Element |
get(Object key)
Looks up an entry.
|
protected static Element |
makeAndCheckElement(Object key,
Object value)
Both CacheEntryFactory can return an Element rather than just a regular value
this method test this, making a fresh Element otherwise.
|
void |
refresh()
Refresh the elements of this cache.
|
void |
refresh(boolean quiet)
Refresh the elements of this cache.
|
Element |
refresh(Object key)
Refresh a single element.
|
Element |
refresh(Object key,
boolean quiet)
Refresh a single element.
|
protected void |
refreshElement(Element element,
Ehcache backingCache)
Refresh a single element.
|
protected Element |
refreshElement(Element element,
Ehcache backingCache,
boolean quiet)
Refresh a single element.
|
get, getAllWithLoader, getCache, getLockForKey, getTimeoutMillis, getWithLoader, liveness, load, loadAll, put, put, putIfAbsent, putIfAbsent, putQuiet, putWithWriter, registerCacheLoader, setTimeoutMillis, unregisterCacheLoader
acquireReadLockOnKey, acquireWriteLockOnKey, addPropertyChangeListener, bootstrap, calculateInMemorySize, calculateOffHeapSize, calculateOnDiskSize, clone, createQuery, disableDynamicFeatures, dispose, evictExpiredElements, flush, getAll, getBootstrapCacheLoader, getCacheConfiguration, getCacheEventNotificationService, getCacheExceptionHandler, getCacheManager, getDiskStoreSize, getGuid, getInternalContext, getKeys, getKeysNoDuplicateCheck, getKeysWithExpiryCheck, getMemoryStoreSize, getName, getOffHeapStoreSize, getQuiet, getQuiet, getRegisteredCacheExtensions, getRegisteredCacheLoaders, getRegisteredCacheWriter, getSearchAttribute, getSearchAttributes, getSize, getStatistics, getStatus, getWriterManager, hasAbortedSizeOf, initialise, isClusterBulkLoadEnabled, isClusterCoherent, isDisabled, isElementInMemory, isElementInMemory, isElementOnDisk, isElementOnDisk, isExpired, isKeyInCache, isNodeBulkLoadEnabled, isNodeCoherent, isReadLockedByCurrentThread, isSearchable, isValueInCache, isWriteLockedByCurrentThread, putAll, recalculateSize, registerCacheExtension, registerCacheWriter, registerDynamicAttributesExtractor, releaseReadLockOnKey, releaseWriteLockOnKey, remove, remove, remove, remove, removeAll, removeAll, removeAll, removeAll, removeAndReturnElement, removeElement, removePropertyChangeListener, removeQuiet, removeQuiet, removeWithWriter, replace, replace, setBootstrapCacheLoader, setCacheExceptionHandler, setCacheManager, setDisabled, setName, setNodeBulkLoadEnabled, setNodeCoherent, setTransactionManagerLookup, toString, tryReadLockOnKey, tryWriteLockOnKey, unregisterCacheExtension, unregisterCacheWriter, waitUntilClusterBulkLoadComplete, waitUntilClusterCoherent
protected final CacheEntryFactory factory
public SelfPopulatingCache(Ehcache cache, CacheEntryFactory factory) throws CacheException
CacheException
public SelfPopulatingCache(Ehcache cache, int numberOfStripes, CacheEntryFactory factory) throws CacheException
BlockingCache
.CacheException
public Element get(Object key) throws LockTimeoutException
get
in interface Ehcache
get
in class BlockingCache
key
- an Object valueLockTimeoutException
- if timeout millis is non zero and this method has been unable to
acquire a lock in that timeEhcache.isExpired(net.sf.ehcache.Element)
public void refresh() throws CacheException
Refreshes bypass the BlockingCache
and act directly on the backing Ehcache
.
This way, BlockingCache
gets can continue to return stale data while the refresh, which
might be expensive, takes place.
Quiet methods are used, so that statistics are not affected. Note that the refreshed elements will not be replicated to any cache peers.
Configure ehcache.xml to stop elements from being refreshed forever:
CacheException
public void refresh(boolean quiet) throws CacheException
Refreshes bypass the BlockingCache
and act directly on the backing Ehcache
.
This way, BlockingCache
gets can continue to return stale data while the refresh, which
might be expensive, takes place.
Quiet methods are used if argument 0 is true, so that statistics are not affected, but note that replication will then not occur
Configure ehcache.xml to stop elements from being refreshed forever:
quiet
- whether the backing cache is quietly updated or not, if true replication will not occurCacheException
public Element refresh(Object key) throws CacheException
Refreshes bypass the BlockingCache
and act directly on the backing Ehcache
.
This way, BlockingCache
gets can continue to return stale data while the refresh, which
might be expensive, takes place.
If the element is absent it is created
Quiet methods are used, so that statistics are not affected. Note that the refreshed element will not be replicated to any cache peers.
key
- CacheException
public Element refresh(Object key, boolean quiet) throws CacheException
Refreshes bypass the BlockingCache
and act directly on the backing Ehcache
.
This way, BlockingCache
gets can continue to return stale data while the refresh, which
might be expensive, takes place.
If the element is absent it is created
Quiet methods are used if argument 1 is true, so that statistics are not affected, but note that replication will then not occur
key
- quiet
- whether the backing cache is quietly updated or not,
if true replication will not occurCacheException
protected void refreshElement(Element element, Ehcache backingCache) throws Exception
protected Element refreshElement(Element element, Ehcache backingCache, boolean quiet) throws Exception
protected static Element makeAndCheckElement(Object key, Object value) throws CacheException
key
- value
- CacheException
- for various illegal states which could be harmfulCopyright 2001-2021, Terracotta, Inc.