Spring Boot集成 Ehcache

在pom中引入相应的jar包

 <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
        </dependency>
        <dependency>
            <groupId>net.sf.ehcache</groupId>
            <artifactId>ehcache</artifactId>
            <version>2.8.3</version>
 </dependency>

在resources目录下面创建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">
<!--	//对象是否永久有效,一但设置了,timeout将不起作用。-->
<!--	eternal="false"-->
<!--	//内存缓存最大数目-->
<!--	maxElementsInMemory="1000"-->
<!--	//硬盘最大缓存个数-->
<!--	maxElementsOnDisk="5000"-->
<!--	//当数量达到maxElementsInMemory,Ehcache将会对象写到磁盘中-->
<!--	overflowToDisk="false"-->
<!--	//是否缓存虚拟机重启期数据-->
<!--	diskPersistent="false"-->
<!--	缓存自失效前允许闲置的时间-->
<!--	timeToIdleSeconds="0"-->
<!--	缓存自失效前允许存货的时间-->
<!--	timeToLiveSeconds="600"-->
<!--	//当达到缓存最大值限制时,缓存清除策略-->
<!--	memoryStoreEvictionPolicy="LRU"-->
	<!--		memoryStoreEvictionPolicy:可选策略有:LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)。 -->
	<diskStore path="java.io.tmpdir" />
	<defaultCache
	eternal="false"
	maxElementsInMemory="10000"
	maxElementsOnDisk="120"
	overflowToDisk="false"
	diskPersistent="false"
	timeToIdleSeconds="120"
	timeToLiveSeconds="120"
	memoryStoreEvictionPolicy="LRU" />

	<!-- 缓存名称demo -->
	<cache
	name="demo"
	eternal="false"
	timeToIdleSeconds="120"
	timeToLiveSeconds="120"
	diskExpiryThreadIntervalSeconds="60"
	maxElementsInMemory="50"
	overflowToDisk="false"
	diskPersistent="false"
	memoryStoreEvictionPolicy="LRU" />

</ehcache>

在application.properties文件中,通过配置来指定EhCache配置文件的位置

spring.cache.jcache.config=classpath:ehcache.xml

在启动类上添加@EnableCaching开启缓存

@EnableCaching

创建serverImpl,注意只能在实现类方法上添加,不能在接口的方法上。


import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
/**
 * 创 建 人 : wangsheng 创建日期:2019年12月
 */
@Service
public class InfoServiceImpl {
    //value属性表示使用哪个缓存策略,缓存策略在ehcache.xml中
    //LRU(最近最少使用,默认策略)、FIFO(先进先出)、LFU(最少访问次数)
    public static final String DEMO_CACHE_NAME = "demo";//这个demo和ehcache中的 name对应

    //所有key 中的单引号 都不能少,不然会被识别成一个对象
    @Cacheable(value = DEMO_CACHE_NAME, key = "'info_'+#id")
    public String findNameById(Integer id) {
        //Cacheable 不存在这个key 才会进入方法
        System.out.println("没有走缓存:" + id);
         return "wangsheng";
    }

    @CacheEvict(value = DEMO_CACHE_NAME, key = "'info_'+#id")//清除缓存:是根据这个key来清理的,没有找到key对应的缓存就没清理了
 public void delete(Integer id) {
    //数据库删除操作……(略)
}


    @CachePut(value = DEMO_CACHE_NAME, key = "'info_'+#id")
    public void update(Integer id) {
        /*
        @cachePut 表示重新放入缓存对象,不管是否存在这个key的缓存,所以一定会进入方法
        */
       
    }

}

创建测试类

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.beans.factory.annotation.Autowired;
@RunWith(SpringRunner.class)
@SpringBootTest
public class controller {
  @Autowired
    InfoServiceImpl infoService;
    @Test
   public void dd(){
        for(int i=0;i<10;i++){
       String cc=infoService.findNameById(1);
       System.out.println(cc);

        }
   }
}

可以看到只有第一次进了方法,其余几次都走了缓存。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

今朝花落悲颜色

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值