public class BlockingCache extends EhcacheDecoratorAdapter
Ehcache
.
It allows concurrent read access to elements already in the cache. If the element is null, other
reads will block until an element with the same key is put into the cache.
This is useful for constructing read-through or self-populating caches.
This implementation uses the ReadWriteLockSync
class. If you wish to use
this class, you will need the concurrent package in your class path.
It features:
Modifier and Type | Class and Description |
---|---|
private static class |
BlockingCache.PutAction<V>
Callable like class to actually execute one of the many Ehcache.put* methods in the context of a BlockingCache
|
Modifier and Type | Field and Description |
---|---|
private java.util.concurrent.atomic.AtomicReference<CacheLockProvider> |
cacheLockProviderReference |
private int |
stripes |
protected int |
timeoutMillis
The amount of time to block a thread before a LockTimeoutException is thrown
|
underlyingCache
Constructor and Description |
---|
BlockingCache(Ehcache cache)
Creates a BlockingCache which decorates the supplied cache.
|
BlockingCache(Ehcache cache,
int numberOfStripes)
Creates a BlockingCache which decorates the supplied cache.
|
Modifier and Type | Method and Description |
---|---|
private void |
acquiredLockForKey(java.lang.Object key,
Sync lock,
LockType lockType) |
private CacheLockProvider |
createCacheLockProvider() |
private <V> V |
doAndReleaseWriteLock(BlockingCache.PutAction<V> putAction) |
Element |
get(java.lang.Object key)
Looks up an entry.
|
Element |
get(java.io.Serializable key)
Gets an element from the cache.
|
java.util.Map |
getAllWithLoader(java.util.Collection keys,
java.lang.Object loaderArgument)
This method is not appropriate to use with BlockingCache.
|
protected Ehcache |
getCache()
Retrieve the EHCache backing cache
|
private CacheLockProvider |
getCacheLockProvider() |
protected Sync |
getLockForKey(java.lang.Object key)
Gets the Sync to use for a given key.
|
int |
getTimeoutMillis()
Gets the time to wait to acquire a lock.
|
Element |
getWithLoader(java.lang.Object key,
CacheLoader loader,
java.lang.Object loaderArgument)
This method is not appropriate to use with BlockingCache.
|
java.lang.String |
liveness()
Synchronized version of getName to test liveness of the object lock.
|
void |
load(java.lang.Object key)
This method is not appropriate to use with BlockingCache.
|
void |
loadAll(java.util.Collection keys,
java.lang.Object argument)
This method is not appropriate to use with BlockingCache.
|
void |
put(Element element)
Adds an entry and unlocks it
|
void |
put(Element element,
boolean doNotNotifyCacheReplicators)
Put an element in the cache.
|
Element |
putIfAbsent(Element element)
Put an element in the cache if no element is currently mapped to the elements key.
|
Element |
putIfAbsent(Element element,
boolean doNotNotifyCacheReplicators)
Put an element in the cache if no element is currently mapped to the elements key.
|
void |
putQuiet(Element element)
Put an element in the cache, without updating statistics, or updating listeners.
|
void |
putWithWriter(Element element)
Put an element in the cache writing through a CacheWriter.
|
void |
registerCacheLoader(CacheLoader cacheLoader)
Register a
CacheLoader with the cache. |
void |
setTimeoutMillis(int timeoutMillis)
Sets the time to wait to acquire a lock.
|
void |
unregisterCacheLoader(CacheLoader cacheLoader)
Unregister a
CacheLoader with the cache. |
acquireReadLockOnKey, acquireWriteLockOnKey, addPropertyChangeListener, bootstrap, calculateInMemorySize, calculateOffHeapSize, calculateOnDiskSize, clearStatistics, clone, createQuery, disableDynamicFeatures, dispose, evictExpiredElements, flush, getAll, getAverageGetTime, getAverageSearchTime, getBootstrapCacheLoader, getCacheConfiguration, getCacheEventNotificationService, getCacheExceptionHandler, getCacheManager, getDiskStoreSize, getGuid, getInternalContext, getKeys, getKeysNoDuplicateCheck, getKeysWithExpiryCheck, getLiveCacheStatistics, getMemoryStoreSize, getName, getOffHeapStoreSize, getQuiet, getQuiet, getRegisteredCacheExtensions, getRegisteredCacheLoaders, getRegisteredCacheWriter, getSampledCacheStatistics, getSearchAttribute, getSearchesPerSecond, getSize, getSizeBasedOnAccuracy, getStatistics, getStatisticsAccuracy, getStatus, getWriterManager, hasAbortedSizeOf, initialise, isClusterBulkLoadEnabled, isClusterCoherent, isDisabled, isElementInMemory, isElementInMemory, isElementOnDisk, isElementOnDisk, isExpired, isKeyInCache, isNodeBulkLoadEnabled, isNodeCoherent, isPinned, isReadLockedByCurrentThread, isSampledStatisticsEnabled, isSearchable, isStatisticsEnabled, isValueInCache, isWriteLockedByCurrentThread, putAll, recalculateSize, registerCacheExtension, registerCacheUsageListener, registerCacheWriter, registerDynamicAttributesExtractor, releaseReadLockOnKey, releaseWriteLockOnKey, remove, remove, remove, remove, removeAll, removeAll, removeAll, removeAll, removeAndReturnElement, removeCacheUsageListener, removeElement, removePropertyChangeListener, removeQuiet, removeQuiet, removeWithWriter, replace, replace, setBootstrapCacheLoader, setCacheExceptionHandler, setCacheManager, setDisabled, setName, setNodeBulkLoadEnabled, setNodeCoherent, setPinned, setSampledStatisticsEnabled, setStatisticsAccuracy, setStatisticsEnabled, setTransactionManagerLookup, toString, tryReadLockOnKey, tryWriteLockOnKey, unpinAll, unregisterCacheExtension, unregisterCacheWriter, waitUntilClusterBulkLoadComplete, waitUntilClusterCoherent
protected volatile int timeoutMillis
private final int stripes
private final java.util.concurrent.atomic.AtomicReference<CacheLockProvider> cacheLockProviderReference
public BlockingCache(Ehcache cache, int numberOfStripes) throws CacheException
cache
- a backing ehcache.numberOfStripes
- how many stripes to has the keys against. Must be a non-zero even number. This is a trade-off between
memory use and concurrencyCacheException
- shouldn't happenpublic BlockingCache(Ehcache cache) throws CacheException
cache
- a backing ehcache.CacheException
- shouldn't happenprivate CacheLockProvider getCacheLockProvider()
private CacheLockProvider createCacheLockProvider()
protected Ehcache getCache()
public Element get(java.lang.Object key) throws java.lang.RuntimeException, LockTimeoutException
put(net.sf.ehcache.Element)
is done
to put an Element in.
If a put is not done, the lock is never released.
If this method throws an exception, it is the responsibility of the caller to catch that exception and call
put(new Element(key, null));
to release the lock acquired. See SelfPopulatingCache
for an example.
Note. If a LockTimeoutException is thrown while doing a get
it means the lock was never acquired,
therefore it is a threading error to call put(net.sf.ehcache.Element)
get
in interface Ehcache
get
in class EhcacheDecoratorAdapter
key
- an Object valueLockTimeoutException
- if timeout millis is non zero and this method has been unable to
acquire a lock in that timejava.lang.RuntimeException
- if thrown the lock will not released. Catch and callput(new Element(key, null));
to release the lock acquired.Ehcache.isExpired(net.sf.ehcache.Element)
private void acquiredLockForKey(java.lang.Object key, Sync lock, LockType lockType)
protected Sync getLockForKey(java.lang.Object key)
key
- the keypublic void put(Element element)
put
in interface Ehcache
put
in class EhcacheDecoratorAdapter
element
- An object. If Serializable it can fully participate in replication and the DiskStore.public void put(Element element, boolean doNotNotifyCacheReplicators) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, CacheException
EhcacheDecoratorAdapter
put
in interface Ehcache
put
in class EhcacheDecoratorAdapter
element
- An object. If Serializable it can fully participate in replication and the DiskStore.doNotNotifyCacheReplicators
- whether the put is coming from a doNotNotifyCacheReplicators cache peer, in which case this put should not initiate a
further notification to doNotNotifyCacheReplicators cache peersjava.lang.IllegalArgumentException
- if the element is nulljava.lang.IllegalStateException
- if the cache is not Status.STATUS_ALIVE
CacheException
public void putQuiet(Element element) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, CacheException
EhcacheDecoratorAdapter
Ehcache.getQuiet(java.io.Serializable)
putQuiet
in interface Ehcache
putQuiet
in class EhcacheDecoratorAdapter
element
- An object. If Serializable it can fully participate in replication and the DiskStore.java.lang.IllegalArgumentException
- if the element is nulljava.lang.IllegalStateException
- if the cache is not Status.STATUS_ALIVE
CacheException
public void putWithWriter(Element element) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException, CacheException
EhcacheDecoratorAdapter
putWithWriter
in interface Ehcache
putWithWriter
in class EhcacheDecoratorAdapter
element
- An object. If Serializable it can fully participate in replication and the DiskStore.java.lang.IllegalArgumentException
- if the element is nulljava.lang.IllegalStateException
- if the cache is not Status.STATUS_ALIVE
CacheException
- if no CacheWriter was registeredpublic Element putIfAbsent(Element element) throws java.lang.NullPointerException
EhcacheDecoratorAdapter
putIfAbsent
in interface Ehcache
putIfAbsent
in class EhcacheDecoratorAdapter
element
- element to be addedjava.lang.NullPointerException
- if the element is null, or has a null keypublic Element putIfAbsent(Element element, boolean doNotNotifyCacheReplicators) throws java.lang.NullPointerException
EhcacheDecoratorAdapter
putIfAbsent
in interface Ehcache
putIfAbsent
in class EhcacheDecoratorAdapter
element
- element to be addeddoNotNotifyCacheReplicators
- whether the put is coming from a doNotNotifyCacheReplicators cache peer, in which case this put should not initiate a
further notification to doNotNotifyCacheReplicators cache peersjava.lang.NullPointerException
- if the element is null, or has a null keyprivate <V> V doAndReleaseWriteLock(BlockingCache.PutAction<V> putAction)
public Element get(java.io.Serializable key) throws java.lang.IllegalStateException, CacheException
EhcacheDecoratorAdapter.getQuiet(Object)
to peak into the Element to see its last access time with getget
in interface Ehcache
get
in class EhcacheDecoratorAdapter
key
- a serializable valuejava.lang.IllegalStateException
- if the cache is not Status.STATUS_ALIVE
CacheException
EhcacheDecoratorAdapter.isExpired(net.sf.ehcache.Element)
public java.lang.String liveness()
public void setTimeoutMillis(int timeoutMillis)
timeoutMillis
- the time in ms. Must be a positive number. 0 means wait forever.public int getTimeoutMillis()
public void registerCacheLoader(CacheLoader cacheLoader)
CacheLoader
with the cache. It will then be tied into the cache lifecycle.
If the CacheLoader is not initialised, initialise it.registerCacheLoader
in interface Ehcache
registerCacheLoader
in class EhcacheDecoratorAdapter
cacheLoader
- A Cache Loader to registerpublic void unregisterCacheLoader(CacheLoader cacheLoader)
CacheLoader
with the cache. It will then be detached from the cache lifecycle.unregisterCacheLoader
in interface Ehcache
unregisterCacheLoader
in class EhcacheDecoratorAdapter
cacheLoader
- A Cache Loader to unregisterpublic Element getWithLoader(java.lang.Object key, CacheLoader loader, java.lang.Object loaderArgument) throws CacheException
getWithLoader
in interface Ehcache
getWithLoader
in class EhcacheDecoratorAdapter
key
- key whose associated value is to be returned.loader
- the override loader to use. If null, the cache's default loader will be usedloaderArgument
- an argument to pass to the CacheLoader.CacheException
- if this method is calledpublic java.util.Map getAllWithLoader(java.util.Collection keys, java.lang.Object loaderArgument) throws CacheException
getAllWithLoader
in interface Ehcache
getAllWithLoader
in class EhcacheDecoratorAdapter
keys
- a collection of keys to be returned/loadedloaderArgument
- an argument to pass to the CacheLoader.CacheException
- if this method is calledpublic void load(java.lang.Object key) throws CacheException
load
in interface Ehcache
load
in class EhcacheDecoratorAdapter
key
- key whose associated value to be loaded using the associated cacheloader if this cache doesn't contain it.CacheException
- if this method is calledpublic void loadAll(java.util.Collection keys, java.lang.Object argument) throws CacheException
loadAll
in interface Ehcache
loadAll
in class EhcacheDecoratorAdapter
CacheException
- if this method is called