使用redis缓存短信验证码和菜品数据

master
sheng 2 years ago
parent 829a86f74a
commit 269fb0f83a

@ -4,10 +4,12 @@
<option name="autoReloadType" value="SELECTIVE" />
</component>
<component name="ChangeListManager">
<list default="true" id="09a0ab93-8583-4cca-9183-2b28acbb0d2c" name="Changes" comment="加入redis缓存验证码和菜品并且使用Spring">
<list default="true" id="09a0ab93-8583-4cca-9183-2b28acbb0d2c" name="Changes" comment="redis环境配置">
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/pom.xml" beforeDir="false" afterPath="$PROJECT_DIR$/pom.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/resources/application.yml" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/resources/application.yml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/DishController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/DishController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/UserController.java" beforeDir="false" afterPath="$PROJECT_DIR$/src/main/java/cn/edu/hactcm/cotroller/UserController.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/application.yml" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/application.yml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/UserController.class" beforeDir="false" afterPath="$PROJECT_DIR$/target/classes/cn/edu/hactcm/cotroller/UserController.class" afterDir="false" />
</list>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -61,7 +63,7 @@
&quot;RunOnceActivity.OpenProjectViewOnStart&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;WebServerToolWindowFactoryState&quot;: &quot;false&quot;,
&quot;last_opened_file_path&quot;: &quot;D:/JavaWeb/project/take_out&quot;,
&quot;last_opened_file_path&quot;: &quot;D:/JavaWeb/project/take_out/src/main/java/cn/edu/hactcm/config&quot;,
&quot;project.structure.last.edited&quot;: &quot;Project&quot;,
&quot;project.structure.proportion&quot;: &quot;0.0&quot;,
&quot;project.structure.side.proportion&quot;: &quot;0.0&quot;,
@ -69,16 +71,19 @@
&quot;spring.configuration.checksum&quot;: &quot;c52b1b9a69639134c64c20f35b444ef6&quot;
}
}</component>
<component name="ReactorSettings">
<option name="notificationShown" value="true" />
</component>
<component name="RecentsManager">
<key name="CreateClassDialog.RecentsKey">
<recent name="cn.edu.hactcm.service.impl" />
</key>
<key name="CopyFile.RECENT_KEYS">
<recent name="D:\JavaWeb\project\take_out\src\main\java\cn\edu\hactcm\config" />
<recent name="D:\JavaWeb\project\take_out\src\main\java\cn\edu\hactcm\entity" />
<recent name="D:\JavaWeb\project\take_out\src\main\java\cn\edu\hactcm\common" />
<recent name="D:\JavaWeb\project\take_out\src\main\java\cn\edu\hactcm\cotroller" />
<recent name="D:\JavaWeb\project\take_out\src\main\java\cn\edu\hactcm\service\impl" />
<recent name="D:\JavaWeb\project\take_out\src\main\java\cn\edu\hactcm\service" />
</key>
</component>
<component name="RunManager" selected="Spring Boot.TakeOutApplication">
@ -161,6 +166,7 @@
<workItem from="1681536930133" duration="12221000" />
<workItem from="1681617252929" duration="9061000" />
<workItem from="1681695089997" duration="825000" />
<workItem from="1681705529397" duration="2991000" />
</task>
<task id="LOCAL-00001" summary="加入redis缓存验证码和菜品并且使用Spring">
<created>1681702796320</created>
@ -169,7 +175,14 @@
<option name="project" value="LOCAL" />
<updated>1681702796320</updated>
</task>
<option name="localTasksCounter" value="2" />
<task id="LOCAL-00002" summary="redis环境配置">
<created>1681704109575</created>
<option name="number" value="00002" />
<option name="presentableId" value="LOCAL-00002" />
<option name="project" value="LOCAL" />
<updated>1681704109575</updated>
</task>
<option name="localTasksCounter" value="3" />
<servers />
</component>
<component name="TypeScriptGeneratedFilesManager">
@ -188,6 +201,7 @@
</component>
<component name="VcsManagerConfiguration">
<MESSAGE value="加入redis缓存验证码和菜品并且使用Spring" />
<option name="LAST_COMMIT_MESSAGE" value="加入redis缓存验证码和菜品并且使用Spring" />
<MESSAGE value="redis环境配置" />
<option name="LAST_COMMIT_MESSAGE" value="redis环境配置" />
</component>
</project>

@ -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<String> 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<String> 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<DishDto>> list(Dish dish){
//直接从缓存中取数据,能取到直接返回,取不到再查数据库
String key ="dish_"+dish.getCategoryId()+dish.getStatus();
List<DishDto> list = (List<DishDto>) redisTemplate.opsForValue().get(key);
if (list != null) {
return R.success(list);
}
//需要查数据库:
//封装条件:
LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(dish.getCategoryId() != null,Dish::getCategoryId,dish.getCategoryId())
@ -160,7 +186,7 @@ public class DishController {
//根据category_id查询该分类对应的所有菜品信息
List<Dish> dishes = dishService.list(queryWrapper);
//由于还要向移动端展示菜品的备选口味所以此处需要查询dish_flavor表将flavor一并返回给前端故此封装为DishDto对象。
List<DishDto> dishDtos = new ArrayList<DishDto>();
list = new ArrayList<DishDto>();
for (Dish dishInfo : dishes) {
// 对象属性值拷贝
DishDto dishDto = new DishDto();
@ -171,9 +197,11 @@ public class DishController {
List<DishFlavor> 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);
}
}

@ -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<User> 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<User> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(User::getPhone,phone);
User user = userService.getOne(queryWrapper);

@ -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:
#在映射实体或者属性时,将数据库中表名和字段名中的下划线去掉,按照驼峰命名法映射

Loading…
Cancel
Save