1-引入maven依赖;

2-增加ehcache.xml

3-bootstrap.yml配置ehcache.xml的路径

4-启动类加注解@EnableCaching

5-使用处加注解@Cacheable(value = "testCache", key = "#user.name")

  Cacheable的value选择配置,默认是默认配置;key是缓存的key值,默认是入参,也可自定义,取其中的一个属性值;

  @CacheEvict(value = "testCache", allEntries = true)

   清空缓存

1-引入maven依赖

        <!--spring boot 缓存支持启动器-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--Ehcahe-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>2.10.1</version>
</dependency>

2-增加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"> <!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir/ehcache"/> <!-- 默认缓存 -->
<defaultCache
maxEntriesLocalHeap="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
maxEntriesLocalDisk="10000000"
diskExpiryThreadIntervalSeconds="120"
memoryStoreEvictionPolicy="LRU">
<persistence strategy="localTempSwap"/>
</defaultCache> <cache name="testCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="3"
timeToLiveSeconds="3"
maxEntriesLocalDisk="10000000"
overflowToDisk="false"
memoryStoreEvictionPolicy="LRU"/>
</ehcache>

3-bootstrap.yml配置ehcache.xml的路径

#ehcache
spring.cache.ehcache.config: ehcache.xml

4-启动类加注解@EnableCaching

@SpringBootApplication
@EnableCaching
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}

5-使用处加注解@Cacheable("testCache")

    @GetMapping(value = "/testEhcache")
@ResponseBody
@Cacheable("testCache")
public int testEhcache(int num) {
logger.info("入参:" + num);
logger.info("执行:" + num);
for (int i = 0, n = num; i < n; i++) {
num += i;
}
logger.info("结果:" + num);
return num;
} @GetMapping(value = "/testEhcache2")
@ResponseBody
@Cacheable("testCache")
public int testEhcache2(int num) {
logger.info("入参:" + num);
logger.info("执行:" + num);
for (int i = 0, n = num; i < n; i++) {
num += Math.random() * 10;
}
logger.info("结果:" + num);
return num;
}

spring boot 使用Ehcache的更多相关文章

  1. (37)Spring Boot集成EHCache实现缓存机制【从零开始学Spring Boot】

    [本文章是否对你有用以及是否有好的建议,请留言] 写后感:博主写这么一系列文章也不容易啊,请评论支持下. 如果看过我之前(35)的文章这一篇的文章就会很简单,没有什么挑战性了. 那么我们先说说这一篇文 ...

  2. Spring Boot集成EHCache实现缓存机制

    SpringBoot 缓存(EhCache 2.x 篇) SpringBoot 缓存 在 Spring Boot中,通过@EnableCaching注解自动化配置合适的缓存管理器(CacheManag ...

  3. Spring Boot2 系列教程(三十)Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...

  4. Spring Boot缓存Ehcache

    Spring Boot 整合 Ehcache   修改 pom 文件 <!-- Spring Boot 缓存支持启动器 --> <dependency> <groupId ...

  5. 另一种缓存,Spring Boot 整合 Ehcache

    用惯了 Redis ,很多人已经忘记了还有另一个缓存方案 Ehcache ,是的,在 Redis 一统江湖的时代,Ehcache 渐渐有点没落了,不过,我们还是有必要了解下 Ehcache ,在有的场 ...

  6. Spring Boot整合EhCache

    本文讲解Spring Boot与EhCache的整合. 1 EhCache简介 EhCache 是一个纯Java的进程内缓存框架,具有快速.精干等特点,是Hibernate中默认CacheProvid ...

  7. spring boot集成ehcache 2.x 用于hibernate二级缓存

    https://www.jianshu.com/p/87b2c309b776 本文将介绍如何在spring boot中集成ehcache作为hibernate的二级缓存.各个框架版本如下 spring ...

  8. Spring Boot 集成 Ehcache 缓存,三步搞定!

    作者:谭朝红 www.ramostear.com/articles/spring_boot_ehcache.html 本次内容主要介绍基于Ehcache 3.0来快速实现Spring Boot应用程序 ...

  9. spring boot 整合 ehcache

    1. 该说的话 每个人都应当学会独立地去思考.去寻找答案,而不是一味地伸手向他人索取所谓的标准答案. 首先,别成为"拿来主义"者,其次远离"拿来主义"的人. 2 ...

随机推荐

  1. AtCoder Grand Contest 005F - Many Easy Problems

    $n \leq 200000$的树,从树上选$k$个点的一个方案会对$Ans_k$产生大小为“最小的包括这$k$个点的连通块大小”的贡献.求每个$Ans_k$.膜924844033. 看每个点对$An ...

  2. My97DatePicker 时间控件

    <td> <input type="text" id="sendDate" name="sendDate" class=& ...

  3. android layout

    android的视图分为两类,一类是布局,另一个类是控件 一.LinearLayout(线性布局) 最常用布局之一,线性布局的特性是每添加一个控件默认会在上个控件的下面占一行. <LinearL ...

  4. 当传入数据只有一个时mybatis中<if>判断会出现There is no getter for property named 'subjectId' in 'class java.lang.Intege

    用"_parameter"代替当前参数 正确: <select id="selectSubjectByPId" parameterType="j ...

  5. yum gd linux

    32位操作系统安装命令:[root@localhost ~]# yum install php-gd*64位操作系统安装命令:[root@localhost ~]# yum install php-g ...

  6. PHP拓展开发

    痛定思痛: 开始了解 PHP 拓展开发,下面这篇文章不错!照着文章讲的,终于实现了! m.php的代码 浏览器访问 m.php 文件!(备注:在linux 命令行中 php -r 'cthulhu() ...

  7. hdu 2795(单点改动)

    题意:有h×w大的公告板.有n条公告要写入,每条公告高度都是1,宽度是wi,每次从最上最左的空位写,假设有空位输出第几行.假设没有足够空位输出-1. 题解:注意h最大1e9.但事实上是看n的大小.由于 ...

  8. Eclipse打包Android项目时用到proguard.cfg后,出现的Warning:can&#39;t find referenced class问题的解决方式

    Warning: can't find superclass or interface Warning: can't find referenced class 这两个问题的解决方法: 1.要把你项目 ...

  9. 【手记】走近科学之为什么JObject不能调用LINQ扩展方法

    Json.NET的JObject明明实现了IEnumerable<T>,具体来说是IEnumerable<KeyValuePair<string, JToken>> ...

  10. C指针——C语言手记

    近期敲代码的时候.发现自己非常多东西都開始忘了. 今天最终有机会好好总结一下指针.当做个笔记同一时候也希望对大家实用.假设有不对的地方.希望大家能帮我指正一下.然后我的实验环境是32位RHEL+ecl ...