SpringBoot集成cache缓存的实现
作者:huanzi-qch 发布时间:2023-11-27 16:37:39
前言
日常开发中,缓存是解决数据库压力的一种方案,通常用于频繁查询的数据,例如新闻中的热点新闻,本文记录springboot中使用cache缓存。
官方文档介绍:https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/reference/htmlsingle/#boot-features-caching-provider-generic
工程结构
代码编写
pom引入依赖,引入cache缓存,数据库使用mysql,ORM框架用jpa
<!--添加springdata-cache依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<!-- 引入ehcache支持 -->
<dependency>
<groupId>net.sf.ehcache</groupId>
<artifactId>ehcache</artifactId>
</dependency>
<!--添加springdata-jpa依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<!--添加MySQL驱动依赖 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
配置文件
server.port=10010
spring.application.name=springboot-cache
spring.cache.type=ehcache
spring.cache.ehcache.config=classpath:/ehcache.xml
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">
<!-- 磁盘缓存位置 -->
<diskStore path="java.io.tmpdir"/>
<!-- maxEntriesLocalHeap:堆内存中最大缓存对象数,0没有限制 -->
<!-- maxElementsInMemory: 在内存中缓存的element的最大数目。-->
<!-- eternal:elements是否永久有效,如果为true,timeouts将被忽略,element将永不过期 -->
<!-- timeToIdleSeconds:发呆秒数,发呆期间未访问缓存立即过期,当eternal为false时,这个属性才有效,0为不限制 -->
<!-- timeToLiveSeconds:总存活秒数,当eternal为false时,这个属性才有效,0为不限制 -->
<!-- overflowToDisk: 如果内存中数据超过内存限制,是否要缓存到磁盘上 -->
<!-- statistics:是否收集统计信息。如果需要监控缓存使用情况,应该打开这个选项。默认为关闭(统计会影响性能)。设置statistics="true"开启统计 -->
<!--
默认缓存
无过期时间,但 600 秒内无人访问缓存立即过期
-->
<defaultCache
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="600"
timeToLiveSeconds="0"
overflowToDisk="false">
</defaultCache>
<!--
xx业务缓存
在有效的 120 秒内,如果连续 60 秒未访问缓存,则缓存失效。
就算有访问,也只会存活 120 秒。
-->
<cache name="myCache"
maxElementsInMemory="1000"
eternal="false"
timeToIdleSeconds="120"
timeToLiveSeconds="0"
overflowToDisk="false">
</cache>
</ehcache>
先写一个套tb_user表的CRUD代码
@RestController
@RequestMapping("/tbUser/")
public class TbUserController {
@Autowired
private TbUserService tbUserService;
//方便测试暂时改成GetMapping
@GetMapping("list")
// @PostMapping("list")
public List<TbUser> list(TbUser entityVo) {
return tbUserService.list(entityVo);
}
@GetMapping("get/{id}")
public TbUser get(@PathVariable("id")Integer id) {
return tbUserService.get(id);
}
//方便测试暂时改成GetMapping
@GetMapping("save")
// @PostMapping("save")
public TbUser save(TbUser entityVo) {
return tbUserService.save(entityVo);
}
@GetMapping("delete/{id}")
public Integer delete( @PathVariable("id") Integer id) {
return tbUserService.delete(id);
}
}
opjo实体类要实现序列化
@Entity
@Table(name = "tb_user")
@Data
public class TbUser implements Serializable {
@Id
@GeneratedValue(strategy= GenerationType.IDENTITY)
private Integer id;//表id
private String username;//用户名
private String password;//密码
private Date created;//创建时间
private Integer descriptionId;//关联详情id
}
serviceImpl中,使用注解来开启缓存
@Service
@Transactional
@CacheConfig(cacheNames = {"myCache"})
public class TbUserServiceImpl implements TbUserService{
@PersistenceContext
private EntityManager em;
@Autowired
private TbUserRepository tbUserRepository;
//@Cacheable缓存数据:key为userList,value为返回值List<TbUser>
@Cacheable(key = "'userList'")
@Override
public List<TbUser> list(TbUser entityVo) {
System.out.println("获取list用户列表缓存数据,"+new Date());
return tbUserRepository.findAll(Example.of(entityVo));
}
//@Cacheable缓存数据:key为参数id,value为返回值TbUser
@Cacheable(key = "#id")
@Override
public TbUser get(Integer id) {
System.out.println("获取数据缓存,key:"+id);
Optional<TbUser> optionalE = tbUserRepository.findById(id);
if (!optionalE.isPresent()) {
throw new RuntimeException("ID不存在!");
}
return optionalE.get();
}
//@CachePut缓存新增的或更新的数据到缓存,其中缓存的名称为people,数据的key是person的id
@CachePut(key = "#entityVo.id")
// @CacheEvict从缓存中删除key为参数userList的数据
@CacheEvict(key = "'userList'")
@Override
public TbUser save(TbUser entityVo) {
System.out.println("新增/更新缓存,key:"+entityVo.getId());
//entityVo传啥存啥,会全部更新
return tbUserRepository.save(entityVo);
}
//清空所有缓存
@CacheEvict(allEntries=true)
@Override
public Integer delete(Integer id) {
System.out.println("清空所有缓存");
tbUserRepository.deleteById(id);
return id;
}
}
效果演示
http://localhost:10010/tbUser/save?id=2&username=李四
调用save方法,key为2,value为当前tbUser对象的数据被缓存下来
http://localhost:10010/tbUser/get/2
当我们调用get方法时,直接获取缓存数据,控制台啥也不打印,连serviceImpl的get方法都不进去(可以打断点调试)
http://localhost:10010/tbUser/save?id=2&username=王五
当我们再次调用save方法更新username时,缓存数据也被更新
http://localhost:10010/tbUser/get/2
再次调用get接口,直接返回缓存数据,后台也是方法都不进去,啥也不打印
http://localhost:10010/tbUser/delete/2
调用delete接口,删除数据,同时删除缓存
再次调用get接口,发现缓存数据被清除,查询数据库
http://localhost:10010/tbUser/list
首次调用list接口,key为userList的,value为用户集合数据被缓存下来,再次调用直接返回缓存数据
当调用save接口,数据更新,删除key为userList的缓存,再次调用list时,重新查库并设置缓存
我们配置了缓存发呆时间,当120秒内未使用该缓存,立即过期,一直用就会一直存在
我们先同时访问两个接口list、get,list接口2分钟后再次访问,get接口不能超过2分钟是不是访问一下,结果如预期
PS:原先使用了这个jar包,有报错
<dependency>
<groupId>org.ehcache</groupId>
<artifactId>ehcache</artifactId>
<version>3.8.1</version>
</dependency>
后面改成用上面“代码编写”里pom中引的jnet.sf.ehcache下面的ar
后记
缓存除了能缓解数据库压力,还能做用户登录状态控制,例如:用户登录成功后cookie中保存颁发的token令牌设置永不过期,缓存存活时间也设置永不过期,发呆时间设置1天,这样只有用户在1天内有访问缓存接口,那他就可以一直保留登录状态,直至有其他业务将token或者缓存清掉。
springboot使用cache缓存暂时先记录到这,后续有空再进行补充。
代码开源
代码已经开源、托管到我的GitHub、码云:
GitHub:https://github.com/huanzi-qch/springBoot
码云:https://gitee.com/huanzi-qch/springBoot
来源:https://www.cnblogs.com/huanzi-qch/p/14871341.html
猜你喜欢
- 本文实例为大家分享了android实现简易计算器展示的具体代码,供大家参考,具体内容如下效果图:一、如图,首先布局计算器主页显示activi
- 1. Spring ProfileSpring可使用Profile绝对程序在不同环境下执行情况,包含配置、加载Bean、依赖等。 Sprin
- 本文实例为大家分享了java实现仿射密码加密解密的具体代码,供大家参考,具体内容如下加密:将明文转化为对应的数字,如 ‘a'->
- 前言:mybatisplus 可以说是对mybatis更好的拓展,一些简单的增删改查的操作已经被作者实现,我们只需引用即可。1.数据库建表这
- 提示:IntelliJ IDEA以下简称IDEA;####IntelliJ IDEA 配置git:需要的材料:一、git.exe二、配置gi
- 1、conditional注解介绍含义: 基于条件的注解作用: 根据是否满足某一个特定条件来决定是否创建某个特定的bean意义: Sprin
- 为什么要重复造轮子你可能会问,Spring已经自带了全局异常拦截,为什么还要重复造轮子呢?这是个好问题,我觉得有以下几个原因装逼Spring
- MongoDBMongoDB作为一种NoSQL数据库产品,其实已经非常著名了。去年,由于MongoDB安全认证的薄弱,上万家公司中招。虽然是
- 在java项目开发过程中,使用properties文件作为配置基本上是必不可少的,很多如系统配置信息,文件上传配置信息等等都是以这种方式进行
- 前言本文章主要从spring security安全认证登录内部调用流程来流程分析登录过程。一、登录时序图时序原图二、配置与代码1.引入库po
- 近日工程中,逐渐感觉到原来复制粘贴代码的笨重,突然想起以前有人和我说起过Git和SVN之类的版本管理工具。由于平时主要是写Java代码,所以
- 本文实例讲述了Java二维数组。分享给大家供大家参考,具体如下:一 点睛1 每行的元素个数不同的二维数组Java定义二维数组更加灵活,允许二
- Java实现按行读取大文件String file = "F:" + File.separator + "a.t
- IDEA安装后找不到.vmoptions文件在安装IDEA后在C盘的C:\Users\你的电脑用户名.IntelliJIdea2019.1\
- 前言SpringBoot是我们经常使用的框架,那么你能不能针对SpringBoot实现自动配置做一个详细的介绍。如果可以的话,能不能画一下实
- 需求描述:企业开发过程中,经常需要将一些静态文本数据放到Resources目录下,项目启动时或者程序运行
- 1、来源random.nextInt() 为 java.util.Random类中的方法; Math.random() 为 java.lan
- 前言java 10 引进一种新的闪闪发光的特性叫做局部变量类型推断。听起来很高大上吧?它是什么呢? 下面的两个情景是我们作为 Java 开发
- 经过一番搜索发现,java操纵excel文件常用的有jxl和poi两种方式,孰好孰坏看自己需求而定。其中最主要的区别在于jxl不支持.xls
- 本文实例讲述了java设计模式之工厂模式。分享给大家供大家参考,具体如下:工厂模式(factory)涉及到4个角色:抽象工厂类角色,具体工厂