Spring Cache缓存套餐实现思路和代码分析
实现步骤: 1). 导入Spring Cache和Redis相关maven坐标 2). 在启动类上加入@EnableCaching注解,开启缓存注解功能 3). 在用户端接口SetmealController的 list 方法上加入@Cacheable注解 4). 在管理端接口SetmealController的 save、delete、update、startOrStop等方法上加入CacheEvict注解 2.3 代码开发按照上述实现步骤: 1). 导入Spring Cache和Redis相关maven坐标(已实现)
- <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>
复制代码
2). 在启动类上加入@EnableCaching注解,开启缓存注解功能
- package com.sky;
-
- import lombok.extern.slf4j.Slf4j;
- import org.springframework.boot.SpringApplication;
- import org.springframework.boot.autoconfigure.SpringBootApplication;
- import org.springframework.cache.annotation.EnableCaching;
- import org.springframework.transaction.annotation.EnableTransactionManagement;
-
- @SpringBootApplication
- @EnableTransactionManagement //开启注解方式的事务管理
- @Slf4j
- @EnableCaching
- public class SkyApplication {
- public static void main(String[] args) {
- SpringApplication.run(SkyApplication.class, args);
- log.info("server started");
- }
- }
复制代码
3). 在用户端接口SetmealController的 list 方法上加入@Cacheable注解
- /**
- * 条件查询
- *
- * @param categoryId
- * @return
- */
- @GetMapping("/list")
- @ApiOperation("根据分类id查询套餐")
- @Cacheable(cacheNames = "setmealCache",key = "#categoryId") //key: setmealCache::100
- public Result<List<Setmeal>> list(Long categoryId) {
- Setmeal setmeal = new Setmeal();
- setmeal.setCategoryId(categoryId);
- setmeal.setStatus(StatusConstant.ENABLE);
-
- List<Setmeal> list = setmealService.list(setmeal);
- return Result.success(list);
- }
复制代码
4). 在管理端接口SetmealController的 save、delete、update、startOrStop等方法上加入CacheEvict注解
- /**
- * 新增套餐
- *
- * @param setmealDTO
- * @return
- */
- @PostMapping
- @ApiOperation("新增套餐")
- @CacheEvict(cacheNames = "setmealCache",key = "#setmealDTO.categoryId")//key: setmealCache::100
- public Result save(@RequestBody SetmealDTO setmealDTO) {
- setmealService.saveWithDish(setmealDTO);
- return Result.success();
- }
- /**
- * 批量删除套餐
- *
- * @param ids
- * @return
- */
- @DeleteMapping
- @ApiOperation("批量删除套餐")
- @CacheEvict(cacheNames = "setmealCache",allEntries = true)
- public Result delete(@RequestParam List<Long> ids) {
- setmealService.deleteBatch(ids);
- return Result.success();
- }
- /**
- * 修改套餐
- *
- * @param setmealDTO
- * @return
- */
- @PutMapping
- @ApiOperation("修改套餐")
- @CacheEvict(cacheNames = "setmealCache",allEntries = true)
- public Result update(@RequestBody SetmealDTO setmealDTO) {
- setmealService.update(setmealDTO);
- return Result.success();
- }
-
- /**
- * 套餐起售停售
- *
- * @param status
- * @param id
- * @return
- */
- @PostMapping("/status/{status}")
- @ApiOperation("套餐起售停售")
- @CacheEvict(cacheNames = "setmealCache",allEntries = true)
- public Result startOrStop(@PathVariable Integer status, Long id) {
- setmealService.startOrStop(status, id);
- return Result.success();
- }
复制代码
|