1.pox.xml 依赖
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.4</version>
</dependency>
注:ehcache在2.5版本之后只支持单例模式。(下面例子说明)
2.相关xml文件
spring-ehcache.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:cache="http://www.springframework.org/schema/cache"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache-3.1.xsd">
<!-- 启用缓存注解功能 -->
<cache:annotation-driven />
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-config.xml"></property>
</bean>
<bean id="cacheManager" class="org.springframework.cache.ehcache.EhCacheCacheManager">
<property name="cacheManager" ref="cacheManagerFactory"></property>
</bean>
</beans>
ehcache-config.xml :
<?xml version="1.0" encoding="UTF-8"?>
<ehcache>
<!-- 指定一个文件目录,当EhCache把数据写到硬盘上时,将把数据写到这个文件目录下 -->
<diskStore path="D:\Work\Ehcache\cache"/>
<!-- 设定缓存的默认数据过期策略 -->
<!-- 内存中最多可以缓存1000个element,不是永久有效,有效时间为一小时-->
<!-- 超过1000element时,element将会输出到磁盘中,输出路径是path-->
<!-- diskPersistent:是否缓存虚拟机重启期数据。(Server重启时将缓存序列化到本地,后再加载,保证缓存在重启后依然有效)。-->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="0"
timeToLiveSeconds="3600"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"/>
<cache name="cacheTest"
maxElementsInMemory="1000"
eternal="false"
overflowToDisk="true"
timeToIdleSeconds="10"
timeToLiveSeconds="20"/>
</ehcache>
说明:如果使用 2.5及以上版本的ehcache ,项目在第一次成功启动后重启会报错: 【Another unnamed CacheManager already exists in the same VM】, 在<ehcache>添加 name="jbpmEhcacheManager" 为其命名可解决同名错误。但这种方式治标不治本,下次重启项目还是会报错。
另一种解决方法是,在spring-ehcache.xml 文件中 ,将 id为"cacheManagerFactory"的bean的shared属性设置为"true"便可解决该问题,如下
<bean id="cacheManagerFactory" class="org.springframework.cache.ehcache.EhCacheManagerFactoryBean">
<property name="configLocation" value="classpath:ehcache-config.xml"></property>
<property name="shared" value="true"></property>
</bean>
3.测试方法类EhcacheTestService
@Service
public class EhcacheTestServiceImpl implements IEhCacheService {
@Cacheable(value = "cacheTest", key = "#param")
public String getTimestamp(String param){
Long timestamp = System.currentTimeMillis();
return timestamp.toString();
}
}
4.调用方法
System.out.println("第一次调用:" + ehcacheTestService.getTimestamp("lon"));
Thread.sleep(2000);
System.out.println("2秒之后调用:" + ehcacheTestService.getTimestamp("lon"));
Thread.sleep(11000);
System.out.println("11秒之后调用:" + ehcacheTestService.getTimestamp("lon"));
5.输出结果
第一次调用:1531991473044
2秒之后调用:1531991473044
11秒之后调用:1531991486054
说明:第一次调用 getTimestamp () 缓存里还没有数据,所以打印当前时间的毫秒数;
第二次调用 , 缓存里已经有了第一次的数据,所以直接返回第一次的时间毫秒数(不访问getTimestamp 方法内部);
第三次调用,由于ehcache-config.xml里面设置的 cacheTest 的 timeToLiveSeconds(有效时间,即缓存自创建日期起至失效时的间隔时间)为20s,而timeToIdleSeconds (闲置时间,即缓存创建以后,最后一次访问缓存的日期至失效之时的时间间隔)为10s,时线程休眠11秒后调用时该缓存已失效,所以打印的是第三次调用的时间毫秒数。