news 2026/4/30 8:08:47

苍穹外卖之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/5/1 6:05:31

SpringBoot 这么实现动态数据源切换,就很丝滑!

最近在做业务需求时&#xff0c;需要从不同的数据库中获取数据然后写入到当前数据库中&#xff0c;因此涉及到切换数据源问题。本来想着使用Mybatis-plus中提供的动态数据源SpringBoot的starter&#xff1a;dynamic-datasource-spring-boot-starter来实现。 结果引入后发现由于…

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

2024年ESWA SCI1区TOP,异构无人机配送问题的集成多目标优化方法,深度解析+性能实测

目录1.摘要2.问题描述3.提出的算法4.结果展示5.参考文献6.代码获取7.算法辅导应用定制读者交流1.摘要 针对异构无人机末端配送路径优化问题&#xff0c;本文提出了一种基于投票机制的集成多目标遗传算法。通过改进聚类方法将客户划分为子区域&#xff0c;降低问题规模&#xf…

作者头像 李华
网站建设 2026/4/23 1:00:07

给女朋友选口红色号?这简直是完美的「分类算法」实战!

前言 在直男的色号认知里&#xff0c;口红只有红、粉、橘三种颜色&#xff0c;而你的女朋友却拥有二十支看起来完全一样的红色&#xff0c;这就是世界的参差。 “宝贝&#xff0c;这三个颜色哪个好看&#xff1f;”手机屏幕亮起&#xff0c;购物车页面上的三支口红像三道送命…

作者头像 李华
网站建设 2026/4/25 10:07:13

图标提取神器!一键提取软件安装包中的图标

下载链接 https://pan.freedw.com/s/82iLVU 今天给大家推荐一款超好用的图标提取工具Quick Any2lco&#xff0c;专门用来提取各种软件安装包里的图标文件&#xff0c;再也不用为找图标发愁了&#xff01; 使用前记得右键用管理员身份运行。 作特别简单&#xff1a;先选择要提…

作者头像 李华
网站建设 2026/5/1 7:05:40

代码质量卫士:使用Pylint和Flake8

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

作者头像 李华
网站建设 2026/5/1 7:18:08

动态库热加载技术

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

作者头像 李华