news 2026/8/24 0:43:46

苍穹外卖之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/8/21 20:16:32

阿里不推荐使用 keySet() 遍历HashMap?是有原因的

引言 HashMap相信所有学Java的都一定不会感到陌生&#xff0c;作为一个非常重用且非常实用的Java提供的容器&#xff0c;它在我们的代码里面随处可见。因此遍历操作也是我们经常会使用到的。HashMap的遍历方式现如今有非常多种&#xff1a; 使用迭代器&#xff08;Iterator)。…

作者头像 李华
网站建设 2026/8/21 20:16:34

Godot开发问题记录:无法为节点拖拽添加脚本(godot显示禁止图标)

问题点&#xff1a;Godot开发过程中&#xff0c;出现无法拖拽C#脚本到节点的问题可尝试的解决方案&#xff1a;经测试&#xff0c;修改C#脚本的编码格式为UTF-8后&#xff0c;可以正确识别加载该脚本备注&#xff1a;对于双击godot编辑界面的cs脚本无法跳转到代码编辑器&#x…

作者头像 李华
网站建设 2026/8/21 20:16:33

2026年,学R语言,为什么399元的专栏真的很值,你只需要这一份资料,其它图文资料不再需要买了!

当前医药数据科学和R语言领域&#xff0c;网络上和书籍市面上一大堆资料&#xff0c;表面看起来琳琅满目&#xff0c;价格从几十元的书籍到动辄几千元一次的线下培训班都有。但绝大多数培训或书籍都受限于时间和篇幅&#xff0c;浅尝辄止&#xff0c;很多仅仅是基础入门&#x…

作者头像 李华
网站建设 2026/8/21 19:54:42

在你的Node.js项目中轻松集成WhatsApp功能!

WhatsApp Web.js&#xff1a;轻松构建Node.js WhatsApp客户端的最佳解决方案 在现代通信中&#xff0c;WhatsApp已经是一个不可或缺的工具&#xff0c;不论是个人还是企业都在使用WhatsApp与客户沟通。今天&#xff0c;我们向大家介绍一个强大的开源库——WhatsApp Web.js&…

作者头像 李华
网站建设 2026/8/21 7:41:14

图片画质增强神器!模糊照片秒变高清

下载链接 https://pan.freedw.com/s/NN5rm8 今天发现个超好用的画质增强工具&#xff0c;不用安装解压就能用&#xff0c;再也不用为模糊照片发愁了&#xff01;不管是老照片修复还是手机拍糊了的图片&#xff0c;这个软件都能一键处理成高清画质。 软件主要分图片增强和视频…

作者头像 李华
网站建设 2026/8/22 1:43:18

使用Pandas进行数据分析:从数据清洗到可视化

SQLAlchemy是Python中最流行的ORM&#xff08;对象关系映射&#xff09;框架之一&#xff0c;它提供了高效且灵活的数据库操作方式。本文将介绍如何使用SQLAlchemy ORM进行数据库操作。 目录 安装SQLAlchemy 核心概念 连接数据库 定义数据模型 创建数据库表 基本CRUD操作…

作者头像 李华