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 net. class. If you wish to use
this class, you will need the concurrent package in your class path.
It features:
"Hashtable / synchronizedMap uses the "one big fat lock" approach to guard the mutable state of the map. That works, but is a big concurrency bottleneck, as you've observed. You went to the opposite extreme, one lock per key. That works (as long as you've got sufficient synchronization in the cache itself to protect its own data structures.)
Lock striping is a middle ground, partitioning keys into a fixed number of subsets, like the trick used at large theaters for will-call ticket pickup -- there are separate lines for "A-F, G-M, N-R, and S-Z". This way, there are a fixed number of locks, each guarding (hopefully) 1/Nth of the keys." - Brian Goetz
Further improvements to hashing suggested by Joe Bowbeer.
| Modifier and Type | Class and Description |
|---|---|
| private abstract 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 final AtomicReference | |
| private final OperationObserver | |
| private final int | |
| protected volatile int | timeoutMillis
The amount of time to block a thread before a LockTimeoutException is thrown |
| Access | Constructor and Description |
|---|---|
| public | BlockingCache(final Ehcache
a backing ehcache. cache, int how many stripes to has the keys against. Must be a non-zero even number. This is a trade-off between
memory use and concurrency numberOfStripes)Creates a BlockingCache which decorates the supplied cache. |
| public | BlockingCache(final Ehcache
a backing ehcache. cache)Creates a BlockingCache which decorates the supplied cache. |
| Modifier and Type | Method and Description |
|---|---|
| private void | |
| private CacheLockProvider | |
| private <V> V | |
| public Element | get(final Object
an Object value key)Overrides net. Implements net. |
| public Element | Returns: the element, or null, if it does not exist.a serializable value key)Overrides net. Implements net. |
| public Map | getAllWithLoader(Collection<E>
a collection of keys to be returned/loaded keys, Object an argument to pass to the CacheLoader. loaderArgument)Overrides net. Implements net. |
| protected Ehcache | |
| private CacheLockProvider | |
| protected Sync | Returns: one of a limited number of Sync's.the key key)Gets the Sync to use for a given key. |
| public int | |
| public Element | getWithLoader(Object
key whose associated value is to be returned. key, CacheLoader the override loader to use. If null, the cache's default loader will be used loader, Object an argument to pass to the CacheLoader. loaderArgument)Overrides net. Implements net. |
| public synchronized String | Returns: the name of the cache.Synchronized version of getName to test liveness of the object lock. |
| public void | load(Object
key whose associated value to be loaded using the associated cacheloader if this cache doesn't contain it. key)Overrides net. Implements net. |
| public void | loadAll(Collection<E>
- collection of the keys whose associated values to be loaded into this cache by using the associated
cacheloader if this cache doesn't contain them. keys, Object can be anything that makes sense to the loader
argument)
The Ehcache native API provides similar functionality to loaders using the
decorator Overrides net. Implements net. |
| public void | put(final Element
An object. If Serializable it can fully participate in replication and the DiskStore. element)Overrides net. Implements net. |
| public void | put(final Element
An object. If Serializable it can fully participate in replication and the DiskStore. element, final boolean whether the put is coming from a doNotNotifyCacheReplicators cache peer, in which case this put should not initiate a
further notification to doNotNotifyCacheReplicators cache peers doNotNotifyCacheReplicators)Overrides net. Implements net. |
| public Element | putIfAbsent(final Element
element to be added element)Overrides net. Implements net. |
| public Element | putIfAbsent(final Element
element to be added element, final boolean whether the put is coming from a doNotNotifyCacheReplicators cache peer, in which case this put should not initiate a
further notification to doNotNotifyCacheReplicators cache peers doNotNotifyCacheReplicators)Overrides net. Implements net. |
| public void | putQuiet(final Element
An object. If Serializable it can fully participate in replication and the DiskStore. element)Overrides net. Implements net. |
| public void | putWithWriter(final Element
An object. If Serializable it can fully participate in replication and the DiskStore. element)Overrides net. Implements net. |
| public void | registerCacheLoader(CacheLoader
A Cache Loader to register cacheLoader)Overrides net. Implements net. CacheLoader with the cache.
|
| public void | setTimeoutMillis(int
the time in ms. Must be a positive number. 0 means wait forever. timeoutMillis)Sets the time to wait to acquire a lock. |
| public void | unregisterCacheLoader(CacheLoader
A Cache Loader to unregister cacheLoader)Overrides net. Implements net. CacheLoader with the cache.
|
| cacheLockProviderReference | back to summary |
|---|---|
| private final AtomicReference<CacheLockProvider> cacheLockProviderReference | |
| getObserver | back to summary |
|---|---|
| private final OperationObserver<BlockingCacheOperationOutcomes. | |
| stripes | back to summary |
|---|---|
| private final int stripes | |
| timeoutMillis | back to summary |
|---|---|
| protected volatile int timeoutMillis The amount of time to block a thread before a LockTimeoutException is thrown | |
| BlockingCache | back to summary |
|---|---|
| public BlockingCache(final Ehcache cache, int numberOfStripes) throws CacheException Creates a BlockingCache which decorates the supplied cache.
| |
| BlockingCache | back to summary |
|---|---|
| public BlockingCache(final Ehcache cache) throws CacheException Creates a BlockingCache which decorates the supplied cache.
| |
| acquiredLockForKey | back to summary |
|---|---|
| private void acquiredLockForKey(final Object key, final Sync lock, final LockType lockType) | |
| createCacheLockProvider | back to summary |
|---|---|
| private CacheLockProvider createCacheLockProvider() | |
| doAndReleaseWriteLock | back to summary |
|---|---|
| private <V> V doAndReleaseWriteLock(BlockingCache. | |
| get | back to summary |
|---|---|
| public Element get(final Object key) throws RuntimeException, LockTimeoutException Overrides net. Implements net. Looks up an entry. Blocks if the entry is null until a call to 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
Note If a LockTimeoutException is thrown while doing a
| |
| get | back to summary |
|---|---|
| public Element get(Serializable key) throws IllegalStateException, CacheException Overrides net. Implements net. Gets an element from the cache. Updates Element Statistics
Note that the Element's lastAccessTime is always the time of this get.
Use
| |
| getAllWithLoader | back to summary |
|---|---|
| public Map Overrides net. Implements net. This method is not appropriate to use with BlockingCache.
| |
| getCache | back to summary |
|---|---|
| protected Ehcache getCache() Retrieve the EHCache backing cache
| |
| getCacheLockProvider | back to summary |
|---|---|
| private CacheLockProvider getCacheLockProvider() | |
| getLockForKey | back to summary |
|---|---|
| protected Sync getLockForKey(final Object key) Gets the Sync to use for a given key. | |
| getTimeoutMillis | back to summary |
|---|---|
| public int getTimeoutMillis() Gets the time to wait to acquire a lock.
| |
| getWithLoader | back to summary |
|---|---|
| public Element getWithLoader(Object key, CacheLoader loader, Object loaderArgument) throws CacheException Overrides net. Implements net. This method is not appropriate to use with BlockingCache.
| |
| liveness | back to summary |
|---|---|
| public synchronized String liveness() Synchronized version of getName to test liveness of the object lock. The time taken for this method to return is a useful measure of runtime contention on the cache.
| |
| load | back to summary |
|---|---|
| public void load(Object key) throws CacheException Overrides net. Implements net. This method is not appropriate to use with BlockingCache.
| |
| loadAll | back to summary |
|---|---|
| public void loadAll(Collection<E> keys, Object argument) throws CacheException Overrides net. Implements net. This method is not appropriate to use with BlockingCache.
| |
| put | back to summary |
|---|---|
| public void put(final Element element) Overrides net. Implements net. Adds an entry and unlocks it
| |
| put | back to summary |
|---|---|
| public void put(final Element element, final boolean doNotNotifyCacheReplicators) throws IllegalArgumentException, IllegalStateException, CacheException Overrides net. Implements net. Doc from net. Put an element in the cache. Resets the access statistics on the element, which would be the case if it has previously been gotten from a cache, and is now being put back. Also notifies the CacheEventListener that:
| |
| putIfAbsent | back to summary |
|---|---|
| public Element putIfAbsent(final Element element) throws NullPointerException Overrides net. Implements net. Doc from net. Put an element in the cache if no element is currently mapped to the elements key.
| |
| putIfAbsent | back to summary |
|---|---|
| public Element putIfAbsent(final Element element, final boolean doNotNotifyCacheReplicators) throws NullPointerException Overrides net. Implements net. Doc from net. Put an element in the cache if no element is currently mapped to the elements key.
| |
| putQuiet | back to summary |
|---|---|
| public void putQuiet(final Element element) throws IllegalArgumentException, IllegalStateException, CacheException Overrides net. Implements net. Doc from net. Put an element in the cache, without updating statistics, or updating listeners. This is meant to be used
in conjunction with
| |
| putWithWriter | back to summary |
|---|---|
| public void putWithWriter(final Element element) throws IllegalArgumentException, IllegalStateException, CacheException Overrides net. Implements net. Doc from net. Put an element in the cache writing through a CacheWriter. If no CacheWriter has been registered for the cache, then this method throws an exception. Resets the access statistics on the element, which would be the case if it has previously been gotten from a cache, and is now being put back. Also notifies the CacheEventListener, if the writer operation succeeds, that:
| |
| registerCacheLoader | back to summary |
|---|---|
| public void registerCacheLoader(CacheLoader cacheLoader) Overrides net. Implements net. Register a If the CacheLoader is not initialised, initialise it.
| |
| setTimeoutMillis | back to summary |
|---|---|
| public void setTimeoutMillis(int timeoutMillis) Sets the time to wait to acquire a lock. This may be modified at any time. The consequences of setting a timeout are:
| |
| unregisterCacheLoader | back to summary |
|---|---|
| public void unregisterCacheLoader(CacheLoader cacheLoader) Overrides net. Implements net. Unregister a
| |
| Modifier and Type | Method and Description |
|---|---|
| pack-priv abstract V |
| element | back to summary |
|---|---|
| private final Element element | |
| PutAction | back to summary |
|---|---|
| private PutAction(Element element) | |
| put | back to summary |
|---|---|
| pack-priv abstract V put() implement method with the put*
| |