EhCacheFactoryBean (Spring Framework 3.2.1.RELEASE API)
Date: 2013-01-24

org.springframework.cache.ehcache

## Class EhCacheFactoryBean

- java.lang.Object
- - org.springframework.cache.ehcache.EhCacheFactoryBean

- All Implemented Interfaces:
    Aware, BeanNameAware, FactoryBean<net.sf.ehcache.Ehcache>, InitializingBean

      public class EhCacheFactoryBean
      extends Object
      implements FactoryBean<net.sf.ehcache.Ehcache>, BeanNameAware, 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.

  Since:
    1.1.1
  Author:
    Dmitriy Kopylenko, Juergen Hoeller
  See Also:
    `setCacheManager(net.sf.ehcache.CacheManager)`, `EhCacheManagerFactoryBean`, `Cache`

- - ### Field Summary

    Fields
    Modifier and Type | Field and Description
    --- | ---
    `protected Log` | `logger`

  - ### Constructor Summary

    Constructors
    Constructor and Description
    `EhCacheFactoryBean()`

  - ### Method Summary

    Methods
    Modifier and Type | Method and Description
    --- | ---
    `void` | `afterPropertiesSet()` Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).
    `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()` Return an instance (possibly shared or independent) of the object managed by this factory.
    `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()` Is the object managed by this factory a singleton? That is, will `FactoryBean.getObject()` always return the same object (a reference that can be cached)?
    `void` | `setBeanName(String name)` Set the name of the bean in the bean factory that created this bean.
    `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.

    - ### Methods inherited from class java.lang.Object

      `clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

- - ### Field Detail

    - #### logger

          protected final Log logger

  - ### Constructor Detail

    - #### EhCacheFactoryBean

          public EhCacheFactoryBean()

  - ### Method Detail

    - #### setCacheManager

          public void setCacheManager(net.sf.ehcache.CacheManager cacheManager)

      Set a CacheManager from which to retrieve a named Cache instance. By default, `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.

      See Also:
        `EhCacheManagerFactoryBean`, `CacheManager.getInstance()`

    - #### setCacheName

          public void setCacheName(String cacheName)

      Set a name for which to retrieve or create a cache instance. Default is the bean name of this EhCacheFactoryBean.

    - #### setMaxElementsInMemory

          public void setMaxElementsInMemory(int maxElementsInMemory)

      Specify the maximum number of cached objects in memory. Default is 10000 elements.

    - #### setMaxElementsOnDisk

          public void setMaxElementsOnDisk(int maxElementsOnDisk)

      Specify the maximum number of cached objects on disk. Default is 10000000 elements.

    - #### setMemoryStoreEvictionPolicy

          public void setMemoryStoreEvictionPolicy(net.sf.ehcache.store.MemoryStoreEvictionPolicy memoryStoreEvictionPolicy)

      Set the memory style eviction policy for this cache.

      Supported values are "LRU", "LFU" and "FIFO", according to the constants defined in EHCache's MemoryStoreEvictionPolicy class. Default is "LRU".

    - #### setOverflowToDisk

          public void setOverflowToDisk(boolean overflowToDisk)

      Set whether elements can overflow to disk when the in-memory cache has reached the maximum size limit. Default is "true".

    - #### setEternal

          public void setEternal(boolean eternal)

      Set whether elements are considered as eternal. If "true", timeouts are ignored and the element is never expired. Default is "false".

    - #### setTimeToLive

          public void setTimeToLive(int timeToLive)

      Set the time in seconds to live for an element before it expires, i.e. the maximum time between creation time and when an element expires.

      This is only used if the element is not eternal. Default is 120 seconds.

    - #### setTimeToIdle

          public 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.

      This is only used if the element is not eternal. Default is 120 seconds.

    - #### setDiskPersistent

          public void setDiskPersistent(boolean diskPersistent)

      Set whether the disk store persists between restarts of the Virtual Machine. Default is "false".

    - #### setDiskExpiryThreadIntervalSeconds

          public void setDiskExpiryThreadIntervalSeconds(int diskExpiryThreadIntervalSeconds)

      Set the number of seconds between runs of the disk expiry thread. Default is 120 seconds.

    - #### setDiskSpoolBufferSize

          public void setDiskSpoolBufferSize(int diskSpoolBufferSize)

      Set the amount of memory to allocate the write buffer for puts to the disk store. Default is 0.

    - #### setClearOnFlush

          public void setClearOnFlush(boolean clearOnFlush)

      Set whether the memory store should be cleared when flush is called on the cache. Default is "true".

    - #### setBlocking

          public void setBlocking(boolean blocking)

      Set whether to use a blocking cache that lets read attempts block until the requested element is created.

      If you intend to build a self-populating blocking cache, consider specifying a `CacheEntryFactory`.

      See Also:
        `BlockingCache`, `setCacheEntryFactory(net.sf.ehcache.constructs.blocking.CacheEntryFactory)`

    - #### setCacheEntryFactory

          public void setCacheEntryFactory(net.sf.ehcache.constructs.blocking.CacheEntryFactory cacheEntryFactory)

      Set an EHCache `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.

      See Also:
        `SelfPopulatingCache`, `UpdatingSelfPopulatingCache`, `UpdatingCacheEntryFactory`

    - #### setBootstrapCacheLoader

          public void setBootstrapCacheLoader(net.sf.ehcache.bootstrap.BootstrapCacheLoader bootstrapCacheLoader)

      Set an EHCache `BootstrapCacheLoader` for this cache, if any.

    - #### setCacheEventListeners

          public void setCacheEventListeners(Set<net.sf.ehcache.event.CacheEventListener> cacheEventListeners)

      Specify EHCache `cache event listeners` to registered with this cache.

    - #### setStatisticsEnabled

          public void setStatisticsEnabled(boolean statisticsEnabled)

      Set whether to enable EhCache statistics on this cache.
      See Also:
        `Cache.setStatisticsEnabled(boolean)`

    - #### setSampledStatisticsEnabled

          public void setSampledStatisticsEnabled(boolean sampledStatisticsEnabled)

      Set whether to enable EhCache's sampled statistics on this cache.
      See Also:
        `Cache.setSampledStatisticsEnabled(boolean)`

    - #### setDisabled

          public void setDisabled(boolean disabled)

      Set whether this cache should be marked as disabled.
      See Also:
        `Cache.setDisabled(boolean)`

    - #### setBeanName

          public void setBeanName(String name)

      Description copied from interface: `BeanNameAware`
      Set the name of the bean in the bean factory that created this bean.

      Invoked after population of normal bean properties but before an init callback such as `InitializingBean.afterPropertiesSet()` or a custom init-method.

      Specified by:
        `setBeanName` in interface `BeanNameAware`
      Parameters:
        `name` - the name of the bean in the factory. Note that this name is the actual bean name used in the factory, which may differ from the originally specified name: in particular for inner bean names, the actual bean name might have been made unique through appending "#..." suffixes. Use the `BeanFactoryUtils.originalBeanName(String)` method to extract the original bean name (without suffix), if desired.

    - #### afterPropertiesSet

          public void afterPropertiesSet()
                                  throws net.sf.ehcache.CacheException,
                                         IOException

      Description copied from interface: `InitializingBean`
      Invoked by a BeanFactory after it has set all bean properties supplied (and satisfied BeanFactoryAware and ApplicationContextAware).

      This method allows the bean instance to perform initialization only possible when all bean properties have been set and to throw an exception in the event of misconfiguration.

      Specified by:
        `afterPropertiesSet` in interface `InitializingBean`
      Throws:
        `net.sf.ehcache.CacheException`
        `IOException`

    - #### createCache

          protected net.sf.ehcache.Cache createCache()

      Create a raw Cache object based on the configuration of this FactoryBean.

    - #### decorateCache

          protected net.sf.ehcache.Ehcache decorateCache(net.sf.ehcache.Ehcache cache)

      Decorate the given Cache, if necessary.
      Parameters:
        `cache` - the raw Cache object, based on the configuration of this FactoryBean
      Returns:
        the (potentially decorated) cache object to be registered with the CacheManager

    - #### getObject

          public net.sf.ehcache.Ehcache getObject()

      Description copied from interface: `FactoryBean`
      Return an instance (possibly shared or independent) of the object managed by this factory.

      As with a `BeanFactory`, this allows support for both the Singleton and Prototype design pattern.

      If this FactoryBean is not fully initialized yet at the time of the call (for example because it is involved in a circular reference), throw a corresponding `FactoryBeanNotInitializedException`.

      As of Spring 2.0, FactoryBeans are allowed to return `null` objects. The factory will consider this as normal value to be used; it will not throw a FactoryBeanNotInitializedException in this case anymore. FactoryBean implementations are encouraged to throw FactoryBeanNotInitializedException themselves now, as appropriate.

      Specified by:
        `getObject` in interface `FactoryBean<net.sf.ehcache.Ehcache>`
      Returns:
        an instance of the bean (can be `null`)
      See Also:
        `FactoryBeanNotInitializedException`

    - #### getObjectType

          public 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()`.
      Specified by:
        `getObjectType` in interface `FactoryBean<net.sf.ehcache.Ehcache>`
      Returns:
        the type of object that this FactoryBean creates, or `null` if not known at the time of the call
      See Also:
        `ListableBeanFactory.getBeansOfType(java.lang.Class<T>)`

    - #### isSingleton

          public boolean isSingleton()

      Description copied from interface: `FactoryBean`
      Is the object managed by this factory a singleton? That is, will `FactoryBean.getObject()` always return the same object (a reference that can be cached)?

      NOTE: If a FactoryBean indicates to hold a singleton object, the object returned from `getObject()` might get cached by the owning BeanFactory. Hence, do not return `true` unless the FactoryBean always exposes the same reference.

      The singleton status of the FactoryBean itself will generally be provided by the owning BeanFactory; usually, it has to be defined as singleton there.

      NOTE: This method returning `false` does not necessarily indicate that returned objects are independent instances. An implementation of the extended `SmartFactoryBean` interface may explicitly indicate independent instances through its `SmartFactoryBean.isPrototype()` method. Plain `FactoryBean` implementations which do not implement this extended interface are simply assumed to always return independent instances if the `isSingleton()` implementation returns `false`.

      Specified by:
        `isSingleton` in interface `FactoryBean<net.sf.ehcache.Ehcache>`
      Returns:
        whether the exposed object is a singleton
      See Also:
        `FactoryBean.getObject()`, `SmartFactoryBean.isPrototype()`