public class EhCacheFactoryBean extends Object implements org.springframework.beans.factory.FactoryBean<net.sf.ehcache.Ehcache>, org.springframework.beans.factory.BeanNameAware, org.springframework.beans.factory.InitializingBean
FactoryBean
that creates a named EHCache Cache
instance
(or a decorator that implements the Ehcache
interface),
representing a cache region within an EHCache CacheManager
.
If the specified named cache is not configured in the cache configuration descriptor, this FactoryBean will construct an instance of a Cache with the provided name and the specified cache properties and add it to the CacheManager for later retrieval. If some or all properties are not set at configuration time, this FactoryBean will use defaults.
Note: If the named Cache instance is found, the properties will be ignored and the Cache instance will be retrieved from the CacheManager.
Note: As of Spring 3.0, Spring's EHCache support requires EHCache 1.3 or higher.
setCacheManager(net.sf.ehcache.CacheManager)
,
EhCacheManagerFactoryBean
,
Cache
Constructor and Description |
---|
EhCacheFactoryBean() |
Modifier and Type | Method and Description |
---|---|
void |
afterPropertiesSet() |
protected net.sf.ehcache.Cache |
createCache()
Create a raw Cache object based on the configuration of this FactoryBean.
|
protected net.sf.ehcache.Ehcache |
decorateCache(net.sf.ehcache.Ehcache cache)
Decorate the given Cache, if necessary.
|
net.sf.ehcache.Ehcache |
getObject() |
Class<? extends net.sf.ehcache.Ehcache> |
getObjectType()
Predict the particular
Ehcache implementation that will be returned from
getObject() based on logic in createCache() and
decorateCache(Ehcache) as orchestrated by afterPropertiesSet() . |
boolean |
isSingleton() |
void |
setBeanName(String name) |
void |
setBlocking(boolean blocking)
Set whether to use a blocking cache that lets read attempts block
until the requested element is created.
|
void |
setBootstrapCacheLoader(net.sf.ehcache.bootstrap.BootstrapCacheLoader bootstrapCacheLoader)
Set an EHCache
BootstrapCacheLoader
for this cache, if any. |
void |
setCacheEntryFactory(net.sf.ehcache.constructs.blocking.CacheEntryFactory cacheEntryFactory)
Set an EHCache
CacheEntryFactory
to use for a self-populating cache. |
void |
setCacheEventListeners(Set<net.sf.ehcache.event.CacheEventListener> cacheEventListeners)
Specify EHCache
cache event listeners
to registered with this cache. |
void |
setCacheManager(net.sf.ehcache.CacheManager cacheManager)
Set a CacheManager from which to retrieve a named Cache instance.
|
void |
setCacheName(String cacheName)
Set a name for which to retrieve or create a cache instance.
|
void |
setClearOnFlush(boolean clearOnFlush)
Set whether the memory store should be cleared when flush is called on the cache.
|
void |
setDisabled(boolean disabled)
Set whether this cache should be marked as disabled.
|
void |
setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds)
Set the number of seconds between runs of the disk expiry thread.
|
void |
setDiskPersistent(boolean diskPersistent)
Set whether the disk store persists between restarts of the Virtual Machine.
|
void |
setDiskSpoolBufferSize(int diskSpoolBufferSize)
Set the amount of memory to allocate the write buffer for puts to the disk store.
|
void |
setEternal(boolean eternal)
Set whether elements are considered as eternal.
|
void |
setMaxElementsInMemory(int maxElementsInMemory)
Specify the maximum number of cached objects in memory.
|
void |
setMaxElementsOnDisk(int maxElementsOnDisk)
Specify the maximum number of cached objects on disk.
|
void |
setMemoryStoreEvictionPolicy(net.sf.ehcache.store.MemoryStoreEvictionPolicy memoryStoreEvictionPolicy)
Set the memory style eviction policy for this cache.
|
void |
setOverflowToDisk(boolean overflowToDisk)
Set whether elements can overflow to disk when the in-memory cache
has reached the maximum size limit.
|
void |
setSampledStatisticsEnabled(boolean sampledStatisticsEnabled)
Set whether to enable EhCache's sampled statistics on this cache.
|
void |
setStatisticsEnabled(boolean statisticsEnabled)
Set whether to enable EhCache statistics on this cache.
|
void |
setTimeToIdle(int timeToIdle)
Set the time in seconds to idle for an element before it expires, that is,
the maximum amount of time between accesses before an element expires.
|
void |
setTimeToLive(int timeToLive)
Set the time in seconds to live for an element before it expires,
i.e.
|
protected final Log logger
public void setCacheManager(net.sf.ehcache.CacheManager cacheManager)
CacheManager.getInstance()
will be called.
Note that in particular for persistent caches, it is advisable to properly handle the shutdown of the CacheManager: Set up a separate EhCacheManagerFactoryBean and pass a reference to this bean property.
A separate EhCacheManagerFactoryBean is also necessary for loading EHCache configuration from a non-default config location.
EhCacheManagerFactoryBean
,
CacheManager.getInstance()
public void setCacheName(String cacheName)
public void setMaxElementsInMemory(int maxElementsInMemory)
public void setMaxElementsOnDisk(int maxElementsOnDisk)
public void setMemoryStoreEvictionPolicy(net.sf.ehcache.store.MemoryStoreEvictionPolicy memoryStoreEvictionPolicy)
Supported values are "LRU", "LFU" and "FIFO", according to the constants defined in EHCache's MemoryStoreEvictionPolicy class. Default is "LRU".
public void setOverflowToDisk(boolean overflowToDisk)
public void setEternal(boolean eternal)
public void setTimeToLive(int timeToLive)
This is only used if the element is not eternal. Default is 120 seconds.
public void setTimeToIdle(int timeToIdle)
This is only used if the element is not eternal. Default is 120 seconds.
public void setDiskPersistent(boolean diskPersistent)
public void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds)
public void setDiskSpoolBufferSize(int diskSpoolBufferSize)
public void setClearOnFlush(boolean clearOnFlush)
public void setBlocking(boolean blocking)
If you intend to build a self-populating blocking cache,
consider specifying a CacheEntryFactory
.
BlockingCache
,
setCacheEntryFactory(net.sf.ehcache.constructs.blocking.CacheEntryFactory)
public void setCacheEntryFactory(net.sf.ehcache.constructs.blocking.CacheEntryFactory cacheEntryFactory)
CacheEntryFactory
to use for a self-populating cache. If such a factory is specified,
the cache will be decorated with EHCache's
SelfPopulatingCache
.
The specified factory can be of type
UpdatingCacheEntryFactory
,
which will lead to the use of an
UpdatingSelfPopulatingCache
.
Note: Any such self-populating cache is automatically a blocking cache.
SelfPopulatingCache
,
UpdatingSelfPopulatingCache
,
UpdatingCacheEntryFactory
public void setBootstrapCacheLoader(net.sf.ehcache.bootstrap.BootstrapCacheLoader bootstrapCacheLoader)
BootstrapCacheLoader
for this cache, if any.public void setCacheEventListeners(Set<net.sf.ehcache.event.CacheEventListener> cacheEventListeners)
cache event listeners
to registered with this cache.public void setStatisticsEnabled(boolean statisticsEnabled)
Cache.setStatisticsEnabled(boolean)
public void setSampledStatisticsEnabled(boolean sampledStatisticsEnabled)
Cache.setSampledStatisticsEnabled(boolean)
public void setDisabled(boolean disabled)
Cache.setDisabled(boolean)
public void setBeanName(String name)
setBeanName
in interface org.springframework.beans.factory.BeanNameAware
public void afterPropertiesSet() throws net.sf.ehcache.CacheException, IOException
afterPropertiesSet
in interface org.springframework.beans.factory.InitializingBean
net.sf.ehcache.CacheException
IOException
protected net.sf.ehcache.Cache createCache()
protected net.sf.ehcache.Ehcache decorateCache(net.sf.ehcache.Ehcache cache)
cache
- the raw Cache object, based on the configuration of this FactoryBeanpublic net.sf.ehcache.Ehcache getObject()
getObject
in interface org.springframework.beans.factory.FactoryBean<net.sf.ehcache.Ehcache>
public Class<? extends net.sf.ehcache.Ehcache> getObjectType()
Ehcache
implementation that will be returned from
getObject()
based on logic in createCache()
and
decorateCache(Ehcache)
as orchestrated by afterPropertiesSet()
.getObjectType
in interface org.springframework.beans.factory.FactoryBean<net.sf.ehcache.Ehcache>
public boolean isSingleton()
isSingleton
in interface org.springframework.beans.factory.FactoryBean<net.sf.ehcache.Ehcache>