news 2026/4/24 15:48:50

苍穹外卖之SpringCache在项目中的应用场景

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
苍穹外卖之SpringCache在项目中的应用场景

SpringCache

参考视频或文章

  • https://juejin.cn/post/7507548342508371983

一、技术介绍

1.概述

  • SpringCache是Spring框架提供的一套声明式缓存抽象层(只提供接口,不提供实现),通过在方法上添加注解简化缓存操作,无需手动编写缓存逻辑。
  • SpringCache支持多种缓存实现,如Caffeine、Redis、EhCache等等,并统一了缓存访问的API。

2.核心特点

  • 基于注解的声明式缓存
  • 支持SpEL(Spring Expression Language)表达式
  • 自动与Spring生态集成
  • 支持条件缓存

3.常用缓存注解

缓存注解功能
@EnableCaching开启注解方式的缓存功能,通常加在项目启动类上。
@Cacheable标记方法,将方法的返回值缓存,下次调用直接从缓存中读取,无需重新执行方法。
@CachePut标记方法,将方法的返回值缓存。
@CacheEvict标记方法,用于清除缓存,通常配合数据删除操作使用。
@Caching组合多个缓存注解,支持在同一个方法上同时配置多种缓存行为。
@CacheConfig标记类,为类中所有方法指定统一的缓存配置,减少重复配置。

二、项目应用

涉及到的文件如下:

sky-take-out:pom.xmlsky-server:pom.xmlsrc/main/java/com.sky:SkyApplicationcontroller:admin:SetmealControlleruser:SetmealController

1.导入和Redis和SpringCache的Maven依赖坐标

1.1sky-take-out: pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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><artifactId>spring-boot-starter-parent</artifactId><groupId>org.springframework.boot</groupId><version>2.7.3</version></parent><groupId>com.sky</groupId><artifactId>sky-take-out</artifactId><packaging>pom</packaging><version>1.0-SNAPSHOT</version><modules><module>sky-common</module><module>sky-pojo</module><module>sky-server</module></modules><properties><druid>1.2.1</druid></properties><dependencyManagement><dependencies><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId><version>${druid}</version></dependency></dependencies></dependencyManagement></project>
1.2sky-server: pom.xml
<?xml version="1.0" encoding="UTF-8"?><projectxmlns="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"><parent><artifactId>sky-take-out</artifactId><groupId>com.sky</groupId><version>1.0-SNAPSHOT</version></parent><modelVersion>4.0.0</modelVersion><artifactId>sky-server</artifactId><dependencies><dependency><groupId>com.sky</groupId><artifactId>sky-common</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>com.sky</groupId><artifactId>sky-pojo</artifactId><version>1.0-SNAPSHOT</version></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId><scope>compile</scope></dependency><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><dependency><groupId>com.alibaba</groupId><artifactId>druid-spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-redis</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-cache</artifactId></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

2.在项目启动类SkyApplication上开启缓存注解功能

@SpringBootApplication@EnableTransactionManagement// 开启注解方式的事务管理@EnableCaching// 开启注解方式的缓存功能publicclassSkyApplication{publicstaticvoidmain(String[]args){SpringApplication.run(SkyApplication.class,args);}}

3.编写user/SetmealController,使用@Cachable更新缓存

/** * 用户端-套餐接口 */@RestController("userSetmealController")@RequestMapping("/user/setmeal")publicclassSetmealController{@AutowiredprivateSetmealServicesetmealService;@AutowiredprivateDishServicedishService;// 根据分类id查询已启用的套餐@GetMapping("/list")@Cacheable(cacheNames="setmeal",key="#categoryId")// key名称:setmeal::{categoryId}publicResult<List<Setmeal>>getByCategoryId(LongcategoryId){List<Setmeal>setmeals=setmealService.getByCategoryId(categoryId);returnResult.success(setmeals);}}

4.编写admin/SetmealController,使用@CacheEvict清除缓存

/** * 套餐管理模块 */@RestController("adminSetmealController")@RequestMapping("/admin/setmeal")publicclassSetmealController{@AutowiredprivateSetmealServicesetmealService;// 新增套餐和对应的菜品@PostMapping@CacheEvict(cacheNames="setmeal",key="#setmealDTO.categoryId")// 清理缓存publicResultsaveWithDish(@RequestBodySetmealDTOsetmealDTO){setmealService.saveWithDish(setmealDTO);returnResult.success();}// 批量删除套餐@DeleteMapping@CacheEvict(cacheNames="setmeal",allEntries=true)// 清理全部缓存publicResultdeleteBatch(@RequestParamList<Long>ids){setmealService.deleteBatch(ids);returnResult.success();}// 修改套餐@PutMapping@CacheEvict(cacheNames="setmeal",allEntries=true)// 清理全部缓存publicResultupdate(@RequestBodySetmealDTOsetmealDTO){setmealService.updateWithDishes(setmealDTO);returnResult.success();}// 起售停售套餐@PostMapping("/status/{status}")@CacheEvict(cacheNames="setmeal",allEntries=true)// 清理全部缓存publicResultchangeStatus(@PathVariableIntegerstatus,Longid){setmealService.changeStatus(status,id);returnResult.success();}}
版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/18 13:34:27

C++与Java性能对比

1、非修改序列算法这些算法不会改变它们所操作的容器中的元素。1.1 find 和 find_iffind(begin, end, value)&#xff1a;查找第一个等于 value 的元素&#xff0c;返回迭代器&#xff08;未找到返回 end&#xff09;。find_if(begin, end, predicate)&#xff1a;查找第一个满…

作者头像 李华
网站建设 2026/4/23 21:19:12

嵌入式LinuxC++开发

1、非修改序列算法 这些算法不会改变它们所操作的容器中的元素。 1.1 find 和 find_if find(begin, end, value)&#xff1a;查找第一个等于 value 的元素&#xff0c;返回迭代器&#xff08;未找到返回 end&#xff09;。find_if(begin, end, predicate)&#xff1a;查找第…

作者头像 李华
网站建设 2026/4/23 12:45:42

C++ 预处理指令:#include、#define 与条件编译

C 预处理指令&#xff1a;#include、#define 与条件编译 在C程序的编译过程中&#xff0c;有一个容易被忽略但至关重要的环节——预处理阶段。它发生在编译器对源代码进行正式编译之前&#xff0c;由预处理程序&#xff08;预处理器&#xff09;对源代码中的“预处理指令”进行…

作者头像 李华
网站建设 2026/4/18 14:45:10

《P4587 [FJOI2016] 神秘数》

题目描述一个可重复数字集合 S 的神秘数定义为最小的不能被 S 的子集的和表示的正整数。例如 S{1,1,1,4,13}&#xff0c;有&#xff1a;11&#xff0c;211&#xff0c;3111&#xff0c;44&#xff0c;541&#xff0c;6411&#xff0c;74111。8 无法表示为集合 S 的子集的和&…

作者头像 李华
网站建设 2026/4/24 8:58:24

系统软件领域中的BSS段

系统软件领域中的BSS段 文章目录系统软件领域中的BSS段一、基本概念二、主要特点三、设计原理与优势四、内存布局示例五、实际操作与验证六、与数据段的区别七、实际应用场景八、注意事项九、相关技术命令一、基本概念 BSS&#xff08;Block Started by Symbol&#xff09; 是…

作者头像 李华