day07-缓存套餐

业务逻辑

1. 导入Spring Cache和Redis相关坐标
2. 开启缓存注解功能
3. 用户端添加注解
4. 管理端添加注解

1. 导入Spring Cache和Redis相关坐标

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-cache</artifactId>
        </dependency>
        
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-redis</artifactId>
        </dependency>

2. 开启缓存注解功能

@SpringBootApplication
@EnableTransactionManagement 
@Slf4j
@EnableCaching		//开启缓存注解功能
public class SkyApplication {
    public static void main(String[] args) {
        SpringApplication.run(SkyApplication.class, args);
        log.info("server started");
    }
}

3. 用户端添加注解
user.SetmealController.java
使用@Cacheable注解
先查询缓存中是否有数据
有数据:直接返回数据,无需查询数据库
无数据:执行SQL语句进行查询,将查询的结果缓存到redis中

	/**
     * 条件查询
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    @Cacheable(cacheNames = "setmealCache", key = "#categoryId")    //redis中的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. 管理端添加注解
新增、删除、修改、起停售中删除redis中的缓存
使用**@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);

        //将所有的菜品缓存数据清理掉,所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

相关推荐

  1. day07-缓存套餐

    2024-05-10 07:04:03       10 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-05-10 07:04:03       16 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-05-10 07:04:03       16 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-05-10 07:04:03       15 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-05-10 07:04:03       18 阅读

热门阅读

  1. C++ QT设计模式:状态模式

    2024-05-10 07:04:03       9 阅读
  2. 设计模式——解释器模式(Interpreter)

    2024-05-10 07:04:03       9 阅读
  3. 养成类游戏原理分析。

    2024-05-10 07:04:03       11 阅读
  4. UINXU

    2024-05-10 07:04:03       6 阅读
  5. 24届电信红队实习生面经

    2024-05-10 07:04:03       7 阅读
  6. 阿里云宣布:全面赶超GPT-4

    2024-05-10 07:04:03       9 阅读