根据分类id查询套餐,使用SpringCache存入Redis缓存

This commit is contained in:
Bunny 2024-01-09 16:10:31 +08:00
parent 39640025af
commit 435736e110
3 changed files with 12 additions and 5 deletions

View File

@ -3,10 +3,12 @@ package com.sky;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.transaction.annotation.EnableTransactionManagement; import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableCaching// 开启缓存注解
@SpringBootApplication @SpringBootApplication
@EnableTransactionManagement //开启注解方式的事务管理 @EnableTransactionManagement // 开启注解方式的事务管理
@Slf4j @Slf4j
public class SkyApplication { public class SkyApplication {
public static void main(String[] args) { public static void main(String[] args) {

View File

@ -9,6 +9,7 @@ import com.sky.vo.SetmealVO;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource; import javax.annotation.Resource;
@ -30,6 +31,7 @@ public class SetmealController {
*/ */
@ApiOperation("新增套餐") @ApiOperation("新增套餐")
@PostMapping() @PostMapping()
@CacheEvict(cacheNames = "setmealCache", key = "@setmealDTO.categoryId")
public Result<String> save(@RequestBody SetmealDTO setmealDTO) { public Result<String> save(@RequestBody SetmealDTO setmealDTO) {
setmealService.saveWithDish(setmealDTO); setmealService.saveWithDish(setmealDTO);
return Result.success(); return Result.success();
@ -70,6 +72,7 @@ public class SetmealController {
*/ */
@ApiOperation("批量删除套餐") @ApiOperation("批量删除套餐")
@DeleteMapping("") @DeleteMapping("")
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
public Result delete(@RequestParam List<Long> ids) { public Result delete(@RequestParam List<Long> ids) {
setmealService.delete(ids); setmealService.delete(ids);
return Result.success(); return Result.success();
@ -96,6 +99,7 @@ public class SetmealController {
*/ */
@ApiOperation("修改套餐") @ApiOperation("修改套餐")
@PutMapping() @PutMapping()
@CacheEvict(cacheNames = "setmealCache", allEntries = true)
public Result update(@RequestBody SetmealDTO setmealDTO) { public Result update(@RequestBody SetmealDTO setmealDTO) {
setmealService.update(setmealDTO); setmealService.update(setmealDTO);
return Result.success(); return Result.success();

View File

@ -7,7 +7,7 @@ import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO; import com.sky.vo.DishItemVO;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
@ -27,10 +27,11 @@ public class SetmealController {
* 条件查询 * 条件查询
* *
* @param categoryId Long * @param categoryId Long
* @return Result<List<Setmeal>> * @return Result<List < Setmeal>>
*/ */
@GetMapping("/list") @GetMapping("/list")
@ApiOperation("根据分类id查询套餐") @ApiOperation("根据分类id查询套餐")
@Cacheable(cacheNames = "setmealCache", key = "#categoryId")// key值
public Result<List<Setmeal>> list(Long categoryId) { public Result<List<Setmeal>> list(Long categoryId) {
Setmeal setmeal = new Setmeal(); Setmeal setmeal = new Setmeal();
setmeal.setCategoryId(categoryId); setmeal.setCategoryId(categoryId);
@ -43,8 +44,8 @@ public class SetmealController {
/** /**
* 根据套餐id查询包含的菜品列表 * 根据套餐id查询包含的菜品列表
* *
* @param id * @param id Long
* @return * @return Result<List < DishItemVO>>
*/ */
@GetMapping("/dish/{id}") @GetMapping("/dish/{id}")
@ApiOperation("根据套餐id查询包含的菜品列表") @ApiOperation("根据套餐id查询包含的菜品列表")