diff --git a/src/main/java/cn/czyx007/reggie/ReggieApplication.java b/src/main/java/cn/czyx007/reggie/ReggieApplication.java index 7deb671..5f315d5 100644 --- a/src/main/java/cn/czyx007/reggie/ReggieApplication.java +++ b/src/main/java/cn/czyx007/reggie/ReggieApplication.java @@ -14,6 +14,6 @@ import org.springframework.transaction.annotation.EnableTransactionManagement; @EnableTransactionManagement public class ReggieApplication { public static void main(String[] args) { - SpringApplication.run(ReggieApplication.class); + SpringApplication.run(ReggieApplication.class, args); } } diff --git a/src/main/java/cn/czyx007/reggie/controller/DishController.java b/src/main/java/cn/czyx007/reggie/controller/DishController.java index 67c500f..e70d85c 100644 --- a/src/main/java/cn/czyx007/reggie/controller/DishController.java +++ b/src/main/java/cn/czyx007/reggie/controller/DishController.java @@ -3,22 +3,24 @@ package cn.czyx007.reggie.controller; import cn.czyx007.reggie.bean.Category; import cn.czyx007.reggie.bean.Dish; import cn.czyx007.reggie.bean.DishFlavor; -import cn.czyx007.reggie.bean.Setmeal; import cn.czyx007.reggie.common.CustomException; import cn.czyx007.reggie.common.R; import cn.czyx007.reggie.dto.DishDto; import cn.czyx007.reggie.service.CategoryService; import cn.czyx007.reggie.service.DishFlavorService; import cn.czyx007.reggie.service.DishService; +import com.alibaba.fastjson.JSON; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import org.springframework.beans.BeanUtils; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.transaction.annotation.Transactional; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import java.util.List; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** @@ -34,6 +36,8 @@ public class DishController { private DishFlavorService dishFlavorService; @Autowired private CategoryService categoryService; + @Autowired + private StringRedisTemplate redisTemplate; /** * 新增菜品 @@ -43,6 +47,15 @@ public class DishController { @PostMapping public R save(@RequestBody DishDto dishDto){ dishService.saveWithFlavor(dishDto); + + //第一种处理方法:清理所有菜品的缓存 + //Set keys = redisTemplate.keys("dish_*"); + //redisTemplate.delete(keys); + + //第二种,精确清理某个分类下的菜品缓存数据 + String key = "dish_" + dishDto.getCategoryId() + "_1"; + redisTemplate.delete(key); + return R.success("新增菜品成功"); } @@ -85,6 +98,15 @@ public class DishController { @PutMapping public R update(@RequestBody DishDto dishDto){ dishService.updateWithFlavor(dishDto); + + //第一种处理方法:清理所有菜品的缓存 + //Set keys = redisTemplate.keys("dish_*"); + //redisTemplate.delete(keys); + + //第二种,精确清理某个分类下的菜品缓存数据 + String key = "dish_" + dishDto.getCategoryId() + "_1"; + redisTemplate.delete(key); + return R.success("修改菜品成功"); } @@ -130,6 +152,16 @@ public class DishController { @GetMapping("/list") public R> list(Dish dish){ + List dtoList = null; + + //先尝试从Redis中获取缓存数据 + String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus(); + dtoList = JSON.parseArray(redisTemplate.opsForValue().get(key), DishDto.class); + if (dtoList != null) { + //若存在,直接返回,无需查询数据库 + return R.success(dtoList); + } + LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); lqw.eq(dish.getCategoryId() != null, Dish::getCategoryId, dish.getCategoryId()); lqw.eq(Dish::getStatus, 1); @@ -137,9 +169,12 @@ public class DishController { lqw.orderByAsc(Dish::getSort).orderByDesc(Dish::getUpdateTime); List dishes = dishService.list(lqw); - List dtoList = dishes.stream().map((item) -> + dtoList = dishes.stream().map((item) -> dishService.getByIdWithFlavor(item.getId()) ).collect(Collectors.toList()); + JSON.toJSONString(dtoList); + //若不存在,则查询数据库,并将查询数据缓存到Redis + redisTemplate.opsForValue().set(key, JSON.toJSONString(dtoList), 60, TimeUnit.MINUTES); return R.success(dtoList); } diff --git a/src/main/java/cn/czyx007/reggie/controller/UserController.java b/src/main/java/cn/czyx007/reggie/controller/UserController.java index e825751..cb3a86b 100644 --- a/src/main/java/cn/czyx007/reggie/controller/UserController.java +++ b/src/main/java/cn/czyx007/reggie/controller/UserController.java @@ -5,6 +5,7 @@ import cn.czyx007.reggie.common.R; import cn.czyx007.reggie.service.UserService; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -13,6 +14,7 @@ import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpSession; import java.util.Map; +import java.util.concurrent.TimeUnit; /** * @author : 张宇轩 @@ -23,6 +25,8 @@ import java.util.Map; public class UserController { @Autowired private UserService userService; + @Autowired + private StringRedisTemplate redisTemplate; /** * 发送验证码 @@ -38,9 +42,9 @@ public class UserController { //String code = ValidateCodeUtils.generateValidateCode4String(4); //发送邮件验证码 //SendEmailUtils.sendAuthCodeEmail(phone, code); - //将生成的验证码保存到session用于校验 -// session.setAttribute(phone, code); - session.setAttribute(phone, "1234"); + //将生成的验证码保存到Redis用于校验,并且设置有效期为5分钟 +// redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES); + redisTemplate.opsForValue().set(phone, "1234", 5, TimeUnit.MINUTES); return R.success("验证码发送成功"); } return R.error("验证码发送失败"); @@ -52,13 +56,13 @@ public class UserController { String phone = map.get("phone"); //获取验证码 String code = map.get("code"); - //从session中获取保存的验证码 - String codeInSession = (String) session.getAttribute(phone); + //从Redis中获取保存的验证码 + String codeInRedis = redisTemplate.opsForValue().get(phone); - codeInSession = "1234"; + codeInRedis = "1234"; //将两个验证码进行比对 - if(StringUtils.hasLength(codeInSession) && codeInSession.equals(code)){ + if(StringUtils.hasLength(codeInRedis) && codeInRedis.equals(code)){ //若比对成功,则登录成功 //若数据库中无对应的手机号/邮箱则进行注册 LambdaQueryWrapper lqw = new LambdaQueryWrapper<>(); @@ -71,6 +75,9 @@ public class UserController { userService.save(user); } session.setAttribute("user", user.getId()); + //登录成功,删除Redis中缓存的验证码 + redisTemplate.delete(phone); + return R.success(user); } return R.error("登录失败"); diff --git a/src/main/resources/application.yml b/src/main/resources/application.yml index ec993d0..a133ca1 100644 --- a/src/main/resources/application.yml +++ b/src/main/resources/application.yml @@ -4,17 +4,19 @@ spring: driver-class-name: com.mysql.cj.jdbc.Driver url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai username: root +# password: # redis: # password: -# password: mybatis-plus: configuration: log-impl: org.apache.ibatis.logging.stdout.StdOutImpl +#菜品\套餐图片存储路径 #reggie: # path: +#用于发送邮箱验证码的账户和密码 #email: # userName: # password: \ No newline at end of file