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

📅 发布时间:2026/7/7 3:31:10 👁️ 浏览次数:
苍穹外卖之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:SetmealController1.导入和Redis和SpringCache的Maven依赖坐标1.1sky-take-out: pom.xml?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdmodelVersion4.0.0/modelVersionparentartifactIdspring-boot-starter-parent/artifactIdgroupIdorg.springframework.boot/groupIdversion2.7.3/version/parentgroupIdcom.sky/groupIdartifactIdsky-take-out/artifactIdpackagingpom/packagingversion1.0-SNAPSHOT/versionmodulesmodulesky-common/modulemodulesky-pojo/modulemodulesky-server/module/modulespropertiesdruid1.2.1/druid/propertiesdependencyManagementdependenciesdependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactIdversion${druid}/version/dependency/dependencies/dependencyManagement/project1.2sky-server: pom.xml?xml version1.0 encodingUTF-8?projectxmlnshttp://maven.apache.org/POM/4.0.0xmlns:xsihttp://www.w3.org/2001/XMLSchema-instancexsi:schemaLocationhttp://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsdparentartifactIdsky-take-out/artifactIdgroupIdcom.sky/groupIdversion1.0-SNAPSHOT/version/parentmodelVersion4.0.0/modelVersionartifactIdsky-server/artifactIddependenciesdependencygroupIdcom.sky/groupIdartifactIdsky-common/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdcom.sky/groupIdartifactIdsky-pojo/artifactIdversion1.0-SNAPSHOT/version/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-test/artifactIdscopetest/scope/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-web/artifactIdscopecompile/scope/dependencydependencygroupIdmysql/groupIdartifactIdmysql-connector-java/artifactIdscoperuntime/scope/dependencydependencygroupIdcom.alibaba/groupIdartifactIddruid-spring-boot-starter/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-data-redis/artifactId/dependencydependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-cache/artifactId/dependency/dependenciesbuildpluginsplugingroupIdorg.springframework.boot/groupIdartifactIdspring-boot-maven-plugin/artifactId/plugin/plugins/build/project2.在项目启动类SkyApplication上开启缓存注解功能SpringBootApplicationEnableTransactionManagement// 开启注解方式的事务管理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(cacheNamessetmeal,key#categoryId)// key名称setmeal::{categoryId}publicResultListSetmealgetByCategoryId(LongcategoryId){ListSetmealsetmealssetmealService.getByCategoryId(categoryId);returnResult.success(setmeals);}}4.编写admin/SetmealController使用CacheEvict清除缓存/** * 套餐管理模块 */RestController(adminSetmealController)RequestMapping(/admin/setmeal)publicclassSetmealController{AutowiredprivateSetmealServicesetmealService;// 新增套餐和对应的菜品PostMappingCacheEvict(cacheNamessetmeal,key#setmealDTO.categoryId)// 清理缓存publicResultsaveWithDish(RequestBodySetmealDTOsetmealDTO){setmealService.saveWithDish(setmealDTO);returnResult.success();}// 批量删除套餐DeleteMappingCacheEvict(cacheNamessetmeal,allEntriestrue)// 清理全部缓存publicResultdeleteBatch(RequestParamListLongids){setmealService.deleteBatch(ids);returnResult.success();}// 修改套餐PutMappingCacheEvict(cacheNamessetmeal,allEntriestrue)// 清理全部缓存publicResultupdate(RequestBodySetmealDTOsetmealDTO){setmealService.updateWithDishes(setmealDTO);returnResult.success();}// 起售停售套餐PostMapping(/status/{status})CacheEvict(cacheNamessetmeal,allEntriestrue)// 清理全部缓存publicResultchangeStatus(PathVariableIntegerstatus,Longid){setmealService.changeStatus(status,id);returnResult.success();}}