|
ehcache | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectnet.sf.ehcache.Cache
Cache is the central class in ehcache. Caches have Element
s and are managed
by the CacheManager
. The Cache performs logical actions. It delegates physical
implementations to its Store
s.
CacheManager
. A Cache thus obtained
is guaranteed to have status STATUS_ALIVE
. This status is checked for any method which
throws IllegalStateException
and the same thrown if it is not alive. This would normally
happen if a call is made after CacheManager.shutdown()
is invoked.
Cache is threadsafe.
Statistics on cache usage are collected and made available through public methods.
Field Summary | |
static java.lang.String |
DEFAULT_CACHE_NAME
A reserved word for cache names. |
static int |
STATUS_ALIVE
The cache is alive. |
static int |
STATUS_DISPOSED
The cache is uninitialised. |
static int |
STATUS_UNINITIALISED
The cache is uninitialised. |
Constructor Summary | |
Cache(java.lang.String name,
int maximumSize,
boolean overflowToDisk,
boolean eternal,
long timeToLiveSeconds,
long timeToIdleSeconds)
Constructor. |
|
Cache(java.lang.String name,
int maximumSize,
boolean overflowToDisk,
boolean eternal,
long timeToLiveSeconds,
long timeToIdleSeconds,
boolean diskPersistent,
long diskExpiryThreadIntervalSeconds)
Full Constructor. |
Method Summary | |
long |
calculateInMemorySize()
Gets the size of the memory store for this cache Warning: This method can be very expensive to run. |
java.lang.Object |
clone()
Clones a cache. |
(package private) void |
dispose()
Flushes all cache items from memory to auxilliary caches and close the auxilliary caches. |
Element |
get(java.io.Serializable key)
Gets an element from the cache. |
long |
getDiskExpiryThreadIntervalSeconds()
|
(package private) DiskStore |
getDiskStore()
Gets the internal DiskStore. |
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. |
int |
getHitCount()
Number of times a requested item was found in the cache |
java.util.List |
getKeys()
Returns a list of all elements in the cache, whether or not they are expired. |
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. |
int |
getMaxElementsInMemory()
Gets the maximum number of elements to hold in memory |
(package private) MemoryStore |
getMemoryStore()
Gets the internal 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()
Gets the cache name |
Element |
getQuiet(java.io.Serializable key)
Gets an element from the cache, without updating Element statistics. |
int |
getSize()
Gets the size of the cache. |
int |
getStatus()
Gets the status attribute of the Store object |
long |
getTimeToIdleSeconds()
Gets timeToIdleSeconds |
long |
getTimeToLiveSeconds()
Gets timeToLiveSeconds |
(package private) void |
initialise(Configuration configuration)
Newly created caches do not have a MemoryStore or a DiskStore . |
boolean |
isDiskPersistent()
|
boolean |
isEternal()
Are elements eternal |
boolean |
isExpired(Element element)
Checks whether this cache element has expired. |
boolean |
isOverflowToDisk()
Does the overflow go to disk |
void |
put(Element element)
Put an element in the cache. |
void |
putQuiet(Element element)
Put an element in the cache, without updating statistics. |
boolean |
remove(java.io.Serializable key)
Removes an Element from the Cache. |
void |
removeAll()
Removes all cached items. |
(package private) void |
setName(java.lang.String name)
Sets the name |
java.lang.String |
toString()
Returns a String representation of Cache |
Methods inherited from class java.lang.Object |
equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
public static final java.lang.String DEFAULT_CACHE_NAME
public static final int STATUS_UNINITIALISED
public static final int STATUS_ALIVE
public static final int STATUS_DISPOSED
Constructor Detail |
public Cache(java.lang.String name, int maximumSize, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds)
Configurator
and clients can create these.
A client can specify their own settings here and pass the Cache
object
into CacheManager.addCache(java.lang.String)
to specify parameters other than the defaults.
Only the CacheManager can initialise them.
This constructor creates disk stores, if specified, that do not persist between restarts.
The default expiry thread interval of 120 seconds is used. This is the interval between runs
of the expiry thread, where it checks the disk store for expired elements. It is not the
the timeToLiveSeconds.
public Cache(java.lang.String name, int maximumSize, boolean overflowToDisk, boolean eternal, long timeToLiveSeconds, long timeToIdleSeconds, boolean diskPersistent, long diskExpiryThreadIntervalSeconds)
Configurator
and clients can create these.
A client can specify their own settings here and pass the Cache
object
into CacheManager.addCache(java.lang.String)
to specify parameters other than the defaults.
Only the CacheManager can initialise them.
Method Detail |
void initialise(Configuration configuration)
MemoryStore
or a DiskStore
.
This method creates those and makes the cache ready to accept elements
public void put(Element element) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException
element
-
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
java.lang.IllegalArgumentException
- if the element is nullpublic void putQuiet(Element element) throws java.lang.IllegalArgumentException, java.lang.IllegalStateException
getQuiet(java.io.Serializable)
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.
element
-
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
java.lang.IllegalArgumentException
- if the element is nullpublic Element get(java.io.Serializable key) throws java.lang.IllegalStateException, CacheException
getQuiet(java.io.Serializable)
to peak into the Element to see its last access time with get
key
- a serializable value
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
CacheException
isExpired(net.sf.ehcache.Element)
public Element getQuiet(java.io.Serializable key) throws java.lang.IllegalStateException, CacheException
key
- a serializable value
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
CacheException
isExpired(net.sf.ehcache.Element)
public java.util.List getKeys() throws java.lang.IllegalStateException, CacheException
Serializable
keys
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
CacheException
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.
Serializable
keys
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
CacheException
public java.util.List getKeysNoDuplicateCheck() throws java.lang.IllegalStateException
Serializable
keys
java.lang.IllegalStateException
- if the cache is not 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.
key
-
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public void removeAll() throws java.lang.IllegalStateException, java.io.IOException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
java.io.IOException
void dispose() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public int getSize() throws java.lang.IllegalStateException, CacheException
Element
s in the MemoryStore
plus
the number of Element
s 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.
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
CacheException
public long calculateInMemorySize() throws java.lang.IllegalStateException, CacheException
java.lang.IllegalStateException
CacheException
public long getMemoryStoreSize() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public int getDiskStoreSize() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public int getStatus()
public int getHitCount() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public int getMemoryStoreHitCount() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public int getDiskStoreHitCount() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public int getMissCountNotFound() throws java.lang.IllegalStateException
getMissCountExpired()
,
or because it was simply not there.
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public int getMissCountExpired() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public java.lang.String getName()
void setName(java.lang.String name)
public long getTimeToIdleSeconds()
public long getTimeToLiveSeconds()
public boolean isEternal()
public boolean isOverflowToDisk()
public int getMaxElementsInMemory()
public java.lang.String toString()
String
representation of Cache
public boolean isExpired(Element element) throws java.lang.IllegalStateException, java.lang.NullPointerException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
java.lang.NullPointerException
- if the element is nullpublic java.lang.Object clone() throws java.lang.CloneNotSupportedException
MemoryStore
or DiskStore
has been created.
Cache
java.lang.CloneNotSupportedException
DiskStore getDiskStore() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
MemoryStore getMemoryStore() throws java.lang.IllegalStateException
java.lang.IllegalStateException
- if the cache is not STATUS_ALIVE
public boolean isDiskPersistent()
public long getDiskExpiryThreadIntervalSeconds()
|
ehcache | ||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |