Class AbstractEhcacheCache

java.lang.Object
org.mybatis.caches.ehcache.AbstractEhcacheCache
All Implemented Interfaces:
org.apache.ibatis.cache.Cache
Direct Known Subclasses:
EhBlockingCache, EhcacheCache

public abstract class AbstractEhcacheCache extends Object implements org.apache.ibatis.cache.Cache
Cache adapter for Ehcache 3.
Author:
Simone Tripodi
  • Field Details

    • CACHE_MANAGER

      protected static org.ehcache.PersistentCacheManager CACHE_MANAGER
      The cache manager reference. A PersistentCacheManager is used so that individual caches may optionally configure a disk tier via setMaxBytesLocalDisk(long).
    • id

      protected final String id
      The cache id (namespace).
    • cache

      protected org.ehcache.Cache<Object,Object> cache
      The cache instance (lazily initialised on first use).
    • timeToIdleSeconds

      protected long timeToIdleSeconds
    • timeToLiveSeconds

      protected long timeToLiveSeconds
    • maxEntriesLocalHeap

      protected long maxEntriesLocalHeap
    • maxEntriesLocalDisk

      protected long maxEntriesLocalDisk
    • maxBytesLocalDisk

      protected long maxBytesLocalDisk
    • memoryStoreEvictionPolicy

      protected String memoryStoreEvictionPolicy
  • Constructor Details

    • AbstractEhcacheCache

      public AbstractEhcacheCache(String id)
      Instantiates a new abstract ehcache cache.
      Parameters:
      id - the cache id (namespace)
  • Method Details

    • getOrCreateCache

      protected org.ehcache.Cache<Object,Object> getOrCreateCache()
      Returns the underlying Ehcache 3 cache, creating it on first use with the current configuration.
    • buildAndRegisterCache

      protected org.ehcache.Cache<Object,Object> buildAndRegisterCache()
      Builds and registers a new Ehcache 3 cache instance using the current configuration fields.
    • clear

      public void clear()
      Specified by:
      clear in interface org.apache.ibatis.cache.Cache
    • getId

      public String getId()
      Specified by:
      getId in interface org.apache.ibatis.cache.Cache
    • getObject

      public Object getObject(Object key)
      Specified by:
      getObject in interface org.apache.ibatis.cache.Cache
    • getSize

      public int getSize()
      Specified by:
      getSize in interface org.apache.ibatis.cache.Cache
    • putObject

      public void putObject(Object key, Object value)
      Specified by:
      putObject in interface org.apache.ibatis.cache.Cache
    • removeObject

      public Object removeObject(Object key)
      Specified by:
      removeObject in interface org.apache.ibatis.cache.Cache
    • unlock

      public void unlock(Object key)
    • equals

      public boolean equals(Object obj)
      Overrides:
      equals in class Object
    • hashCode

      public int hashCode()
      Overrides:
      hashCode in class Object
    • getReadWriteLock

      public ReadWriteLock getReadWriteLock()
      Specified by:
      getReadWriteLock in interface org.apache.ibatis.cache.Cache
    • toString

      public String toString()
      Overrides:
      toString in class Object
    • setTimeToIdleSeconds

      public void setTimeToIdleSeconds(long timeToIdleSeconds)
      Sets the time to idle for an element before it expires. Is only used if the element is not eternal. If the cache has already been initialised the configuration change takes effect immediately by recreating the cache.
      Parameters:
      timeToIdleSeconds - the default amount of time to live for an element from its last accessed or modified date
    • setTimeToLiveSeconds

      public void setTimeToLiveSeconds(long timeToLiveSeconds)
      Sets the time to live for an element before it expires. Is only used if the element is not eternal. If the cache has already been initialised the configuration change takes effect immediately by recreating the cache.
      Parameters:
      timeToLiveSeconds - the default amount of time to live for an element from its creation date
    • setMaxEntriesLocalHeap

      public void setMaxEntriesLocalHeap(long maxEntriesLocalHeap)
      Sets the maximum objects to be held in memory (0 = no limit). If the cache has already been initialised the configuration change takes effect immediately by recreating the cache.
      Parameters:
      maxEntriesLocalHeap - The maximum number of elements in heap, before they are evicted (0 == no limit)
    • setMaxEntriesLocalDisk

      public void setMaxEntriesLocalDisk(long maxEntriesLocalDisk)
      Sets the maximum number elements on Disk. 0 means unlimited.

      Note: this property is retained for compatibility but has no effect in Ehcache 3, which does not support an entry-count limit for the disk tier. Use setMaxBytesLocalDisk(long) to configure disk storage instead.

      Parameters:
      maxEntriesLocalDisk - the maximum number of Elements to allow on the disk. 0 means unlimited.
    • setMaxBytesLocalDisk

      public void setMaxBytesLocalDisk(long maxBytesLocalDisk)
      Sets the maximum bytes to be used for the disk tier. When greater than zero a disk resource pool is added to the cache, allowing entries evicted from the heap to overflow to disk. If set to zero (the default) no disk tier is configured and the cache is heap-only.
      Parameters:
      maxBytesLocalDisk - the maximum number of bytes to allocate on disk. 0 means no disk tier (heap-only).
    • setMemoryStoreEvictionPolicy

      public void setMemoryStoreEvictionPolicy(String memoryStoreEvictionPolicy)
      Sets the eviction policy. Stored for informational purposes; Ehcache 3 manages its own eviction strategy.
      Parameters:
      memoryStoreEvictionPolicy - a String representation of the policy. One of "LRU", "LFU" or "FIFO".
    • recreateCacheIfInitialized

      protected void recreateCacheIfInitialized()
      Recreates the underlying Ehcache 3 cache with the current configuration if the cache has already been initialised. Called by property setters when a configuration change is requested after first use.