springboot整合ehcache

引入依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-cache</artifactId>
</dependency>

<dependency>
    <groupId>net.sf.ehcache</groupId>
    <artifactId>ehcache</artifactId>
</dependency>

配置application.yml

spring:
  cache:
    ehcache:
      config: 'classpath:ehcache.xml'

创建ehcache.xml文件

<ehcache xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:noNamespaceSchemaLocation="http://ehcache.org/ehcache.xsd">

    <diskStore path="java.io.tmpdir"/>

    <!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 -->
    <!-- eternal:elements是否永久有效,如果为true,timeouts将被忽略,element将永不过期 -->
    <!-- maxElementsInMemory: 在内存中缓存的element的最大数目。-->
    <!-- timeToIdleSeconds:失效前的空闲秒数,当eternal为false时,这个属性才有效,0为不限制 -->
    <!-- timeToLiveSeconds:失效前的存活秒数,创建时间到失效时间的间隔为存活时间,当eternal为false时,这个属性才有效,0为不限制 -->
    <!-- maxElementsOnDisk:磁盘缓存中最多可以存放的元素数量,0表示无穷大-->
    <!--diskExpiryThreadIntervalSeconds:磁盘缓存的清理线程运行间隔,默认是120秒-->
    <!-- overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上 -->
    <!-- statistics:是否收集统计信息。如果需要监控缓存使用情况,应该打开这个选项。默认为关闭(统计会影响性能)。设置statistics="true"开启统计 -->
    <defaultCache
            maxEntriesLocalHeap="1000"
            eternal="false"
            maxElementsInMemory="10000"
            timeToIdleSeconds="3600"
            timeToLiveSeconds="3600"
            maxElementsOnDisk="10000000"
            diskExpiryThreadIntervalSeconds="120"
            overflowToDisk="true">
    </defaultCache>

    <cache name="demos"
           eternal= "false"
           maxEntriesLocalHeap="0"
           timeToIdleSeconds="200">
    </cache>


</ehcache>

改文件会显示访问不到http://ehcache.org/ehcache.xsd而报错  idea中

settings - languages&frameworks - schemas and dtds ,添加地址 http://ehcache.org/ehcache.xsd就可以了

配置完成

在启动类上开启缓存

@SpringBootApplication
@EnableCaching
public class SantintApplication {

    public static void main(String[] args) {
        SpringApplication.run(SantintApplication.class, args);
    }

}

创建实体类  由于需要实体类支持缓存中的磁盘存储,所以需要实体类实现可序列化接口

@Data
public class Demo implements Serializable {

    private static final long serialVersionUID = 1L;

    private Integer id;
    private String name;


}

业务层

@Override
    @Cacheable(value="demos")
    public Demo getEhcache(Integer id) {
        Demo demo = new Demo(1,"小白");
        return demo;
    }

@Cacheable可以标记在一个方法上,也可以标记在一个类上,当标记在一个方法上时表示该方法是支持缓存的,当标记在一个类上时则表示该类所有的方法都是支持缓存的。对于一个支持缓存的方法,Spring会在其被调用后将其返回值缓存起来,以保证下次利用同样的参数来执行该方法时可以直接从缓存中获取结果,而不需要再次执行该方法。Spring在缓存方法的返回值时是以键值对进行缓存的,值就是方法的返回结果。

@Cacheable可以指定三个属性,value、key和condition。

value属性指定cache的名称(即选择ehcache.xml中哪种缓存方式存储)

key属性是用来指定Spring缓存方法的返回结果时对应的key的。该属性支持SpringEL表达式。当我们没有指定该属性时,Spring将使用默认策略生成key。我们也直接使用“#参数名”或者“#p参数index”。下面是几个使用参数作为key的示例

@Cacheable(value="demos", key="#id")
 
public Demo find(Integer id) {
 
   return null;
 
}
 
 
 
@Cacheable(value="demos", key="#p0")
 
public Demo find(Integer id) {
 
   return null;
 
}
 
 
 
@Cacheable(value="demos", key="#user.id")
 
public Demo find(Demo demo) {
 
   return null;
 
}
 
 
 
@Cacheable(value="demos", key="#p0.id")
 
public Demo find(Demo demo) {
 
   return null;
 
}

最后,使用@CacheEvict清除缓存;

@CacheEvict(value="demos",allEntries=true)
public void saveDemos(Demos demos) {
this.demosRepository.save(demos);
}

说明:@CacheEvict是用来标注在需要清除缓存元素的方法或类上的。当标记在一个类上时表示其中所有的方法的执行都会触发缓存的清除操作。@CacheEvict可以指定的属性有value、key、condition、allEntries和beforeInvocation。

其中value、key和condition的语义与@Cacheable对应的属性类似;allEntries是boolean类型,表示是否需要清除缓存中的所有元素。默认为false,表示不需要。

当指定了allEntries为true时,Spring Cache将忽略指定的key。有的时候我们需要Cache一下清除所有的元素,这比一个一个清除元素更有效率

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值