Spring Cache缓存功能

[复制链接]
admin 发表于 7 天前 | 显示全部楼层 |阅读模式
依赖包:
  1. <dependency>
  2.         <groupId>org.springframework.boot</groupId>
  3.         <artifactId>spring-boot-starter-cache</artifactId>
复制代码
常用注解
在SpringCache中提供了很多缓存操作的注解,常见的是以下的几个:

注解
说明
@EnableCaching开启缓存注解功能,通常加在启动类上
@Cacheable在方法执行前先查询缓存中是否有数据,如果有数据,则直接返回缓存数据;如果没有缓存数据,调用方法并将方法返回值放到缓存中
@CachePut将方法的返回值放到缓存中
@CacheEvict将一条或多条数据从缓存中删除
在spring boot项目中,使用缓存技术只需在项目中导入相关缓存技术的依赖包,并在启动类上使用@EnableCaching开启缓存支持即可。
例如,使用Redis作为缓存技术,只需要导入Spring data Redis的maven坐标即可。

实现思路
实现步骤:
1). 导入Spring Cache和Redis相关maven坐标
2). 在启动类上加入@EnableCaching注解,开启缓存注解功能
3). 在用户端接口SetmealController的 list 方法上加入@Cacheable注解
4). 在管理端接口SetmealController的 save、delete、update、startOrStop等方法上加入CacheEvict注解

1). 导入Spring Cache和Redis相关maven坐标
  1. <dependency>
  2.       <groupId>org.springframework.boot</groupId>
  3.       <artifactId>spring-boot-starter-data-redis</artifactId>
  4. </dependency>

  5. <dependency>
  6.       <groupId>org.springframework.boot</groupId>
  7.       <artifactId>spring-boot-starter-cache</artifactId>
  8. </dependency>
复制代码
2). 在启动类上加入@EnableCaching注解,开启缓存注解功能
  1. package com.sky;

  2. import lombok.extern.slf4j.Slf4j;
  3. import org.springframework.boot.SpringApplication;
  4. import org.springframework.boot.autoconfigure.SpringBootApplication;
  5. import org.springframework.cache.annotation.EnableCaching;
  6. import org.springframework.transaction.annotation.EnableTransactionManagement;

  7. @SpringBootApplication
  8. @EnableTransactionManagement //开启注解方式的事务管理
  9. @Slf4j
  10. @EnableCaching
  11. public class SkyApplication {
  12.     public static void main(String[] args) {
  13.         SpringApplication.run(SkyApplication.class, args);
  14.         log.info("server started");
  15.     }
  16. }
复制代码
3). 在用户端接口SetmealController的 list 方法上加入@Cacheable注解
  1. /**
  2.      * 条件查询
  3.      *
  4.      * @param categoryId
  5.      * @return
  6.      */
  7.     @GetMapping("/list")
  8.     @ApiOperation("根据分类id查询套餐")
  9.     @Cacheable(cacheNames = "setmealCache",key = "#categoryId") //key: setmealCache::100
  10.     public Result<List<Setmeal>> list(Long categoryId) {
  11.         Setmeal setmeal = new Setmeal();
  12.         setmeal.setCategoryId(categoryId);
  13.         setmeal.setStatus(StatusConstant.ENABLE);

  14.         List<Setmeal> list = setmealService.list(setmeal);
  15.         return Result.success(list);
  16.     }
复制代码

Value值就是返回值
4). 在管理端接口SetmealController的 save、delete、update、startOrStop等方法上加入CacheEvict注解


  1. /**
  2.      * 新增套餐
  3.      *
  4.      * @param setmealDTO
  5.      * @return
  6.      */
  7.     @PostMapping
  8.     @ApiOperation("新增套餐")
  9.     @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
  10.     public Result save(@RequestBody SetmealDTO setmealDTO) {
  11.         setmealService.saveWithDish(setmealDTO);
  12.         return Result.success();
  13.     }
  14.         /**
  15.      * 批量删除套餐
  16.      *
  17.      * @param ids
  18.      * @return
  19.      */
  20.     @DeleteMapping
  21.     @ApiOperation("批量删除套餐")
  22.     @CacheEvict(cacheNames = "setmealCache",allEntries = true)
  23.     public Result delete(@RequestParam List<Long> ids) {
  24.         setmealService.deleteBatch(ids);
  25.         return Result.success();
  26.     }
  27.         /**
  28.      * 修改套餐
  29.      *
  30.      * @param setmealDTO
  31.      * @return
  32.      */
  33.     @PutMapping
  34.     @ApiOperation("修改套餐")
  35.     @CacheEvict(cacheNames = "setmealCache",allEntries = true)
  36.     public Result update(@RequestBody SetmealDTO setmealDTO) {
  37.         setmealService.update(setmealDTO);
  38.         return Result.success();
  39.     }

  40.     /**
  41.      * 套餐起售停售
  42.      *
  43.      * @param status
  44.      * @param id
  45.      * @return
  46.      */
  47.     @PostMapping("/status/{status}")
  48.     @ApiOperation("套餐起售停售")
  49.     @CacheEvict(cacheNames = "setmealCache",allEntries = true)
  50.     public Result startOrStop(@PathVariable Integer status, Long id) {
  51.         setmealService.startOrStop(status, id);
  52.         return Result.success();
  53.     }
复制代码
网站建设,公众号小程序开发,多商户单商户小程序制作,高端系统定制开发,App软件开发联系我们【手机/微信:17817817816
微信扫码

网站建设,公众号小程序开发,商城小程序,系统定制开发,App软件开发等

粤ICP备2024252464号

在本版发帖
微信扫码
QQ客服返回顶部
快速回复 返回顶部 返回列表