苍穹外卖day07——缓存菜品套餐+购物车功能实现

这篇具有很好参考价值的文章主要介绍了苍穹外卖day07——缓存菜品套餐+购物车功能实现。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

缓存菜品——需求设计与分析

问题说明

用户访问量过大带来的一个直接效果就是响应速度慢,使用体验下降。

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 实现思路

使用redis缓存菜品数据,减少数据库查询操作。

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 页面展示上基本就是同一个分类在同一页,所以key-value结构可以使用不同的分类来做key。

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

缓存菜品——代码开发

在小程序每一次点击不同的分类,后端哪里都会刷刷刷的连接数据库查询返回,对后端压力肥肠的大,因此使用Redis的作用在这里就能体现了。

修改用户端的DishController代码

@RestController("userDishController")
@RequestMapping("/user/dish")
@Slf4j
@Api(tags = "C端-菜品浏览接口")
public class DishController {
    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 根据分类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);//查询起售中的菜品

        //如果不存在,查询数据库,将查询到的数据放入redis中
        list = dishService.listWithFlavor(dish);
        redisTemplate.opsForValue().set(key,list);
        return Result.success(list);
    }
}

相对应的在redis里面会有缓存数据

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

修改管理端的DIshController代码 

要保证数据一致性,数据库数据发生改变时要及时修改redis,不然小程序端的数据和数据库数据会不一致。

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

/**
 * 菜品管理
 */
@RestController
@RequestMapping("/admin/dish")
@Api(tags="菜品相关接口")
@Slf4j
public class DishController {

    @Autowired
    private DishService dishService;

    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增菜品
     * @param dishDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO){
        log.info("新增菜品:{}",dishDTO);
        dishService.saveWithFlavor(dishDTO);

        //清理缓存数据
        String key="dish_"+dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * @param dishPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO){
        log.info("菜品分页查询:{}",dishPageQueryDTO);
        PageResult pageResult= dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 菜品批量删除
     * 通过@RequestParam注解将字符串转换为数组
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids){
        log.info("菜品批量删除:{}",ids);
        dishService.deleteBatch(ids);

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

        return Result.success();
    }
    /**
     * 根据id查询菜品
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id){
        log.info("根据id查询菜品:{}",id);
        DishVO dishVO=dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * 根据id修改菜品基本信息和对应口味信息
     * @param dishDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO){
        log.info("修改菜品:{}",dishDTO);
        dishService.updateWithFlavor(dishDTO);

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

        return Result.success();
    }



    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list=dishService.list(categoryId);
        return Result.success(list);
    }
    /**
     * 菜品起售停售
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售停售")
    public Result<String> startOrStop(@PathVariable Integer status, Long id){
        dishService.startOrStop(status,id);
        //将所有的菜品缓存数据清理掉,所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * 清理缓存数据
     * @param pattern
     */
    private void cleanCache(String pattern){
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}

缓存菜品——功能测试

不做了,好累啊。

缓存套餐——Spring Cache

在以下这个文章里面。Spring Cache_北岭山脚鼠鼠的博客-CSDN博客

实现思路+代码开发

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 在启动类上加入注解

@EnableCaching //开启缓存注解的功能

 user端

    /**
     * 条件查询
     *
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询套餐")
    @Cacheable(cacheNames = "sermealCache",key="#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);
    }

admin端

/**
 * 套餐管理
 */
@RestController
@RequestMapping("/admin/setmeal")
@Api(tags="套餐相关接口")
@Slf4j
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    /**
     * 新增套餐
     * @param setmealDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增套餐")
    @CacheEvict(cacheNames = "setmealCachce",key="#setmealDTO.categoryId")
    public Result save(@RequestBody SetmealDTO setmealDTO){
        setmealService.saveWithDish(setmealDTO);
        return Result.success();
    }

    /**
     * 分页查询
     * @param setmealPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("分页查询")
    public Result<PageResult> page(SetmealPageQueryDTO setmealPageQueryDTO)
    {
        PageResult pageResult=setmealService.pageQuery(setmealPageQueryDTO);
        return Result.success(pageResult);
    }
    /**
     * 批量删除套餐
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除套餐")
    @CacheEvict(cacheNames = "setmealCache",allEntries = true)
    public Result delete(@RequestParam List<Long> ids){
        setmealService.deleteBatch(ids);
        return Result.success();
    }

    /**
     * 根据id查询套餐,用于修改页面回显数据
     *
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询套餐")
    public Result<SetmealVO> getById(@PathVariable Long id) {
        SetmealVO setmealVO = setmealService.getByIdWithDish(id);
        return Result.success(setmealVO);
    }

    /**
     * 修改套餐
     *
     * @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();
    }
}

缓存套餐——功能测试

测试无误

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

虽然但是,明明小程序没有显示东西,但是数据还是进了缓存。

 添加购物车——需求分析与设计

产品原型

购物车就是暂时存放所选商品的地方。

没有口味就是直接加入购物车,有口味要先选择口味。

接口设计

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

数据库设计

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 冗余字段的存在减少了查询次数。

 添加购物车——代码开发(1)

用到的DTO

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 Controller层中

@RestController
@RequestMapping("/user/shoppingCart")
@Slf4j
@Api(tags = "C端购物车相关接口")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 添加购物车
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/add")
    @ApiOperation("添加购物车")
    public Result add(@RequestBody ShoppingCartDTO shoppingCartDTO){
        log.info("添加购物车:{}",shoppingCartDTO);
        shoppingCartService.addShoppingCart(shoppingCartDTO);
        return Result.success();
    }
}

Service层中

@Service
@Slf4j
public class ShoppingCartServiceImpl implements ShoppingCartService {


    @Autowired
    private ShoppingCartMapper shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 添加购物车
     * @param shoppingCartDTO
     */
    @Override
    public void addShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        //判断当前加入购物车的商品是否存在
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart); //属性拷贝
        Long userId = BaseContext.getCurrentId();  //拦截器获取到的用户id
        shoppingCart.setUserId(userId);
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        //如果已经存在了,只需要将数量加一
        if(list!=null&&list.size()>0){
            //这里list要么没有数据,要么只有一条数据
            ShoppingCart cart = list.get(0);
            cart.setNumber(cart.getNumber()+1); //update shopping_cart set number=?where id=?
            shoppingCartMapper.updateNumberById(cart);
        }else {
            //如果不存在,需要插入一条购物车数据
            /**
             * 判断这次添加到购物车的是菜品还是套餐
             */
            Long dishId = shoppingCartDTO.getDishId();
            if(dishId!=null){
                //本次添加是菜品
                Dish dish = dishMapper.getById(dishId);
                shoppingCart.setName(dish.getName());
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setAmount(dish.getPrice());
            }else{
                //本次添加的是套餐
                Long setmealId = shoppingCartDTO.getSetmealId();
                Setmeal setmeal = setmealMapper.getById(setmealId);
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setAmount(setmeal.getPrice());
            }
            shoppingCart.setNumber(1);
            shoppingCart.setCreateTime(LocalDateTime.now());
            //统一插入数据
            shoppingCartMapper.insert(shoppingCart);
        }
        
    }
}

Mapper层中

@Mapper
public interface ShoppingCartMapper {

    /**
     * 动态条件查询
     * @param shoppingCart
     * @return
     */
    List<ShoppingCart> list(ShoppingCart shoppingCart);


    /**
     * 根据id修改商品数量
     * @param shoppingCart
     */
    @Update("update shopping_cart set number = #{number} where id = #{id}")
    void updateNumberById(ShoppingCart shoppingCart);

    /**
     * 插入购物车数据
     * @param shoppingCart
     */
    @Insert("insert into shopping_cart(name , user_id, dish_id, setmeal_id, dish_flavor, number, amount,image, create_time)" +
            "values(#{name},#{userId},#{dishId},#{setmealId},#{dishFlavor},#{number},#{amount},#{image},#{createTime})")
    void insert(ShoppingCart shoppingCart);
}

对应的映射文件

<mapper namespace="com.sky.mapper.ShoppingCartMapper">

    <select id="list" resultType="com.sky.entity.ShoppingCart">
        select * from shopping_cart
            <where>
                <if test="userId != null">
                    and user_id = #{userId}
                </if>
                <if test="setmealId != null">
                    and setmeal_id = #{setmealId}
                </if>
                <if test="dishId != null">
                    and dish_id = #{dishId}
                </if>
                <if test="dishFlavor != null">
                    and dish_flavor = #{dishFlavor}
                </if>
            </where>
    </select>
</mapper>

 添加购物车——功能测试

前端点击添加成功有新数据

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

查看购物车——需求分析和设计+代码开发+功能测试

产品原型

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 接口设计

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

Controller中

    /**
     * 查看购物车
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查看购物车")
    public Result<List<ShoppingCart>> list(){
        List<ShoppingCart>list=shoppingCartService.showShoppingCart();
        return Result.success(list);
    }

Service中

    /**
     * 查看购物车
     * @return
     */
    @Override
    public List<ShoppingCart> showShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        ShoppingCart shoppingCart = ShoppingCart.builder()
                .userId(userId)
                .build();
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }

mapper层已经有了

功能测试

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

清空购物车——需求分析和设计+代码开发+功能测试

产品原型

店家清空直接删除所有数据

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 接口设计

Controller中

    /**
     * 清空购物车
     * @return
     */
    @DeleteMapping("/clean")
    @ApiOperation("清空购物车")
    public Result clean(){
        shoppingCartService.cleanShoppingCart();
        return Result.success();
    }

Service中

    /**
     * 清空购物车
     */
    @Override
    public void cleanShoppingCart() {
        //获取当前微信用户的id
        Long userId = BaseContext.getCurrentId();
        shoppingCartMapper.deleteById(userId);
    }

mapper中

    /**
     * 根据用户id删除购物车数据
     * @param userId
     */
    @Delete("delete from shopping_cart where user_id = #{userId}")
    void deleteById(Long userId);

功能测试不写了,反正没人看

删除购物车——需求分析与设计

产品原型

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信点击减号可以减少一个或者是直接删除

 接口设计

苍穹外卖day07——缓存菜品套餐+购物车功能实现,SpringBoot,缓存,redis,微信

 Controller层中

    /**
     * 删除购物车商品
     * @param shoppingCartDTO
     * @return
     */
    @PostMapping("/sub")
    @ApiOperation("/删除购物车商品")
    public Result sub(ShoppingCartDTO shoppingCartDTO){
        log.info("删除商品信息:{}",shoppingCartDTO);
        shoppingCartService.subShoppingCart(shoppingCartDTO);
        return Result.success();
    }

Service层中

    /**
     * 删除购物车数据
     * @param shoppingCartDTO
     */
    @Override
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        ShoppingCart shoppingCart=new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        //获取用户id,查询当前登录用户的id
        shoppingCart.setId(BaseContext.getCurrentId());

        List<ShoppingCart> list=shoppingCartMapper.list(shoppingCart);
        if(list!=null&&list.size()>0){
            shoppingCart=list.get(0);
            Integer number = shoppingCart.getNumber();
            if(number==1){
                //当前商品在购物车中的份数为1,直接删除当前记录
                shoppingCartMapper.deleteById2(shoppingCart.getId());
            }else{
                //当前商品在购物车中的份数不为1,修改份数即可
                shoppingCart.setNumber(shoppingCart.getNumber() - 1);
                shoppingCartMapper.updateNumberById(shoppingCart);
            }
        }
    }

Mapper层中

这个删除是根据id删除,上面的是根据userId删除,两者不同的。文章来源地址https://www.toymoban.com/news/detail-619661.html

    /**
     * 根据id删除购物车数据
     * @param id
     */
    @Delete("delete from shopping_cart where id = #{id}")
    void deleteById2(Long id);

到了这里,关于苍穹外卖day07——缓存菜品套餐+购物车功能实现的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • 苍穹外卖day11笔记

    今日首先介绍前端技术Apache ECharts,说明后端需要准备的数据,然后讲解具体统计功能的实现,包括营业额统计、用户统计、订单统计、销量排名。 ECharts是一款基于 Javascript 的数据可视化图表库。我们用它来展示图表数据。 步骤 1). 引入echarts.js 文件 2). 为 ECharts 准备一个设

    2024年02月13日
    浏览(26)
  • Vue项目商品购物车前端本地缓存逻辑(适用H5/ipad/PC端)——前端实现购物车删除商品、购物车增减数量,清空购物车功能

    Vue3 + Vite + Ts开源后台管理系统模板 基于ElementUi或AntdUI再次封装基础组件文档 基于Element-plus再次封装基础组件文档(vue3+ts)

    2024年02月12日
    浏览(36)
  • 苍穹外卖day02项目日志

    参考产品原型,设计表和接口。 1.1.1设计表 看员工管理的产品原型: 有员工姓名、账号、手机号、账号状态、最后操作时间等。 注意,操作一栏不是字段,其中的启用禁用才是。 再看添加员工的原型:  可以发现还有性别和身份证号。 不要忘了旁边: 还有密码。 总结出了

    2024年02月14日
    浏览(55)
  • 黑马苍穹外卖学习Day12

    结果 Controller层 Service实现类

    2024年01月25日
    浏览(40)
  • 项目实战————苍穹外卖(DAY11)

    Apache ECharts 营业额统计 用户统计 订单统计 销量排名Top10 功能实现: 数据统计 数据统计效果图: 1.1 介绍 Apache ECharts 是一款基于 Javascript 的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。 官网地址:Apache ECharts 常见效果展示: 1). 柱形图

    2024年01月20日
    浏览(35)
  • 苍穹外卖 day1 搭建成功环境

    idea找不到打包生成的文件目录怎么办,首先点击这个小齿轮 然后就能找到隐藏的文件 这个jar包内含tomcat,可以直接丢在linux上用 开发环境:开发人员在开发阶段使用的环境,一般外部用户无法访问 测试环境:专门给测试人员使用的环境,测试项目用 生产环境:正式对外提

    2024年02月11日
    浏览(47)
  • 苍穹外卖-day14:前端环境搭建、员工管理

    前端环境搭建 员工分页查询 启用禁用员工账号 新增员工 修改员工 1.1 技术选型 本项目使用到的前端技术如下: node.js vue ElementUI axios vuex vue-router typescript 1.2 熟悉前端代码结构 直接导入课程资料中提供的苍穹外卖项目前端初始工程,此工程中已经开发了部分功能,后续我们

    2024年04月28日
    浏览(26)
  • 【学习笔记】java项目—苍穹外卖day11

    Apache ECharts 营业额统计 用户统计 订单统计 销量排名Top10 功能实现: 数据统计 数据统计效果图: 1.1 介绍 Apache ECharts 是一款基于 Javascript 的数据可视化图表库,提供直观,生动,可交互,可个性化定制的数据可视化图表。 官网地址:https://echarts.apache.org/zh/index.html 常见效果

    2024年04月09日
    浏览(33)
  • 苍穹外卖 day2 反向代理和负载均衡

    前端请求地址:http://localhost/api/employee/login 路径并不匹配 后端接口地址:http://localhost:8080/admin/employee/login 在这个页面上点击f12 后转到networ验证,出现一些详细的地址信息 原因就是nginx的反向代理,将前端数据转发到了后端 相当于转发一次请求 优点: 提高访问速度,相当于

    2024年02月12日
    浏览(35)
  • Java项目-苍穹外卖-Day10-SpirngTask及WebSocket

    本章实现的业务功能 超时未支付订单自动取消,配送中订单商家忘点完成自动再固定时间检查且修改成完成状态 来单提醒功能 催单提醒功能 一般的话周几和第几日是不能同时出现的 因为比如 4月15日 周四 可能4月15日不是周四 可能冲突的 所以周和日一般只能有一个 现在有

    2024年02月09日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包