Top Description Methods
net.sf.ehcache

public Interface Ehcache

extends Cloneable
Known Direct Subinterfaces
net.sf.ehcache.terracotta.InternalEhcache
Known Direct Implementers
net.sf.ehcache.constructs.classloader.ClassLoaderAwareCache
Imports
java.beans.PropertyChangeListener, java.io.Serializable, java.util.Collection, .List, .Map, .Set, net.sf.ehcache.bootstrap.BootstrapCacheLoader, net.sf.ehcache.config.CacheConfiguration, .NonstopConfiguration, net.sf.ehcache.event.RegisteredEventListeners, net.sf.ehcache.exceptionhandler.CacheExceptionHandler, net.sf.ehcache.extension.CacheExtension, net.sf.ehcache.loader.CacheLoader, net.sf.ehcache.search.Attribute, .Query, net.sf.ehcache.search.attribute.DynamicAttributesExtractor, net.sf.ehcache.statistics.CacheUsageListener, .StatisticsGateway, net.sf.ehcache.terracotta.TerracottaNotRunningException, net.sf.ehcache.transaction.manager.TransactionManagerLookup, net.sf.ehcache.writer.CacheWriter, .CacheWriterManager

An interface for Ehcache.

Ehcache is the central interface. Caches have Elements and are managed by the CacheManager. The Cache performs logical actions. It delegates physical implementations to its net.sf.ehcache.store.Stores.

A reference to an EhCache can be obtained through the CacheManager. An Ehcache thus obtained is guaranteed to have status 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.

Statistics on cache usage are collected and made available through public methods.

Author
Greg Luck
Version
$Id: Ehcache.java 10789 2018-04-26 02:08:13Z adahanne $

Method Summary

Modifier and TypeMethod and Description
public void
acquireReadLockOnKey(Object
- The key that retrieves a value that you want to protect via locking
key
)

Acquires the proper read lock for a given cache key

public void
acquireWriteLockOnKey(Object
- The key that retrieves a value that you want to protect via locking
key
)

Acquires the proper write lock for a given cache key

public void
addPropertyChangeListener(PropertyChangeListener
the listener to add
listener
)

Add a PropertyChangeListener.

public void
bootstrap()

Bootstrap command.

public long

Returns:

the approximate size of the memory store in bytes
calculateInMemorySize
()

Deprecated
Gets the size of the memory store for this cache

Warning

This method can be very expensive to run.

public long

Returns:

the size of the off-heap store in bytes
calculateOffHeapSize
()

Deprecated
Gets the size of the off-heap store for this cache
public long

Returns:

the size of the on-disk store in bytes
calculateOnDiskSize
()

Deprecated
Gets the size of the on-disk store for this cache
public Object

Returns:

an object of type net.sf.ehcache.Cache
clone
()

Clones a cache.

public Query

Returns:

a new query builder
createQuery
()

Create a new query builder for this cache

public void
disableDynamicFeatures()

Disables dynamic configuration and disable/enable for this cache.

public void
dispose()

Flushes all cache items from memory to auxilliary caches and close the auxilliary caches.

public void
evictExpiredElements()

Causes all elements stored in the Cache to be synchronously checked for expiry, and if expired, evicted.

public void
flush()

Flushes all cache items from memory to the disk store, and from the DiskStore to disk.

public Element

Returns:

the element, or null, if it does not exist.
get
(Serializable
a serializable value
key
)

Gets an element from the cache.

public Element

Returns:

the element, or null, if it does not exist.
get
(Object
an Object value
key
)

Gets an element from the cache.

public Map<Object, Element>

Returns:

Map of key and elements for the provided keys, value will be null for the keys which do not exist
getAll
(Collection<?>
a collection of keys for which value is to be fetched
keys
)

Gets all the elements from the cache for the keys provided.

public Map<K, V>

Returns:

a Map populated from the Cache. If there are no elements, an empty Map is returned.
getAllWithLoader
(Collection<E>
a collection of keys to be returned/loaded
keys
,
Object
an argument to pass to the CacheLoader.
loaderArgument
)

The getAll method will return, from the cache, a Map of the objects associated with the Collection of keys in argument "keys".

public BootstrapCacheLoader

Returns:

the BootstrapCacheLoader to use
getBootstrapCacheLoader
()

Accessor for the BootstrapCacheLoader associated with this cache.

public CacheConfiguration

Returns:

CacheConfiguration the cache configuration
getCacheConfiguration
()

Gets the cache configuration this cache was created with.

public RegisteredEventListeners

Returns:

the RegisteredEventListeners instance for this cache.
getCacheEventNotificationService
()

Use this to access the service in order to register and unregister listeners

public CacheExceptionHandler

Returns:

an ExceptionHandler on the Cache
getCacheExceptionHandler
()

public CacheManager

Returns:

the manager or null if there is none
getCacheManager
()

Gets the CacheManager managing this cache.

public int

Returns:

the number of elements in the disk store.
getDiskStoreSize
()

Deprecated
Returns the number of elements in the disk store.
public String

Returns:

the globally unique identifier for this cache instance. This is guaranteed to be unique.
getGuid
()

The GUID for this cache instance can be used to determine whether two cache instance references are pointing to the same cache.

public Object

Returns:

some internal context (generally will be null)
getInternalContext
()

This should not be used

public List<E>

Returns:

a list of Object keys
getKeys
()

Returns a list of all elements in the cache, whether or not they are expired.

public List<E>

Returns:

a list of Object keys
getKeysNoDuplicateCheck
()

Deprecated versions since 2.1 do not return duplicates
Returns a list of all elements in the cache, whether or not they are expired.
public List<E>

Returns:

a list of Object keys
getKeysWithExpiryCheck
()

Returns a list of all elements in the cache.

public long

Returns:

the number of elements in the memory store
getMemoryStoreSize
()

Deprecated
Returns the number of elements in the memory store.
public String

Returns:

the name
getName
()

Gets the cache name.

public long

Returns:

the number of elements in the off-heap store
getOffHeapStoreSize
()

Deprecated
Returns the number of elements in the off-heap store.
public Element

Returns:

the element, or null, if it does not exist.
getQuiet
(Serializable
a serializable value
key
)

Gets an element from the cache, without updating Element statistics.

public Element

Returns:

the element, or null, if it does not exist.
getQuiet
(Object
a serializable value
key
)

Gets an element from the cache, without updating Element statistics.

public List<CacheExtension>

Returns:

the cache extensions as a live list
getRegisteredCacheExtensions
()

public List<CacheLoader>

Returns:

the cache loaders as a live list
getRegisteredCacheLoaders
()

public CacheWriter

Returns:

the registered CacheWriter; or null if none was registered before
getRegisteredCacheWriter
()

Retrieves the CacheWriter that was registered for this cache.

public <
type of the attribute
T
>
Attribute<T>

Returns:

the search attribute
getSearchAttribute
(String
the name of the attribute to retrieve
attributeName
)

Retrieve the given named search attribute

public Set<Attribute<T>>

Returns:

set of all search attributes in effect at the time of calling this method
getSearchAttributes
()

public int

Returns:

The size value
getSize
()

Gets the size of the cache.

public StatisticsGateway

Returns:

the statistics gateway
getStatistics
()

Gets an immutable Statistics object representing the Cache statistics at the time.

public Status

Returns:

The status value from the Status enum class
getStatus
()

Gets the status attribute of the Cache.

public Element

Returns:

an element if it existed or could be loaded, otherwise null
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
)

This method will return, from the cache, the object associated with the argument "key".

public CacheWriterManager

Returns:

the writer manager that's set up for this cache
getWriterManager
()

Obtain the writer manager that's used by this cache instance.

public boolean

Returns:

true if the cache may contain partially sized objects
hasAbortedSizeOf
()

Check if the cache may contain elements which the SizeOf engine could not fully size.

public void
public boolean

Returns:

true if the cache is in bulk-load mode cluster-wide, false otherwise
isClusterBulkLoadEnabled
()

Returns true if at least one node in the cluster is in bulk-load mode.

public boolean

Returns:

true if the cache is in coherent mode cluster-wide, false otherwise
isClusterCoherent
()

Deprecated Use isClusterBulkLoadEnabled() instead
Returns true if the cache is in coherent mode cluster-wide.
public boolean

Returns:

true if the cache is disabled.
isDisabled
()

Whether this cache is disabled.

public boolean

Returns:

true if an element matching the key is found in memory
isElementInMemory
(Serializable
the key of the element to remove
key
)

Whether an Element is stored in the cache in Memory, indicating a very low cost of retrieval.

public boolean

Returns:

true if an element matching the key is found in memory
isElementInMemory
(Object
the key of the element to remove
key
)

Whether an Element is stored in the cache in Memory, indicating a very low cost of retrieval.

public boolean

Returns:

true if an element matching the key is found in the diskStore
isElementOnDisk
(Serializable
the key of the element to remove
key
)

Whether an Element is stored in the cache on Disk, indicating a higher cost of retrieval.

public boolean

Returns:

true if an element matching the key is found in the diskStore
isElementOnDisk
(Object
the key of the element to remove
key
)

Whether an Element is stored in the cache on Disk, indicating a higher cost of retrieval.

public boolean

Returns:

true if it has expired
isExpired
(Element
the element to check
element
)

Checks whether this cache element has expired.

public boolean

Returns:

true if an Element matching the key is found in the cache. No assertions are made about the state of the Element.
isKeyInCache
(Object
the key to check for
key
)

An inexpensive check to see if the key exists in the cache.

public boolean

Returns:

true if the cache is in coherent mode cluster-wide, false otherwise
isNodeBulkLoadEnabled
()

Returns true if the current node is in bulk-load mode.

public boolean

Returns:

true if the cache is in coherent mode cluster-wide, false otherwise
isNodeCoherent
()

Deprecated Use isNodeBulkLoadEnabled() instead
Returns true if the cache is in coherent mode for the current node.
public boolean

Returns:

true if a read lock for the key is held by the current thread
isReadLockedByCurrentThread
(Object
the key
key
)

Returns true if a read lock for the key is held by the current thread

public boolean

Returns:

true if this cache is searchable
isSearchable
()

Is this cache searchable?

public boolean

Returns:

true if an Element matching the key is found in the cache. No assertions are made about the state of the Element.
isValueInCache
(Object
to check for
value
)

An extremely expensive check to see if the value exists in the cache.

public boolean

Returns:

true if a write lock for the key is held by the current thread
isWriteLockedByCurrentThread
(Object
the key
key
)

Returns true if a write lock for the key is held by the current thread

public void
load(final Object
key whose associated value to be loaded using the associated cacheloader if this cache doesn't contain it.
key
)

The load method provides a means to "pre load" the cache.

public void
loadAll(final 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
,
final Object
can be anything that makes sense to the loader

The Ehcache native API provides similar functionality to loaders using the decorator net.sf.ehcache.constructs.blocking.SelfPopulatingCache

argument
)

The loadAll method provides a means to "pre load" objects into the cache.

public void
put(Element
An object. If Serializable it can fully participate in replication and the DiskStore.
element
)

Put an element in the cache.

public void
put(Element
An object. If Serializable it can fully participate in replication and the DiskStore.
element
,
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
)

Put an element in the cache.

public void
putAll(Collection<Element>
the collection of elements to be put in the cache.
elements
)

Puts a collection of elements in to the cache.

public Element

Returns:

the element previously cached for this key, or null if none.
putIfAbsent
(Element
element to be added
element
)

Put an element in the cache if no element is currently mapped to the elements key.

public Element

Returns:

the element previously cached for this key, or null if none.
putIfAbsent
(Element
element to be added
element
,
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
)

Put an element in the cache if no element is currently mapped to the elements key.

public void
putQuiet(Element
An object. If Serializable it can fully participate in replication and the DiskStore.
element
)

Put an element in the cache, without updating statistics, or updating listeners.

public void
putWithWriter(Element
An object. If Serializable it can fully participate in replication and the DiskStore.
element
)

Put an element in the cache writing through a CacheWriter.

public void
registerCacheExtension(CacheExtension
the cache extension
cacheExtension
)

Register a CacheExtension with the cache.

public void
registerCacheLoader(CacheLoader
A Cache Loader to register
cacheLoader
)

Register a CacheLoader with the cache.

public void
registerCacheWriter(CacheWriter
A CacheWriter to register
cacheWriter
)

Register the CacheWriter for this cache.

public void
registerDynamicAttributesExtractor(DynamicAttributesExtractor
the dynamic attribute extractor
extractor
)

Allows user to register a dynamic attribute extractor with a searchable cache that is dynamically indexable, as indicated by its configuration.

public void
releaseReadLockOnKey(Object
- The key that retrieves a value that you want to protect via locking
key
)

Release a held read lock for the passed in key

public void
releaseWriteLockOnKey(Object
- The key that retrieves a value that you want to protect via locking
key
)

Release a held write lock for the passed in key

public boolean

Returns:

true if the element was removed, false if it was not found in the cache
remove
(Serializable
the key of the element to remove
key
)

Removes an net.sf.ehcache.Element from the Cache.

public boolean

Returns:

true if the element was removed, false if it was not found in the cache
remove
(Object
the key of the element to remove
key
)

Removes an net.sf.ehcache.Element from the Cache.

public boolean

Returns:

true if the element was removed, false if it was not found in the cache
remove
(Serializable
the key of the element to remove
key
,
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
)

Removes an net.sf.ehcache.Element from the Cache.

public boolean

Returns:

true if the element was removed, false if it was not found in the cache
remove
(Object
of the element to remove
key
,
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
)

Removes an net.sf.ehcache.Element from the Cache.

public void
removeAll(Collection<?>
a collection of keys to operate on
keys
)

Removes given set of net.sf.ehcache.Element from the Cache.

public void
removeAll(Collection<?>
a collection of keys to operate on
keys
,
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
)

Removes all cached items.

public void
removeAll()

Removes all cached items.

public void
removeAll(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
)

Removes all cached items.

public boolean

Returns:

true if the value was removed
removeElement
(Element
Element to be removed
element
)

Remove the Element mapped to the key for the supplied element if the value of the supplied Element compares equal to the value of the cached Element.

public void
removePropertyChangeListener(PropertyChangeListener
the listener to remove
listener
)

Remove a PropertyChangeListener.

public boolean

Returns:

true if the element was removed, false if it was not found in the cache
removeQuiet
(Serializable
of the element to remove
key
)

Removes an net.sf.ehcache.Element from the Cache, without notifying listeners.

public boolean

Returns:

true if the element was removed, false if it was not found in the cache
removeQuiet
(Object
of the element to remove
key
)

Removes an net.sf.ehcache.Element from the Cache, without notifying listeners.

public boolean

Returns:

true if the element was removed, false if it was not found in the cache
removeWithWriter
(Object
of the element to remove
key
)

Removes an net.sf.ehcache.Element from the Cache and any stores it might be in. This also removes through to a CacheWriter.

public boolean

Returns:

true if the Element was replaced
replace
(Element
Element to be test against
old
,
Element
Element to be cached
element
)

Replace the cached element only if the current Element is equal to the supplied old Element.

public Element

Returns:

the Element previously cached for this key, or null if no Element was cached
replace
(Element
Element to be cached
element
)

Replace the cached element only if an Element is currently cached for this key

public void
setBootstrapCacheLoader(BootstrapCacheLoader
the loader to be used
bootstrapCacheLoader
)

Sets the bootstrap cache loader.

public void
setCacheExceptionHandler(CacheExceptionHandler
the cache exception handler
cacheExceptionHandler
)

Sets an ExceptionHandler on the Cache.

public void
setCacheManager(CacheManager
the CacheManager for this cache to use.
cacheManager
)

Sets the CacheManager

public void
setDisabled(boolean
true if you wish to disable, false to enable
disabled
)

Disables or enables this cache.

public void
setName(String
the name of the cache. Should not be null.
name
)

Sets the cache name which will name.

public void
setNodeBulkLoadEnabled(boolean
true enables bulk-load, false disables it if not already disabled
enabledBulkLoad
)

Enable/disable bulk-load mode in this node for this cache.

public void
setNodeCoherent(boolean
true transitions to coherent mode, false to incoherent mode
coherent
)

Deprecated Use setNodeBulkLoadEnabled(boolean) instead
Sets the cache in coherent or incoherent mode depending on the parameter on this node.
public void
setTransactionManagerLookup(TransactionManagerLookup
the transactionManagerLookup
transactionManagerLookup
)

This class is used to access the transaction manager used during XA.

public String
toString()

Returns a String representation of net.sf.ehcache.Cache.

public boolean

Returns:

whether the lock was awarded
tryReadLockOnKey
(Object
- The key that retrieves a value that you want to protect via locking
key
,
long
- millis until giveup on getting the lock
timeout
)

Try to get a read lock on a given key.

public boolean

Returns:

whether the lock was awarded
tryWriteLockOnKey
(Object
- The key that retrieves a value that you want to protect via locking
key
,
long
- millis until giveup on getting the lock
timeout
)

Try to get a write lock on a given key.

public void
unregisterCacheExtension(CacheExtension
the cache extension
cacheExtension
)

Unregister a CacheExtension with the cache.

public void
unregisterCacheLoader(CacheLoader
A Cache Loader to unregister
cacheLoader
)

Unregister a CacheLoader with the cache.

public void
unregisterCacheWriter()

Unregister the CacheWriter from the cache.

public void
waitUntilClusterBulkLoadComplete()

This method waits until all the connected nodes have disabled bulk-load.

public void
waitUntilClusterCoherent()

Deprecated Use waitUntilClusterBulkLoadComplete() instead
This method waits until the cache is in coherent mode in all the connected nodes.