JavaWeb(苍穹外卖)--学习笔记13(微信小程序开发,缓存菜品,Spring Cache)

前言

本篇文章是学习B站黑马程序员苍穹外卖的学习笔记📑。我的学习路线是Java基础语法-JavaWeb-做项目,管理端的功能学习完之后,就进入到了用户端微信小程序的开发,小程序菜品数据是通过数据库获得,如果用户端访问量过大,数据库的压力会增加。

🔍通过Redis(之前说过他的临时存储功能)来缓存菜品数据,减少数据库查询操作。内存操作的性能比磁盘IO性能更高。

🙌通过实现小程序缓存菜品整个功能来了解Redis缓存功能

先通过流程图来了解一下整个程序执行过程:

直接看代码(缓存菜品):

java 复制代码
    /**
     * 根据分类id查询菜品
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<DishVO>> list(Long categoryId) {
        //构造redis中的key,规则:dish_分类id
        String key = "dish_" + categoryId;

        //查询redis中是否存在菜品数据
        List<DishVO> list = (List<DishVO>) redisTemplate.opsForValue().get(key);
        if (list != null && list.size() > 0) {
            //缓存存在,无需查询数据库
            return Result.success(list);
        }

        Dish dish = new Dish();
        dish.setCategoryId(categoryId);
        dish.setStatus(StatusConstant.ENABLE);//查询起售中的菜品

        //不存在,根据分类id查询数据库,将查询到的菜品数据缓存到redis中
        list = dishService.listWithFlavor(dish);
        redisTemplate.opsForValue().set(key, list);

        return Result.success(list);
    }

}

2.清除菜品缓存:

java 复制代码
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        redisTemplate.delete(key);

        return Result.success();
    }

🔍SpringCache实现缓存更方便的工具

1. 什么是SpringCache?

Spring Cache 提供了一套统一的注解和接口,使得开发者可以轻松地将缓存功能集成到应用中,而不需要关心底层缓存的具体实现(如 Redis、Ehcache 等)。

Spring Cache:提供了更高层次的抽象,通过注解(如 @Cacheable, @CachePut, @CacheEvict)简化了缓存的操作。

2. 来看看Spring Cache常用注解:


3. Spring Cache的配置使用

application.xml文件:

xml 复制代码
   redis:
    host: localhost
    port: 6379
    password: 123456
    database: 0

加入依赖:

xml 复制代码
<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>

然后就可以添加注解了:

在Controller层添加如下注解:

@Cacheable(cacheNames = "setmealCache", key = "#categoryId")

java 复制代码
    /**
     * 条件查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    @Cacheable(cacheNames = "setmealCache", key = "#categoryId")//缓存套餐数据key:setmealCache:categoryId
    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);
    }

发出请求执行方法就可以看到缓存中有数据了

小白啊!!!写的不好轻喷啊🤯如果觉得写的不好,点个赞吧🤪(批评是我写作的动力)

...。。。。。。。。。。。...

...。。。。。。。。。。。...

相关推荐
江屿风1 小时前
AI Agent架构全解析:从规划到记忆的智能跃迁
人工智能·笔记·架构·agent·c/s·mcp工作流
fly_sunnn4 小时前
机械狗的前置学习(python与Linux基础操作)
学习
吃我一窝jio5 小时前
Java Bug复盘与性能优化实战
java·开发语言·笔记
Rabitebla6 小时前
【Linux】文件篇(六):Shell 实战——从系统调用到命令行重定向
linux·服务器·c++·笔记·学习
Star(up up up)7 小时前
MySQL 事务隔离级别是如何实现的?从 MVCC 到锁机制原理详解
笔记·vscode·sql·学习·程序人生·mysql·学习方法
琳少_20 小时前
java程序开发笔记
java·开发语言·数据库·笔记
Z21-X1 天前
云原生运维学习笔记:Kubernetes Volume 与 Secret 实验
运维·笔记·学习·云原生·kubernetes
一只大鸽子1 天前
游戏开发笔记7-Godot RPG游戏基础-场景切换、全局脚本
笔记·游戏·godot
用户788477316341 天前
小程序提审被拒 7 次复盘:审核备注怎么写、隐私接口怎么声明
微信小程序