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

Spring Boot 3.X 下Redis缓存的尝试(二):自动注解实现自动化缓存操作

前言

上文我们做了在Spring Boot下对Redis的基本操作,如果频繁对Redis进行操作而写对应的方法显示使用注释更会更高效;

比如:

依之前操作对一个业务进行定入缓存需要把数据拉取到后再定入;

而今天我们可以通过注释的方式不需要额外代码去实现对Redis的操作。

推荐在读本文之前先看一下上一篇《》

一、加入依赖

Swagger 3.0

<dependency><groupId>org.springdoc</groupId><artifactId>springdoc-openapi-starter-webmvc-ui</artifactId><version>2.3.0</version>
</dependency>
spring-boot-starter-cache  缓存
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId>
</dependency>

二、开启缓存

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

三、Swagger配置

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;@Configuration
public class SwaggerConfig {@Beanpublic GroupedOpenApi publicApi() {String[] paths = {"/**"};String[] packages = {"com.example.demo.controller"};//扫描的路径return GroupedOpenApi.builder().group("public").pathsToMatch(paths).packagesToScan(packages).build();}@Beanpublic OpenAPI openAPI() {return new OpenAPI().info(new Info().title("接口文档标题").description("SpringBoot3 集成 Swagger3接口文档").version("v1")).externalDocs(new ExternalDocumentation().description("项目API文档").url("/"));}
}

四、实体类

import lombok.Data;import java.io.Serializable;@Data
public class Stu implements  Serializable{//private static final long serialVersionUID = 1L;private Integer id;private String name;private Integer age;
}

五、服务类 注解实现

import com.example.demo.mode.Stu;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;@Service
public class RedisService {//----通过注释实现Redis的写入@Cacheable (cacheNames = "abc",unless =  "#result==null",key =  "#id")public Stu getStuById(Integer id) {Stu stu = new Stu();stu.setId(id);stu.setName("张三");stu.setAge(18);return stu;}//---- 通过注释实现Redis的更新@CachePut (cacheNames = "abc",key =  "#result.id")public Stu update(Stu stu) {stu.setName("李四");stu.setAge(19);return stu;}//---- 通过注释实现Redis的删除@CacheEvict (cacheNames = "abc",key =  "#id")public void delete(Integer id){}
}

六、控制层测试

@RestController
@Tag(name = "TestController",description = "测试接口")
@RequestMapping(value = "/swaggertest")
public class StuController {@Autowiredprivate RedisService redisService;@GetMapping("/getStuById")public Stu getStuById(Integer id){return redisService.getStuById(id);}@GetMapping("/update")public Stu update(Stu stu){return redisService.update(stu);}@DeleteMapping ("/delete")public void delete(Integer id){redisService.delete(id);}
}

七、效果展示

下篇:《Spring Boot 3.X 下Redis缓存的尝试(三):精进-用Jason进行序列化》

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

相关文章:

  • 【03】完整开发腾讯云播放器SDK的UniApp官方UTS插件——优雅草上架插件市场-卓伊凡
  • C:\Users\中文名修改为英文名
  • Web 架构相关文章目录(持续更新中)
  • Redis 安装配置和性能优化
  • 购物商城网站 Java+Vue.js+SpringBoot,包括商家管理、商品分类管理、商品管理、在线客服管理、购物订单模块
  • PostgreSQL 安全纵深防御:从权限到加密
  • 【美团技术团队】从实际案例聊聊Java应用的GC优化
  • 在word中点击zotero Add/Edit Citation没有反应的解决办法
  • 整合swagger,以及Knife4j优化界面
  • Unity | AmplifyShaderEditor插件基础(第四集:简易shader)
  • 【安全攻防与漏洞】​​量子计算对HTTPS的威胁:后量子密码学进展
  • linux C语言中的动态库 静态库说明
  • Flash烧录速度和加载配置速度(纯FPGA ZYNQ)
  • 解构与重构:PLM 系统如何从管理工具进化为创新操作系统?
  • Redis:介绍和认识,通用命令,数据类型和内部编码,单线程模型
  • N2语法 強調、限定
  • OpenAI 即将推出 GPT-5:开启多模态、持续记忆对话新时代
  • 《前端面试题:CSS预处理器(Sass、Less等)》
  • 嵌入式开发之STM32学习笔记day20
  • vue-19(Vuex异步操作和变更)
  • 人工智能-Chain of Thought Prompting(思维链提示,简称CoT)
  • [GESP202412 五级] 奇妙数字 题解
  • 《操盘实战》速读笔记
  • 元素 “cas:serviceResponse“ 的前缀 “cas“ 未绑定
  • CppCon 2014 学习:CHEAP, SIMPLE, AND SAFE LOGGING USING C++ EXPRESSION TEMPLATES
  • 专业级PDF转CAD解决方案
  • 如何屏蔽端口
  • nvidia系列教程-agx-orin安装ros
  • STM32 智能小车项目 两路红外循迹模块原理与实战应用详解
  • [论文阅读] 软件工程 | 量子计算如何赋能软件工程(Quantum-Based Software Engineering)