diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index 04f9be5..27fd2b3 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -4,10 +4,12 @@
-
+
-
-
+
+
+
+
@@ -61,7 +63,7 @@
"RunOnceActivity.OpenProjectViewOnStart": "true",
"RunOnceActivity.ShowReadmeOnStart": "true",
"WebServerToolWindowFactoryState": "false",
- "last_opened_file_path": "D:/JavaWeb/project/take_out",
+ "last_opened_file_path": "D:/JavaWeb/project/take_out/src/main/java/cn/edu/hactcm/config",
"project.structure.last.edited": "Project",
"project.structure.proportion": "0.0",
"project.structure.side.proportion": "0.0",
@@ -69,16 +71,19 @@
"spring.configuration.checksum": "c52b1b9a69639134c64c20f35b444ef6"
}
}
+
+
+
+
-
@@ -161,6 +166,7 @@
+
1681702796320
@@ -169,7 +175,14 @@
1681702796320
-
+
+ 1681704109575
+
+
+
+ 1681704109575
+
+
@@ -188,6 +201,7 @@
-
+
+
\ No newline at end of file
diff --git a/src/main/java/cn/edu/hactcm/cotroller/DishController.java b/src/main/java/cn/edu/hactcm/cotroller/DishController.java
index bd7ad4a..68f2389 100644
--- a/src/main/java/cn/edu/hactcm/cotroller/DishController.java
+++ b/src/main/java/cn/edu/hactcm/cotroller/DishController.java
@@ -13,11 +13,13 @@ import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
+import java.util.concurrent.TimeUnit;
@RestController
@Slf4j
@@ -32,6 +34,9 @@ public class DishController {
@Autowired
private DishFlavorService dishFlavorService;
+
+ @Autowired
+ private RedisTemplate redisTemplate;
/**
* 添加菜品,涉及对两张表进行操作,引入新的数据模型Dto同时包含两张表的属性值,可以完美接受浏览器端传输的数据。
* @param dishDto
@@ -39,6 +44,11 @@ public class DishController {
*/
@PostMapping
public R save(@RequestBody DishDto dishDto){
+ String key = "dish_"+dishDto.getCategoryId()+dishDto.getStatus();
+ redisTemplate.delete(key);
+ //全部删除
+ /*Set keys = redisTemplate.keys("dish_*");
+ redisTemplate.delete(keys);*/
dishService.saveWithFlavor(dishDto);
return R.success("添加菜品成功!");
}
@@ -112,6 +122,14 @@ public class DishController {
@Transactional
@PutMapping
public R update(@RequestBody DishDto dishDto){
+ //由于执行了更新操作,缓存中的数据与数据库中的数据已经不再一直,产生脏读数据。所以要对缓存进行删除
+ //按照分类删除
+ String key = "dish_"+dishDto.getCategoryId()+dishDto.getStatus();
+ redisTemplate.delete(key);
+ //全部删除
+ /*Set keys = redisTemplate.keys("dish_*");
+ redisTemplate.delete(keys);*/
+
//对dish表进行更新
dishService.updateById(dishDto);
@@ -152,6 +170,14 @@ public class DishController {
*/
@GetMapping("/list")
public R> list(Dish dish){
+ //直接从缓存中取数据,能取到直接返回,取不到再查数据库
+ String key ="dish_"+dish.getCategoryId()+dish.getStatus();
+ List list = (List) redisTemplate.opsForValue().get(key);
+ if (list != null) {
+ return R.success(list);
+ }
+
+ //需要查数据库:
//封装条件:
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId())
@@ -160,7 +186,7 @@ public class DishController {
//根据category_id查询该分类对应的所有菜品信息
List dishes = dishService.list(queryWrapper);
//由于还要向移动端展示菜品的备选口味,所以此处需要查询dish_flavor表,将flavor一并返回给前端,故此封装为DishDto对象。
- List dishDtos = new ArrayList();
+ list = new ArrayList();
for (Dish dishInfo : dishes) {
// 对象属性值拷贝
DishDto dishDto = new DishDto();
@@ -171,9 +197,11 @@ public class DishController {
List dishFlavors = dishFlavorService.list(dishFlavorLambdaQueryWrapper);
// 为DishDto对象的flavors属性赋值
dishDto.setFlavors(dishFlavors);
- dishDtos.add(dishDto);
+ list.add(dishDto);
}
- return R.success(dishDtos);
+ //将查询到的数据放入缓存
+ redisTemplate.opsForValue().set(key,list,60, TimeUnit.MINUTES);
+ return R.success(list);
}
}
diff --git a/src/main/java/cn/edu/hactcm/cotroller/UserController.java b/src/main/java/cn/edu/hactcm/cotroller/UserController.java
index 7f944d8..fdef736 100644
--- a/src/main/java/cn/edu/hactcm/cotroller/UserController.java
+++ b/src/main/java/cn/edu/hactcm/cotroller/UserController.java
@@ -7,6 +7,7 @@ import cn.edu.hactcm.utils.ValidateCodeUtils;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
@@ -14,6 +15,7 @@ import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpSession;
import java.util.Map;
+import java.util.concurrent.TimeUnit;
@RestController
@RequestMapping("/user")
@@ -22,6 +24,8 @@ public class UserController {
@Autowired
private UserService userService;
+ @Autowired
+ private RedisTemplate redisTemplate;
/**
* 发送手机短信验证码
@@ -36,7 +40,8 @@ public class UserController {
log.info(code);
//模拟发送短信
//SMSUtils.sendMessage("汉马外卖","","1111111111",code);
- session.setAttribute(user.getPhone(),code);
+// session.setAttribute(user.getPhone(),code);
+ redisTemplate.opsForValue().set(user.getPhone(),code,5, TimeUnit.MINUTES);
return R.success("发送成功");
}
return R.error("发送失败");
@@ -47,7 +52,7 @@ public class UserController {
public R login(@RequestBody Map map, HttpSession session){
String code = (String) map.get("code");
String phone = (String) map.get("phone");
- if (code != null && code.equals(session.getAttribute(phone))) {
+ if (code != null && code.equals(redisTemplate.opsForValue().get(phone))) {
LambdaQueryWrapper queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getPhone,phone);
User user = userService.getOne(queryWrapper);
diff --git a/target/classes/application.yml b/target/classes/application.yml
index 69ac670..0aa8245 100644
--- a/target/classes/application.yml
+++ b/target/classes/application.yml
@@ -10,6 +10,10 @@ spring:
url: jdbc:mysql://localhost:3306/reggie?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: yjx062711.
+ redis:
+ host: localhost
+ port: 6379
+ database: 0
mybatis-plus:
configuration:
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射
diff --git a/target/classes/cn/edu/hactcm/cotroller/UserController.class b/target/classes/cn/edu/hactcm/cotroller/UserController.class
index 5177e04..d5b64d7 100644
Binary files a/target/classes/cn/edu/hactcm/cotroller/UserController.class and b/target/classes/cn/edu/hactcm/cotroller/UserController.class differ