![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8bf51961a3b0135749d62b8d004bd1ef.png)
环境搭建
maven坐标
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
配置文件
redis:
host: localhost
port: 6379
#password: 123456
database: 0 #0号数据库 切换数据库 select [number]
jedis:
#Redis连接池配置
pool:
max-active: 8 #最大连接数
max-wait: 1ms #连接池最大阻塞等待时间
max-idle: 4 #连接池中的最大空闲连接
min-idle: 0 #连接池中的最小空闲连接
配置类RedisConfig(暂时不用)
package com.taotao.reggie.config;
/**
* create by 刘鸿涛
* 2022/10/4 20:53
*/
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;
/**
* create by 刘鸿涛
* 2022/10/4 11:56
*/
@SuppressWarnings({"all"})
@Configuration
public class RedisConfig extends CachingConfigurerSupport {
@Bean
public RedisTemplate<Object,Object> redisTemplate (RedisConnectionFactory connectionFactory){
RedisTemplate<Object, Object> redisTemplate = new RedisTemplate<>();
redisTemplate.setKeySerializer(new StringRedisSerializer());
redisTemplate.setHashKeySerializer(new StringRedisSerializer());
redisTemplate.setConnectionFactory(connectionFactory);
return redisTemplate;
}
}
缓存短信验证码
实现思路
代码改造
UserController.java
send
login
功能测试
在redis中保存5分钟
登录成功后,此数据消失