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