EhCache learning notes

Introduction to EhCache

CacheManager is an interface defined by Spring to manage Cache. Spring itself has provided us with two implementations of Cache manager, one is ConcurrentMap based on Java API, the other is Ehcache based on the third-party Cache implementation. ConcurrentMap does not set timeToIdleSeconds (the access interval when the element expires) and timeToLiveSeconds (the time when the element exists in the cache). The cache can only be cleaned through cacheEvict. The cache that is not cleaned will always exist.

spring and ehcache integrated configuration

maven dependence

Corresponding relationship between spring and ehcache versions:

  • spring3.1 --> ehcache2.8.3
  • spring4.2.4 --> ehcache2.10.1
<dependency>
      <groupId>net.sf.ehcache</groupId>
      <artifactId>ehcache</artifactId>
      <version>2.10.1</version>
</dependency>

Configure ehcache.xml

<?xml version="1.0" encoding="UTF-8"?>
<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd"
         updateCheck="false">
    <diskStore path="java.io.tmpdir"/>
    <defaultCache
            maxEntriesLocalHeap="10000"
            eternal="false"
            overflowToDisk="true"
            timeToIdleSeconds="120"
            timeToLiveSeconds="120"
            diskSpoolBufferSizeMB="30"
            maxEntriesLocalDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            memoryStoreEvictionPolicy="LRU">
    </defaultCache>

    <cache name="transformCache"
           maxEntriesLocalHeap="10000"
           maxEntriesLocalDisk="1000"
           eternal="false"
           diskSpoolBufferSizeMB="30"
           timeToIdleSeconds="300"
           timeToLiveSeconds="600"
           memoryStoreEvictionPolicy="LFU"
           transactionalMode="off">
    </cache>
</ehcache>

Configure springContext.xml

<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context"
    xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
       xmlns:cache="http://www.springframework.org/schema/cache"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
        http://www.springframework.org/schema/context   
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
        http://www.springframework.org/schema/tx 
        http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
        http://www.springframework.org/schema/cache
        http://www.springframework.org/schema/cache/spring-cache-4.2.xsd">

    <!-- It is necessary to enable the caching annotation function, otherwise the annotation will not take effect. In addition, the annotation must be declared in the spring Will not take effect in the main profile -->
    <cache:annotation-driven cache-manager="ehcacheManager"/>

    <!-- cacheManager Factory class, specifying ehcache.xml Location -->
    <bean id="ehcacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
        <property name="configLocation" value="config/spring/ehcache.xml" />
    </bean>
    <!-- statement cacheManager -->
    <bean id="ehcacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
        <property name="cacheManager" ref="ehcacheManagerFactory" />
    </bean>
</beans>

Tags: Ehcache Spring xml Java

Posted on Wed, 04 Dec 2019 07:56:05 -0800 by TRI0N