当前位置: 首页 > news >正文

Spring Cache简单介绍和使用

目录

一、简介

二、使用默认ConcurrentMapManager 

(一)创建数据库和表

(二)创建boot项目

(三)使用Api

1、@EnableCaching

2、@CachePut

3、@cacheable

4、@CacheEvict

三、使用redis作为cache


一、简介

Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单地加一个注解,就能实现缓存功能。

Spring Cache提供了一层抽象,底层可以切换不同的cache实现。具体就是通过CacheManager接口来统一不同的缓存技术。
CacheManager是Spring提供的各种缓存技术抽象接口
针对不同的缓存技术需要实现不同的CacheManager:

CacheManager默认使用的ConcurrentMapManager 

Spring Cache 常用注解

 在spring boot项目中,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在启动类上使用@EnableCaching开启缓存支持即可。
例如,使用Redis作为缓存技术,只需要导入Spring data Redis的maven坐标即可。

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

 spring cache的基本api在web下的context包中

 如果有使用其他的api可以导入cache的依赖

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

二、使用默认ConcurrentMapManager 

(一)创建数据库和表

创建cache_demo数据库,并创建user表

> create database cache_demo;
Query OK, 1 row affected (0.02 sec)> use cache_demo;
Database changed> create table user (
> id bigint primary key,
> name varchar(50),
> age int,
> address varchar(50)
>);

 

 (二)创建boot项目

改POM

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.4.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.itheima</groupId><artifactId>cache_demo</artifactId><version>1.0-SNAPSHOT</version><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>compile</scope></dependency><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><version>1.18.20</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.76</version></dependency><dependency><groupId>commons-lang</groupId><artifactId>commons-lang</artifactId><version>2.6</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.4.2</version></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>1.1.23</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><version>2.4.5</version></plugin></plugins></build>
</project>

 写YML

server:port: 8080
spring:application:#应用的名称,可选name: cache_demodatasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/cache_demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: root
mybatis-plus:configuration:#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: ASSIGN_ID

User

@Data
public class User implements Serializable {private static final long serialVersionUID = 1L;private Long id;private String name;private int age;private String address;}UserMapper
====================================================
@Mapper
public interface UserMapper extends BaseMapper<User>{
}UserController
====================================================
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {@Autowiredprivate UserService userService;// 增加User@PostMappingpublic User save(User user){userService.save(user);return user;}// 删除User@DeleteMapping("/{id}")public void delete(@PathVariable Long id){userService.removeById(id);}// 更新User@PutMappingpublic User update(User user){userService.updateById(user);return user;}// 根据id查询User@GetMapping("/{id}")public User getById(@PathVariable Long id){User user = userService.getById(id);return user;}// 根据id和name查询User集合@GetMapping("/list")public List<User> list(User user){LambdaQueryWrapper<User> queryWrapper = new LambdaQueryWrapper<>();queryWrapper.eq(user.getId() != null,User::getId,user.getId());queryWrapper.eq(user.getName() != null,User::getName,user.getName());List<User> list = userService.list(queryWrapper);return list;}
}

 主启动类

@Slf4j
@SpringBootApplication
public class CacheDemoApplication {public static void main(String[] args) {SpringApplication.run(CacheDemoApplication.class,args);log.info("项目启动成功...");}
}

 (三)使用Api

1、@EnableCaching

启动类上加注解@EnableCaching  // 使用spring cache

2、@CachePut

    // 在controller中加入缓存对象@Autowiredprivate CacheManager cacheManager;/*** CachePut:将方法返回值放入缓存* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key*/@CachePut(value = "userCache",key = "#user.id")@PostMappingpublic User save(User user){userService.save(user);return user;}

通过ctrl点进key下载源码后我们可以看到这里使用的是SpEL语言动态获取值

 

测试,使用 postman 发请求 

 

 

 第一次添加会将返回的user存放进cacheManager中

 

3、@cacheable

在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中
    /*** Cacheable:在方法执行前spring先查看缓存中是否有数据,如果有数据,则直接返回缓存数据;若没有数据,调用方法并将方法返回值放到缓存中* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key* condition:条件,满足条件时才缓存数据* unless:满足条件则不缓存*/@Cacheable(value = "userCache",key = "#id",unless = "#result == null")@GetMapping("/{id}")public User getById(@PathVariable Long id){User user = userService.getById(id);return user;}

测试,我们使用jack的id去查询一下,同样使用postman,在这个方法设置断点,如果没有触发断点说明是在cache中查询直接返回的

 4、@CacheEvict

    /*** CacheEvict:清理指定缓存* value:缓存的名称,每个缓存名称下面可以有多个key* key:缓存的key*/@CacheEvict(value = "userCache",key = "#p0")//@CacheEvict(value = "userCache",key = "#root.args[0]")//@CacheEvict(value = "userCache",key = "#id")@DeleteMapping("/{id}")public void delete(@PathVariable Long id){userService.removeById(id);}//@CacheEvict(value = "userCache",key = "#p0.id")//@CacheEvict(value = "userCache",key = "#user.id")//@CacheEvict(value = "userCache",key = "#root.args[0].id")@CacheEvict(value = "userCache",key = "#result.id")@PutMappingpublic User update(User user){userService.updateById(user);return user;}

三、使用redis作为cache

导入redis依赖

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

修改YML

server:port: 8080
spring:application:#应用的名称,可选name: cache_demodatasource:druid:driver-class-name: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/cache_demo?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=trueusername: rootpassword: 123456redis:host: 192.168.23.100port: 6379password: zjy123...000database: 1cache:redis:time-to-live: 1800000 #设置缓存过期时间,可选
mybatis-plus:configuration:#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射map-underscore-to-camel-case: truelog-impl: org.apache.ibatis.logging.stdout.StdOutImplglobal-config:db-config:id-type: ASSIGN_ID

启动项目后,cache变成了RedisCacheManager

 

使用postman发送save请求

db01

 发送DELETE删除缓存

 

 

 

http://www.lryc.cn/news/32636.html

相关文章:

  • ECCV 2022|面向精确的主动相机定位算法
  • web实现环形旋转、圆形、弧形、querySelectorAll、querySelector、clientWidth、sin、cos、PI
  • PyCharm+Python+Selenium自动化测试动态验证码识别
  • git版本回退简单记录
  • QT入门Display Widgets之QLine、QLcdNumber、QTextBrowser
  • Spring学习笔记
  • 数据的标准化处理
  • 性能优化|记一次线上OOM问题处理
  • Vue动态粒子特效插件(背景线条吸附动画)
  • 【Java 类】002-类、属性、方法、代码块
  • Ubuntu Linux 编译安装的基本步骤
  • day59反刍笔记
  • 【阅读笔记】你不知道的Javascript--强制类型转换4
  • 华为OD机试真题Python实现【有效子字符串】真题+解题思路+代码(20222023)
  • 上门按摩预约APP源码-东郊到家源码(开发,PHP,平台搭建)
  • STL讲解——模拟实现vector
  • 各种经典排序算法介绍及实现源码
  • 历史大讲堂:这是真·图形化 苹果系统历史回顾(上)
  • 今天女神节,用python画个贺卡送给母亲吧
  • 【编程基础之Python】11、Python中的表达式
  • 华为OD机试真题Python实现【乱序整数序列两数之和绝对值最小】真题+解题思路+代码(20222023)
  • 字符串转换整数 (atoi)(python)
  • 洛谷 P1115 最大子段和
  • 【Linux】-- 权限和Shell运行原理
  • C++各类设计模式及实现详解
  • 【Linux】进程理解与学习(Ⅰ)
  • 认识代码之前,请先认识你自己 |《编程人生》
  • react学习笔记-5:react路由
  • [Python图像处理] 使用高通滤波器实现同态滤波
  • PyTorch深度学习:60分钟入门