|
ehcache | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectnet.sf.ehcache.constructs.blocking.BlockingCache
public class BlockingCache
A blocking decorator for an Ehcache, backed by a Ehcache.
Mutex class from Doug Lea's concurrency package. If you wish to use
this class, you will need the concurrent package in your class path.
It features:
Mutex class does not appear in the JDK5 concurrency package. Doug Lea has
generously offered the following advice:
"You should just be able to use ReentrantLock here. We supply
ReentrantLock, but not Mutex because the number of cases where a
non-reentrant mutex is preferable is small, and most people are more
familiar with reentrant seamantics. If you really need a non-reentrant
one, the javadocs for class AbstractQueuedSynchronizer include sample
code for them."
-Doug
"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.
| Field Summary | |
|---|---|
protected Ehcache |
cache
The backing Cache |
static int |
LOCK_NUMBER
The default number of locks to use. |
protected Mutex[] |
locks
Based on the lock striping concept from Brian Goetz. |
protected int |
timeoutMillis
The amount of time to block a thread before a LockTimeoutException is thrown |
| Constructor Summary | |
|---|---|
BlockingCache(Ehcache cache)
Creates a BlockingCache which decorates the supplied cache. |
|
| Method Summary | |
|---|---|
void |
bootstrap()
Bootstrap command. |
long |
calculateInMemorySize()
Gets the size of the memory store for this cache Warning: This method can be very expensive to run. |
void |
clearStatistics()
Resets statistics counters back to 0. |
java.lang.Object |
clone()
Clones a cache. |
void |
dispose()
Flushes all cache items from memory to auxilliary caches and close the auxilliary caches. |
void |
evictExpiredElements()
Causes all elements stored in the Cache to be synchronously checked for expiry, and if expired, evicted. |
void |
flush()
Flushes all cache items from memory to the disk store, and from the DiskStore to disk. |
Element |
get(java.lang.Object key)
Looks up an entry. |
Element |
get(java.io.Serializable key)
Gets an element from the cache. |
BootstrapCacheLoader |
getBootstrapCacheLoader()
Accessor for the BootstrapCacheLoader associated with this cache. |
protected Ehcache |
getCache()
Retrieve the EHCache backing cache |
RegisteredEventListeners |
getCacheEventNotificationService()
Use this to access the service in order to register and unregister listeners |
CacheManager |
getCacheManager()
Gets the CacheManager managing this cache. |
long |
getDiskExpiryThreadIntervalSeconds()
|
int |
getDiskStoreHitCount()
Number of times a requested item was found in the Disk Store. |
int |
getDiskStoreSize()
Returns the number of elements in the disk store. |
java.lang.String |
getGuid()
The GUID for this cache instance can be used to determine whether two cache instance references are pointing to the same cache. |
int |
getHitCount()
The number of times a requested item was found in the cache. |
java.util.List |
getKeys()
Returns the keys for this cache. |
java.util.List |
getKeysNoDuplicateCheck()
Returns a list of all elements in the cache, whether or not they are expired. |
java.util.List |
getKeysWithExpiryCheck()
Returns a list of all elements in the cache. |
protected Mutex |
getLockForKey(java.lang.Object key)
Gets the Mutex to use for a given key. |
int |
getMaxElementsInMemory()
Gets the maximum number of elements to hold in memory. |
MemoryStoreEvictionPolicy |
getMemoryStoreEvictionPolicy()
The policy used to evict elements from the MemoryStore. |
int |
getMemoryStoreHitCount()
Number of times a requested item was found in the Memory Store. |
long |
getMemoryStoreSize()
Returns the number of elements in the memory store. |
int |
getMissCountExpired()
Number of times a requested element was found but was expired. |
int |
getMissCountNotFound()
Number of times a requested element was not found in the cache. |
java.lang.String |
getName()
Returns this cache's name |
Element |
getQuiet(java.lang.Object key)
Gets an element from the cache, without updating Element statistics. |
Element |
getQuiet(java.io.Serializable key)
Gets an element from the cache, without updating Element statistics. |
int |
getSize()
Gets the size of the cache. |
Statistics |
getStatistics()
Gets an immutable Statistics object representing the Cache statistics at the time. |
int |
getStatisticsAccuracy()
Accurately measuring statistics can be expensive. |
Status |
getStatus()
Gets the status attribute of the Cache. |
int |
getTimeoutMillis()
Gets the time to wait to acquire a lock. |
long |
getTimeToIdleSeconds()
Gets timeToIdleSeconds. |
long |
getTimeToLiveSeconds()
Gets timeToLiveSeconds. |
void |
initialise()
Newly created caches do not have a MemoryStore or a DiskStore. |
boolean |
isDiskPersistent()
|
boolean |
isElementInMemory(java.lang.Object key)
Whether an Element is stored in the cache in Memory, indicating a very low cost of retrieval. |
boolean |
isElementInMemory(java.io.Serializable key)
Whether an Element is stored in the cache in Memory, indicating a very low cost of retrieval. |
boolean |
isElementOnDisk(java.lang.Object key)
Whether an Element is stored in the cache on Disk, indicating a higher cost of retrieval. |
boolean |
isElementOnDisk(java.io.Serializable key)
Whether an Element is stored in the cache on Disk, indicating a higher cost of retrieval. |
boolean |
isEternal()
Are elements eternal. |
boolean |
isExpired(Element element)
Checks whether this cache element has expired. |
boolean |
isKeyInCache(java.lang.Object key)
An inexpensive check to see if the key exists in the cache. |
boolean |
isOverflowToDisk()
Does the overflow go to disk. |
boolean |
isValueInCache(java.lang.Object value)
An extremely expensive check to see if the value exists in the cache. |
java.lang.String |
liveness()
Synchronized version of getName to test liveness of the object lock. |
void |
put(Element element)
Adds an entry and unlocks it |
void |
put(Element element,
boolean doNotNotifyCacheReplicators)
Put an element in the cache. |
void |
putQuiet(Element element)
Put an element in the cache, without updating statistics, or updating listeners. |
boolean |
remove(java.lang.Object key)
Removes an Element from the Cache. |
boolean |
remove(java.lang.Object key,
boolean doNotNotifyCacheReplicators)
Removes an Element from the Cache. |
boolean |
remove(java.io.Serializable key)
Removes an Element from the Cache. |
boolean |
remove(java.io.Serializable key,
boolean doNotNotifyCacheReplicators)
Removes an Element from the Cache. |
void |
removeAll()
Removes all cached items. |
void |
removeAll(boolean doNotNotifyCacheReplicators)
Removes all cached items. |
boolean |
removeQuiet(java.lang.Object key)
Removes an Element from the Cache, without notifying listeners. |
boolean |
removeQuiet(java.io.Serializable key)
Removes an Element from the Cache, without notifying listeners. |
void |
setBootstrapCacheLoader(BootstrapCacheLoader bootstrapCacheLoader)
Sets the bootstrap cache loader. |
void |
setCacheManager(CacheManager cacheManager)
Sets the CacheManager |
void |
setDiskStorePath(java.lang.String diskStorePath)
DiskStore paths can conflict between CacheManager instances. |
void |
setName(java.lang.String name)
Sets the cache name which will name. |
void |
setStatisticsAccuracy(int statisticsAccuracy)
Sets the statistics accuracy. |
void |
setTimeoutMillis(int timeoutMillis)
Sets the time to wait to acquire a lock. |
| Methods inherited from class java.lang.Object |
|---|
equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Methods inherited from interface net.sf.ehcache.Ehcache |
|---|
toString |
| Field Detail |
|---|
public static final int LOCK_NUMBER
protected final Mutex[] locks
protected final Ehcache cache
protected int timeoutMillis
| Constructor Detail |
|---|
public BlockingCache(Ehcache cache)
throws CacheException
cache - a backing ehcache.
CacheException| Method Detail |
|---|
protected Ehcache getCache()
public java.lang.String getName()
getName in interface Ehcachepublic void setName(java.lang.String name)
setName in interface Ehcachename - the name of the cache. Should not be null.public long getTimeToIdleSeconds()
getTimeToIdleSeconds in interface Ehcachepublic long getTimeToLiveSeconds()
getTimeToLiveSeconds in interface Ehcachepublic boolean isEternal()
isEternal in interface Ehcachepublic boolean isOverflowToDisk()
isOverflowToDisk in interface Ehcachepublic int getMaxElementsInMemory()
getMaxElementsInMemory in interface Ehcachepublic MemoryStoreEvictionPolicy getMemoryStoreEvictionPolicy()
MemoryStore.
This can be one of:
getMemoryStoreEvictionPolicy in interface Ehcache
public boolean isExpired(Element element)
throws java.lang.IllegalStateException,
java.lang.NullPointerException
isExpired in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
java.lang.NullPointerException - if the element is null
public java.lang.Object clone()
throws java.lang.CloneNotSupportedException
LruMemoryStore or DiskStore has been created.
A new, empty, RegisteredEventListeners is created on clone.
clone in interface Ehcacheclone in class java.lang.ObjectCache
java.lang.CloneNotSupportedExceptionpublic boolean isDiskPersistent()
isDiskPersistent in interface Ehcachepublic long getDiskExpiryThreadIntervalSeconds()
getDiskExpiryThreadIntervalSeconds in interface Ehcachepublic RegisteredEventListeners getCacheEventNotificationService()
getCacheEventNotificationService in interface Ehcachepublic boolean isElementInMemory(java.io.Serializable key)
isElementInMemory in interface Ehcachepublic boolean isElementInMemory(java.lang.Object key)
isElementInMemory in interface Ehcachepublic boolean isElementOnDisk(java.io.Serializable key)
isElementOnDisk in interface Ehcachepublic boolean isElementOnDisk(java.lang.Object key)
isElementOnDisk in interface Ehcachepublic java.lang.String getGuid()
getGuid in interface Ehcachepublic CacheManager getCacheManager()
getCacheManager in interface Ehcachepublic void clearStatistics()
clearStatistics in interface Ehcachepublic int getStatisticsAccuracy()
getStatisticsAccuracy in interface EhcacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT, Statistics.STATISTICS_ACCURACY_GUARANTEED, Statistics.STATISTICS_ACCURACY_NONEpublic void setStatisticsAccuracy(int statisticsAccuracy)
setStatisticsAccuracy in interface EhcachestatisticsAccuracy - one of Statistics.STATISTICS_ACCURACY_BEST_EFFORT, Statistics.STATISTICS_ACCURACY_GUARANTEED, Statistics.STATISTICS_ACCURACY_NONEpublic void evictExpiredElements()
evictExpiredElements in interface Ehcachepublic boolean isKeyInCache(java.lang.Object key)
isKeyInCache in interface Ehcachekey - the key to check for
public boolean isValueInCache(java.lang.Object value)
isValueInCache in interface Ehcachevalue - to check for
public Statistics getStatistics()
throws java.lang.IllegalStateException
Statistics.STATISTICS_ACCURACY_BEST_EFFORT.
The size is the number of Elements in the MemoryStore plus
the number of Elements in the DiskStore.
This number is the actual number of elements, including expired elements that have
not been removed. Any duplicates between stores are accounted for.
Expired elements are removed from the the memory store when
getting an expired element, or when attempting to spool an expired element to
disk.
Expired elements are removed from the disk store when getting an expired element,
or when the expiry thread runs, which is once every five minutes.
Statistics.STATISTICS_ACCURACY_GUARANTEED.
This method accounts for elements which might be expired or duplicated between stores. It take approximately
200ms per 1000 elements to execute.
Statistics.STATISTICS_ACCURACY_NONE.
The number given may contain expired elements. In addition if the DiskStore is used it may contain some double
counting of elements. It takes 6ms for 1000 elements to execute. Time to execute is O(log n). 50,000 elements take
36ms.
getStatistics in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVEpublic void setCacheManager(CacheManager cacheManager)
setCacheManager in interface EhcachecacheManager - public BootstrapCacheLoader getBootstrapCacheLoader()
getBootstrapCacheLoader in interface Ehcache
public void setBootstrapCacheLoader(BootstrapCacheLoader bootstrapCacheLoader)
throws CacheException
setBootstrapCacheLoader in interface EhcachebootstrapCacheLoader - the loader to be used
CacheException - if this method is called after the cache is initialized
public void setDiskStorePath(java.lang.String diskStorePath)
throws CacheException
setDiskStorePath in interface EhcachediskStorePath - the new path to be used.
CacheException - if this method is called after the cache is initializedpublic void initialise()
MemoryStore or a DiskStore.
This method creates those and makes the cache ready to accept elements
initialise in interface Ehcachepublic void bootstrap()
bootstrap in interface Ehcache
public void dispose()
throws java.lang.IllegalStateException
dispose in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public Element get(java.lang.Object key)
throws LockTimeoutException
put(net.sf.ehcache.Element) is done
to put an Element in.
If a put is not done, the lock is never released
Note. If a LockTimeoutException is thrown while doing a get(java.lang.Object) it means the lock was never acquired,
therefore it is a threading error to call put(net.sf.ehcache.Element)
get in interface Ehcachekey - an Object value
LockTimeoutException - if timeout millis is non zero and this method has been unable to
acquire a lock in that timeEhcache.isExpired(net.sf.ehcache.Element)protected Mutex getLockForKey(java.lang.Object key)
key - the key
public void put(Element element)
put in interface Ehcacheelement - 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
put in interface Ehcacheelement - 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 peers
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
java.lang.IllegalArgumentException - if the element is null
CacheException
public void putQuiet(Element element)
throws java.lang.IllegalArgumentException,
java.lang.IllegalStateException,
CacheException
getQuiet(java.io.Serializable)
putQuiet in interface Ehcacheelement - An object. If Serializable it can fully participate in replication and the DiskStore.
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
java.lang.IllegalArgumentException - if the element is null
CacheException
public Element get(java.io.Serializable key)
throws java.lang.IllegalStateException,
CacheException
getQuiet(Object) to peak into the Element to see its last access time with get
get in interface Ehcachekey - a serializable value
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheExceptionisExpired(net.sf.ehcache.Element)
public Element getQuiet(java.io.Serializable key)
throws java.lang.IllegalStateException,
CacheException
getQuiet in interface Ehcachekey - a serializable value
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheExceptionisExpired(net.sf.ehcache.Element)
public Element getQuiet(java.lang.Object key)
throws java.lang.IllegalStateException,
CacheException
getQuiet in interface Ehcachekey - a serializable value
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheExceptionisExpired(net.sf.ehcache.Element)
public java.util.List getKeys()
throws CacheException
getKeys in interface EhcacheCacheException
public java.util.List getKeysWithExpiryCheck()
throws java.lang.IllegalStateException,
CacheException
getKeys()
, which is synchronised, and which takes 8ms per 1000 entries. This way
cache liveness is preserved, even if this method is very slow to return.
Consider whether your usage requires checking for expired keys. Because
this method takes so long, depending on cache settings, the list could be
quite out of date by the time you get it.
getKeysWithExpiryCheck in interface EhcacheObject keys
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheException
public java.util.List getKeysNoDuplicateCheck()
throws java.lang.IllegalStateException
getKeysNoDuplicateCheck in interface EhcacheObject keys
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public boolean remove(java.io.Serializable key)
throws java.lang.IllegalStateException
Element from the Cache. This also removes it from any
stores it may be in.
Also notifies the CacheEventListener after the element was removed, but only if an Element
with the key actually existed.
remove in interface Ehcachekey -
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public boolean remove(java.lang.Object key)
throws java.lang.IllegalStateException
Element from the Cache. This also removes it from any
stores it may be in.
Also notifies the CacheEventListener after the element was removed, but only if an Element
with the key actually existed.
remove in interface Ehcachekey -
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public boolean remove(java.io.Serializable key,
boolean doNotNotifyCacheReplicators)
throws java.lang.IllegalStateException
Element from the Cache. This also removes it from any
stores it may be in.
Also notifies the CacheEventListener after the element was removed, but only if an Element
with the key actually existed.
remove in interface Ehcachekey - 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 peers
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public boolean remove(java.lang.Object key,
boolean doNotNotifyCacheReplicators)
throws java.lang.IllegalStateException
Element from the Cache. This also removes it from any
stores it may be in.
Also notifies the CacheEventListener after the element was removed, but only if an Element
with the key actually existed.
remove in interface Ehcachekey - 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 peers
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public boolean removeQuiet(java.io.Serializable key)
throws java.lang.IllegalStateException
Element from the Cache, without notifying listeners. This also removes it from any
stores it may be in.
removeQuiet in interface Ehcachekey -
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public boolean removeQuiet(java.lang.Object key)
throws java.lang.IllegalStateException
Element from the Cache, without notifying listeners. This also removes it from any
stores it may be in.
removeQuiet in interface Ehcachekey -
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public void removeAll()
throws java.lang.IllegalStateException,
CacheException
removeAll in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheException
public void removeAll(boolean doNotNotifyCacheReplicators)
throws java.lang.IllegalStateException,
CacheException
removeAll in interface EhcachedoNotNotifyCacheReplicators - 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
java.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheException
public void flush()
throws java.lang.IllegalStateException,
CacheException
flush in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheException
public int getSize()
throws java.lang.IllegalStateException,
CacheException
Elements in the MemoryStore plus
the number of Elements in the DiskStore.
This number is the actual number of elements, including expired elements that have
not been removed.
Expired elements are removed from the the memory store when
getting an expired element, or when attempting to spool an expired element to
disk.
Expired elements are removed from the disk store when getting an expired element,
or when the expiry thread runs, which is once every five minutes.
To get an exact size, which would exclude expired elements, use getKeysWithExpiryCheck().size(),
although see that method for the approximate time that would take.
To get a very fast result, use getKeysNoDuplicateCheck().size(). If the disk store
is being used, there will be some duplicates.
getSize in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
CacheException
public long calculateInMemorySize()
throws java.lang.IllegalStateException,
CacheException
calculateInMemorySize in interface Ehcachejava.lang.IllegalStateException
CacheException
public long getMemoryStoreSize()
throws java.lang.IllegalStateException
getMemoryStoreSize in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVE
public int getDiskStoreSize()
throws java.lang.IllegalStateException
getDiskStoreSize in interface Ehcachejava.lang.IllegalStateException - if the cache is not Status.STATUS_ALIVEpublic Status getStatus()
getStatus in interface Ehcachepublic int getHitCount()
getHitCount in interface Ehcachepublic int getMemoryStoreHitCount()
getMemoryStoreHitCount in interface Ehcachepublic int getDiskStoreHitCount()
getDiskStoreHitCount in interface Ehcachepublic int getMissCountNotFound()
getMissCountExpired(),
or because it was simply not there.
getMissCountNotFound in interface Ehcachepublic int getMissCountExpired()
getMissCountExpired in interface Ehcachepublic 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()
|
ehcache | |||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||