ehcache3配置文件
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' xmlns:jsr107='http://www.ehcache.org/v3/jsr107' xmlns='http://www.ehcache.org/v3'
xsi:schemaLocation="
http://www.ehcache.org/v3 http://www.ehcache.org/schema/ehcache-core-3.1.xsd
http://www.ehcache.org/v3/jsr107 http://www.ehcache.org/schema/ehcache-107-ext-3.1.xsd">
<!-- 指定硬盘存储目录,如用不到硬盘缓存,无需设置 -->
<persistence directory="D:/dataDir" />
<cache alias="myCache">
<key-type>java.lang.Integer</key-type>
<value-type>java.lang.String</value-type>
<expiry>
<!-- tti / ttl 策略只能选择一种,不能同时选择两种,与ehcache2不同 -->
<!-- <tti unit="seconds">30</tti> --> <!-- 设定缓存数据多久没有访问则过期 -->
<ttl unit="seconds">15</ttl> <!-- 设定缓存数据过期时间 -->
</expiry>
<resources>
<heap unit="entries">20</heap>
<offheap unit="MB">10</offheap>
<!--persistent= true开启持久化,即重启服务缓存还存在 -->
<disk persistent="true" unit="MB">500</disk>
</resources>
</cache>
<!-- cache-template 创建公用配置,被继承用 -->
<cache-template name="myDefaults">
<key-type>java.lang.Long</key-type>
<value-type>java.lang.String</value-type>
<heap unit="entries">200</heap>
</cache-template>
<!-- 继承自myDefaults,并重写其参数 -->
<cache alias="bar" uses-template="myDefaults">
<key-type>java.lang.Number</key-type>
</cache>
</config>
spring 引入 ehcache3
spring未对ehcache3进行直接的支持(对ehcache2直接支持)
但ehcache3对Jcache规范做了适配,spring对Jcache支持关于Jcache 和 ehcache2会另写两个博文
<!-- 指定ehcache3的配置文件 -->
<bean id="ehcache"
class="org.springframework.cache.jcache.JCacheManagerFactoryBean">
<property name="cacheManagerUri" value="classpath:ehcache-config.xml" />
</bean>
<bean id="cacheManager" class="org.springframework.cache.jcache.JCacheCacheManager">
<property name="cacheManager" ref="ehcache" />
</bean>
<!-- 开启注解 -->
<cache:annotation-driven cache-manager="cacheManager" />
通过注解使用缓存功能
注意:只有被spring管理的bean,加注解才有效。 spring在生成bean时会生成代理将缓存功能加入
/**调用该方法,自动进行缓存。 value为配置文件内缓存的名称 **/
@Cacheable(value="myCache",key="#id")
public String getUser(int id){
System.out.println("进行了计算");
return "用户"+id;
}
/**调用该方法,自动删除缓存**/
@CacheEvict(value="myCache",key="#id")
public String deleteUserCache(int id){
System.out.println("对用户进行了删除");
return "用户"+id;
}
进行单元测试
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations ={"/applicationContext.xml"})
public class EhcacheTest {
@Autowired
UserService userService ;
/**测试缓存是否生效 , 以及断开服务后重启缓存是否还存在**/
/** 测试结果 服务重启,缓存依然生效, 调用方法直接去取缓存 **/
/**所有缓存都持久化在硬盘上**/
@Test
public void test1() {
for (int i=0; i<20; i++) {
System.out.println(userService.getUser(i));
}
for (int i=0; i<20; i++) {
System.out.println(userService.getUser(i));
}
}
/**测试移除缓存**/
@Test
public void test2(){
userService.getUser(21); //首次进行了计算
userService.getUser(21); //未进行计算,取了缓存
userService.deleteUserCache(21);
userService.getUser(21); //缓存被移除,重新计算
}
/**测试缓存过期是否有效 **/
/**测试结果,缓存在15秒后过期,重新计算**/
@Test
public void test3() throws InterruptedException{
while(true){
System.out.println(userService.getUser(1));
Thread.sleep(1000);
}
}
}
版权声明:本文为yi_shen_zhi_nu原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。