目录
对数据进行缓存,降低数据库被查询的压力
在高并发的情况下,频繁查询数据库会导致系统性能下降,服务端响应时间增长,所以需要对方法进行优化,提高系统的性能。
下面关于git分支类的大家可以直接省略
码云:Gitee - 基于 Git 的代码托管和研发协作平台
- .git
- logs
- rebel.xml
- target/
- !.mvn/wrapper/maven-wrapper.jar
- log.path_IS_UNDEFINED
- .DS_Store
- offline_user.md
-
- ### STS ###
- .apt_generated
- .classpath
- .factorypath
- .project
- .settings
- .springBeans
-
- ### IntelliJ IDEA ###
- .idea
- *.iws
- *.iml
- *.ipr
-
- ### NetBeans ###
- nbproject/private/
- build/
- nbbuild/
- dist/
- nbdist/
- .nb-gradle/
- generatorConfig.xml
-
- ### nacos ###
- third-party/nacos/derby.log
- third-party/nacos/data/
- third-party/nacos/work/
-
- file/
因为项目git初始化之后源码文件会都变成红色,所以在根目录右键点击添加(如下)
之后进行正常的commit和push就OK了,详解见提交到远程仓库_雾喔的博客-CSDN博客_提交到远程仓库
刷新码云仓库界面,就可以看到项目成功提交上去了。
导入依赖:
- <dependency>
- <groupId>org.springframework.boot</groupId>
- <artifactId>spring-boot-starter-data-redis</artifactId>
- <version>2.7.5</version>
- </dependency>
大家可以去maven仓库找最新版本
- spring:
- redis:
- host: ip地址(本地的话就是localhost)
- port: 6379(端口号)
- password: redis密码
- database: redis仓库
- import org.springframework.cache.annotation.CachingConfigurerSupport;
- import org.springframework.context.annotation.Bean;
- import org.springframework.context.annotation.Configuration;
- import org.springframework.data.redis.connection.RedisConnectionFactory;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.data.redis.serializer.StringRedisSerializer;
-
- /**
- * @author a1002
- */
- @Configuration
- public class RedisConfig extends CachingConfigurerSupport {
- @Bean
- public RedisTemplate
- RedisTemplate
- //默认的Key序列化器为:JdkSerializationRedisSerializer
- redisTemplate.setKeySerializer(new StringRedisSerializer());
- redisTemplate.setConnectionFactory(connectionFactory);
- return redisTemplate;
- }
- }
- @Autowired
- private RedisTemplate redisTemplate;
发送验证码的接口:
- //将生成的验证码缓存到redis,并且设置有效期为五分钟
- redisTemplate.opsForValue().set(phone, code, 5, TimeUnit.MINUTES);
验证码有效期为五分钟,所以为
TimeUnit.MINUTES
在登录接口加上:
- //从redis获取缓存的验证码
- Object codeInSession = redisTemplate.opsForValue().get(phone);
如果登录成功:
- //如果用户登录成功,删除redis中缓存的验证码
- redisTemplate.delete(phone);
对数据进行缓存,降低数据库被查询的压力,有数据被执行save或update方法时要清理缓存。
在使用缓存过程中,要注意保证数据库中的数据和缓存中的数据一致,如果数据库中的数据发生变化,需要及时清理缓存。
在查询数据库中数据的接口加上(Get):
- List
dishDtoList = null; -
- String key = "dish_" + dish.getCategoryId() + "_" + dish.getStatus();
-
- //先从redis中获取数据
- dishDtoList = (List
) redisTemplate.opsForValue().get(key); -
- if (dishDtoList != null) {
- //如果存在,无需再查询数据库
- return R.success(dishDtoList);
- }
下面的步骤是正常的从数据库查询数据
在方法最后面加上:
- //如果不存在,需要查询数据库,将查询到的数据缓存到Redis中
- redisTemplate.opsForValue().set(key, dishDtoList, 60, TimeUnit.MINUTES);
设置的有效时间为一个小时。
在使用缓存过程中,要注意保证数据库中的数据和缓存中的数据一致,如果数据库中的数据发生变化,需要及时清理缓存数据。
方法一:
清除所有的同类缓存
- //清理所有的菜品的缓存数据
- Set keys=redisTemplate.keys("dish_*");
- redisTemplate.delete(keys);
方法二:
精确清理某个分类下的菜品缓存
- String key="dish_"+dishDto.getCategoryId()+"_1";
- redisTemplate.delete(key);
(
关于缓存的进一步了解的话大家可以去了解一下Spring Cache
Spring Cache是一个框架,实现了基于注解的缓存功能,只需要简单的加一个注解,就能实现缓存功能。
spring cache提供了一层抽象,底层可以切换不同的cache实现,具体就是通过CacheManager接口来统一不同的缓存技术。
)
合并分支之前需要保证分支没有问题
git checkout master
git pull origin master
git merge v1.0
git status
时把v1.0分支选上
---------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------我是可爱的分割线--------------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
按照指示操作获取授权码
-
-
org.springframework.boot -
spring-boot-starter-mail -
2.7.4 -
这个是大致目录:
-
- import com.sun.mail.util.MailSSLSocketFactory;
-
- import javax.mail.*;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Properties;
-
-
- public class MailUtils {
- //发送第二封验证码邮件
- public static void sendMail(String to, String vcode) throws Exception {
-
- //设置发送邮件的主机 smtp.qq.com
- String host = "smtp.qq.com";
-
- //1.创建连接对象,连接到邮箱服务器
- Properties props = System.getProperties();
-
- //Properties 用来设置服务器地址,主机名 。。 可以省略
-
- //设置邮件服务器
- props.setProperty("mail.smtp.host", host);
- props.put("mail.smtp.auth", "true");
-
- //SSL加密
- MailSSLSocketFactory sf = new MailSSLSocketFactory();
-
- sf.setTrustAllHosts(true);
-
- props.put("mail.smtp.ssl.enable", "true");
-
- props.put("mail.smtp.ssl.socketFactory", sf);
-
- //props:用来设置服务器地址,主机名;Authenticator:认证信息
-
- Session session = Session.getDefaultInstance(props, new Authenticator() {
-
- @Override
-
- //通过密码认证信息
-
- protected PasswordAuthentication getPasswordAuthentication() {
-
- //new PasswordAuthentication(用户名, password);
-
- //这个用户名密码就可以登录到邮箱服务器了,用它给别人发送邮件
-
- return new PasswordAuthentication("2728XXXXX8@qq.com", "thklbwsdgtkpdghb");
-
- }
-
- });
-
- try {
-
- Message message = new MimeMessage(session);
-
- //2.1设置发件人:
-
- message.setFrom(new InternetAddress("27287XXXX38@qq.com"));
-
- //2.2设置收件人 这个TO就是收件人
-
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
-
- //2.3邮件的主题
-
- message.setSubject("来自黑客星球网站验证码邮件");
-
- //2.4设置邮件的正文 第一个参数是邮件的正文内容 第二个参数是:是文本还是html的连接
-
- message.setContent("
来自黑客星球的网站验证码邮件,请接收你的成神验证码:
你的验证码是:"
+ vcode + ",请妥善保管好你的验证码!", "text/html;charset=UTF-8"); -
- //3.发送一封激活邮件
-
- Transport.send(message);
-
-
- } catch (MessagingException mex) {
-
- mex.printStackTrace();
-
- }
-
- }
- }
- import java.util.Random;
-
- /**
- * 随机生成验证码工具类
- *
- * @author a1002
- */
- public class ValidateCodeUtils {
- /**
- * 随机生成验证码
- *
- * @param length 长度为4位或者6位
- * @return
- */
- public static Integer generateValidateCode(int length) {
- Integer code = null;
- if (length == 4) {
- code = new Random().nextInt(9999);//生成随机数,最大为9999
- if (code < 1000) {
- code = code + 1000;//保证随机数为4位数字
- }
- } else if (length == 6) {
- code = new Random().nextInt(999999);//生成随机数,最大为999999
- if (code < 100000) {
- code = code + 100000;//保证随机数为6位数字
- }
- } else {
- throw new RuntimeException("只能生成4位或6位数字验证码");
- }
- return code;
- }
-
- /**
- * 随机生成指定长度字符串验证码
- *
- * @param length 长度
- * @return
- */
- public static String generateValidateCode4String(int length) {
- Random rdm = new Random();
- String hash1 = Integer.toHexString(rdm.nextInt());
- String capstr = hash1.substring(0, length);
- return capstr;
- }
- }
- import com.mailredistest.utils.MailUtils;
- import com.mailredistest.utils.ValidateCodeUtils;
- import lombok.extern.slf4j.Slf4j;
- import org.apache.maven.surefire.shade.org.apache.commons.lang3.StringUtils;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.data.redis.core.RedisTemplate;
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.PostMapping;
- import org.springframework.web.bind.annotation.RequestMapping;
- import org.springframework.web.servlet.ModelAndView;
-
- import java.util.concurrent.TimeUnit;
-
- /**
- * @author a1002
- */
- @Controller
- @RequestMapping("/user")
- @Slf4j
- public class IndexController {
-
- @Autowired
- private RedisTemplate redisTemplate;
-
- /**
- * 用户登录
- */
- @PostMapping("/login")
- public ModelAndView login(String mail, String code) {
- ModelAndView mv = new ModelAndView();
- // log.info(code.toString());
-
- //从Session中获取保存的验证码
- // Object codeInSession = session.getAttribute(phone);
-
- //从redis获取缓存的验证码
- Object codeInSession = redisTemplate.opsForValue().get(mail);
-
- //进行验证码的比对(页面提交的验证码和Session中保存的验证码比对)
- if (codeInSession != null && codeInSession.equals(code)) {
- //如果能够比对成功,说明登录成功
-
- //如果用户登录成功,删除redis中缓存的验证码
- redisTemplate.delete(mail);
-
- log.info("==================登录成功!====================");
-
- mv.setViewName("index");
- return mv;
- }
- mv.setViewName("login");
- return mv;
- }
-
-
- /**
- * 发送手机短信验证码
- */
- @PostMapping("/sendMsg")
- public ModelAndView sendMsg(String mail) throws Exception {
- log.info("============{}========", mail);
-
- ModelAndView mv = new ModelAndView();
-
- if (StringUtils.isNotEmpty(mail)) {
- //生成随机的4位验证码
- String code = ValidateCodeUtils.generateValidateCode(4).toString();
- log.info("code={}", code);
-
- MailUtils.sendMail(mail, code);
-
- //将生成的验证码缓存到redis,并且设置有效期为30秒
- redisTemplate.opsForValue().set(mail, code, 30, TimeUnit.SECONDS);
- }
-
- mv.setViewName("login");
- return mv;
- }
-
- }
- import org.springframework.stereotype.Controller;
- import org.springframework.web.bind.annotation.RequestMapping;
-
- /**
- * @author a1002
- */
- @Controller
- public class View {
-
- @RequestMapping("/index")
- public String q2() {
- return "/index";
- }
-
- @RequestMapping("/login")
- public String q1() {
- return "/login";
- }
-
- }
- html>
- <html xmlns:th="http://www.thymeleaf.org">
- <head>
- <meta charset="UTF-8">
- <title>logintitle>
- head>
- <body>
- <form th:action="@{/user/sendMsg}" method="post">
- <input class="up" type="text" name="mail" placeholder="邮箱" required>
- <input type="submit" class="login" name="发送验证码">
- form>
-
- <form th:action="@{/user/login}" method="post">
- <input class="up" type="text" name="mail" placeholder="邮箱" required>
- <input class="down" type="text" name="code" placeholder="验证码" required>
- <input type="submit" class="login" name="登录">
- form>
- body>
- html>
---------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------我是可爱的分割线鸭-----------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
很开心小组新增了很多大一的孩子。
最近也发生一个比较有意思的事情,当然最后这些都会成为回忆。
最近也挺烦恼的,拿不了快递。
最近惰性有点强。
最近有了一个新的体悟,假如在某件事或某个人上投入太多的精力,事后后劲虽然会有一段时间挺大,但之后的很长一段时间后劲就会变得异常绵长和断断续续,就是那种以为自己忘了,实则不然。
最后祝我越来越好🎉
---------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------
-------------------------------------------------我是可爱的分割线鸭-----------------------------------------------------
---------------------------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------------------------------------------------------------