首页 > 其他分享 >SpringBoot整合Ehcache缓存(二十二)

SpringBoot整合Ehcache缓存(二十二)

时间:2022-11-10 15:33:19浏览次数:56  
标签:Ehcache ehcache 缓存 SpringBoot id key public


二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。

上一章简单介绍了SpringBoot整合Cache缓存技术(二十一),如果没有看过,​​请观看上一章​​

一.Ehcache

关于 Ehcache的详细用法,可以参考: ​​EhCache​

是进程内的缓存框架.

二. SpringBoot 整合 Ehcache 缓存

按照上一章节 Spring Cache 整合Redis 的用法, 整合 Ehcache.

按照 SpringBoot_Cache 项目 创建 SpringBoot_Ehcache 项目.

去掉关于 Redis的的相关信息,包括 依赖, config 配置和 RedisUtil 工具类。

二.一 添加 ehcache依赖

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!--添加 ehcache依赖-->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>

注意,要去掉 spring-boot-starter-data-redis 依赖信息.

二.二 配置 ehcache.xml 文件

在 resource 目录下, 创建 ehcache.xml 缓存配置文件

<ehcache>
<diskStore path="java.io.tmpdir/spring_ehcache"/>
<defaultCache
maxElementsInMemory="10000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="120"
overflowToDisk="false"
diskPersistent="false"
diskExpiryThreadIntervalSeconds="120"
/>
<cache name="user_"
maxElementsInMemory="10000"
eternal="true"
overflowToDisk="true"
diskPersistent="true"
diskExpiryThreadIntervalSeconds="600"/>
</ehcache>
  • name​: 缓存的名称,可以通过​​指定名称​​获取指定的某个​​Cache对象
  • ​maxElementsInMemory​​​ :内存中允许存储的最大的​​元素个数​​,0代表​​无限​​个
  • ​clearOnFlush​​​:内存数量最大时​​是否清除​​。
  • ​eternal​​​ :设置缓存中对象是否为​​永久​​的,如果是,​​超时设置将被忽略​​,对象从不过期。根据存储数据的不同,例如一些静态不变的数据如省市区等可以设置为永不过时
  • ​timeToIdleSeconds​​​ : 设置对象在​​失效前​​的​​允许闲置​​时间(单位:秒)。仅当​​eternal=false​​对象​​不是​​永久有效时​​使用​​,可选属性,默认值是0,也就是​​可闲置时间无穷大​​。
  • ​timeToLiveSeconds​​​ :缓存数据的 ​​生存时间​​(TTL),也就是一个元素从构建到消亡的最大时间间隔值,这只能在元素不是永久驻留时有效,如果该值是0就意味着元素可以停顿无穷长的时间。(​​和上面的两者取最小值​​)
  • ​overflowToDisk​​​:内存不足时,是否​​启用磁盘​​缓存。
  • ​maxEntriesLocalDisk​​:当内存中对象数量达到maxElementsInMemory时,Ehcache将会对象写到磁盘中。
  • ​maxElementsOnDisk​​:硬盘最大缓存个数。
  • ​diskSpoolBufferSizeMB​​​:这个参数设置DiskStore(​​磁盘缓存​​)的缓存区​​大小​​。默认是30MB。每个Cache都应该有自己的一个缓冲区。
  • diskPersistent​​:是否在​​VM重启时存储硬盘的缓存数据​​。默认值是​​false。
  • ​diskExpiryThreadIntervalSeconds​​​:​​磁盘失效线程​​运行时间间隔,默认是120秒。
  • ​memoryStoreEvictionPolicy​​​:当达到​​maxElementsInMemory​​限制时,Ehcache将会根据​​指定的策略​​去清理内存。默认策略是​​LRU(最近最少使用)​​。你可以设置为​​FIFO(先进先出​​)或是​​LFU(较少使用)​​。这里比较遗憾,Ehcache并没有提供一个用户定制策略的接口,仅仅支持三种指定策略,感觉做的不够理想

二.三 application.yml 指定 ehcache缓存配置文件位置

server:
port: 8081
servlet:
context-path: /Ehcache
# 引入 数据库的相关配置
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/springboot?serverTimezone=GMT%2B8&useUnicode=true&characterEncoding=UTF-8&useSSL=false&allowMultiQueries=true
username: root
password: abc123
# 去掉了以前大量的Redis配置
# 配置ehcache的使用
cache:
ehcache:
config: classpath:ehcache.xml # 配置ehcache文件的路径
#整合mybatis时使用的
mybatis:
#包别名
type-aliases-package: top.yueshushu.learn.pojo
#映射文件路径
mapper-locations: classpath:mybatis/mapper/**/*.xml
configuration:
#日志信息
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl

二.四 启动类添加 @EnableCaching 缓存

@MapperScan("top.yueshushu.learn.mapper")
@SpringBootApplication
//开启缓存
@EnableCaching
public class RedisApplication {
public static void main(String[] args) {
SpringApplication.run(RedisApplication.class,args);
System.out.println("运行 Redis Cache的Ehcache缓存");
}
}

二.五 Spring Cache 注解与 Ehcache的使用

可以参考上一章节 Spring Cache 中注解的相关的用法信息.

这儿老蝴蝶不做太过的讲述.

二.五.一 @CacheConfig 类统一配置

@CacheConfig(cacheNames ={"user_"})
public class UserServiceImpl implements UserService {

}

二.五.二 @Cacheable 查询

@Override
@Cacheable(key="#id" )
public User findById(int id) {
return userMapper.findById(id);
}
@Override
@Cacheable(key = "#root.targetClass+#root.methodName")
public List<User> findAll() {
return userMapper.findAll();
}

@Cacheable(key = "#root.targetClass+#root.methodName")
@Override
public List<User> findByNameAndSex(String name, String sex) {
return userMapper.findByNameAndSex(name,sex);
}

二.五.三 @CachePut 修改

@CachePut(key = "#result.id")
@Override
public User addUser(User user) {
userMapper.addUser(user);
return user;

}
@Override
@CachePut(key = "#user.id")
public User updateUser(User user) {
userMapper.updateUser(user);
//更新全部的缓存信息
return user;
}

二.五.四 @CacheEvict 清空缓存

@Override
@CacheEvict(key = "#id")
public void deleteUser(int id) {
userMapper.deleteById(id);
}

二.五.五 @Caching 多缓存配置

@Caching(
cacheable = {
@Cacheable(key = "#name"), // 放置缓存到 name
@Cacheable(key="#sex") // 放置缓存到 sex , id的缓存用 put更新
},
put = {
@CachePut(key="#id") //同时更新 id缓存
}
)
@Override
public List<User> findByNameAndSexAndId(String name, String sex, Integer id) {
return userMapper.findByNameAndSexAndId(name,sex,id);
}

相应的测试,也是正常的。 Ehcache是放置在内存里面的,所以无法查询相应的内存信息.

本章节的代码放置在 github 上:

​https://github.com/yuejianli/springboot/tree/develop/SpringBoot_Ehcache​

谢谢您的观看,如果喜欢,请关注我,再次感谢 !!!


标签:Ehcache,ehcache,缓存,SpringBoot,id,key,public
From: https://blog.51cto.com/u_13420484/5841788

相关文章

  • SpringBoot整合Redis_Jedis版(二十)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了SpringBoot整合Redis(十九),如果没有看过,​​请观看上一章​​SpringBoot2.0版本之后,采......
  • SpringBoot整合MyBatisPlus(十四)
    二八佳人体似酥,腰间仗剑斩愚夫。虽然不见人头落,暗里教君骨髓枯。上一章简单介绍了SpringBoot整合Thymeleaf(十三),如果没有看过,​​请观看上一章​​一.MyBatisPlus的简......
  • SpringBoot系统启动任务(三十二)的方式
    当我们离开世界的时候,那些人,知道我们来过就好.上一章简单介绍了SpringBoot通过Cors解决跨域问题(三十一),如果没有看过,​​请观看上一章​​本章节参考江南一点雨大神的文......
  • 用java做一个内存缓存
    项目中对接第三方系统需要先获取认证token后,才能调用其他接口,token的有效期(固定为1小时),如果使用redis来做,十分简单,设置redis缓存加上1个小时有效期就可以解决。现在需要自......
  • Springboot项目部署到docker
    Manve项目部署到docker第一步:将springboot项目打包Maven打包SpringBoot项目报错(repackagefailed:Unabletofindmainclass),排除寻找Main方法,一般用于被依赖的公用......
  • SpringBoot启动报错The APR based Apache Tomcat Native..
    SpringBoot项目启动报错TheAPRbasedApacheTomcatNativel...一、报错信息2022-11-1009:50:53org.apache.catalina.core.AprLifecycleListenerinit信息:TheAP......
  • 浏览器的缓存机制
    浏览器的缓存机制(强缓存、协商缓存)先来粗略的概念:什么是浏览器的缓存机制浏览器的缓存机制就是把一个请求过的web资源(例如:html页面、图片、js、数据等)拷贝一份副......
  • 浅谈内存缓存和硬盘缓存
    内存缓存(frommemorycache)和硬盘缓存(fromdiskcache)内存缓存(frommemorycache):内存缓存具有两个特点,分别是快速读取和时效性:1、快速读取:内存缓存会将编译解析后......
  • 记录一次springboot 集成 openfeign 实现模块间调用异常
    记录一次springboot集成openfeign实现模块间调用异常 问题背景product 服务作为服务端,提供了一个对外通信Fegin接口ProductClient,放在了com.imooc.product.clie......
  • springboot整合项目-商城项目展示购物车勾选到支付页面并展示功能
    显示勾选的购物车数据1.持久层1.规划sql语句用户在购物车列表页面通过随机勾选相关的商品,在点击结算俺就后,跳转到结算页面,在这个页面需要展示用户在上个页面说勾选的购......