Jedis-springboot整合redis
Jedis
引入jedis依赖
注意事项
测试相关数据类型
Key
String
List
set
hash
zset
案例
spring boot整合redis
引入相关依赖
在application.properties中配置redis 配置
创建redis配置类
创建测试类
Jedis
引入jedis依赖
<dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.8.0</version>
</dependency>
注意事项
禁用 Linux 的防火墙:Linux(CentOs7)里执行命令
systemctl stop/disable firewalld.service
redis.conf中注释掉 bind 127.0.0.1,然后 protected-mode no
测试相关数据类型
Key
String
List
set
hash
zset
案例
// 该类用于处理手机验证码的生成和验证
public class PhoneCode {public static void main(String[] args) {verifyCode("13678765435");// 从Redis获取验证码并进行验证//getRedisCode("13678765435","993438");}// 生成一个6位的随机验证码public static String getCode() {Random random = new Random();String code = "";for (int i = 0; i < 6; i++) {random.nextInt(10);code += random.nextInt(10);}return code;}// 验证手机验证码的有效性,并限制每天的发送次数public static void verifyCode(String phone) {Jedis jedis = new Jedis("192.168.24.128", 6379);String countKey = "phone:VerifyCode" + phone + ":count";String codeKey = "phone:VerifyCode" + phone + ":code";String count = jedis.get(countKey);if (count == null) {jedis.setex(countKey, 24 * 60 * 60, "1");} else if (Integer.parseInt(count) <= 2) {jedis.incr(countKey);} else if (Integer.parseInt(count) > 2) {System.out.println("今天发送次数已达到上限");jedis.close();return;}String vcode = getCode();jedis.setex(codeKey, 120, vcode);jedis.close();}// 从Redis获取验证码,并与用户输入的验证码进行比对public static void getRedisCode(String phone,String code) {Jedis jedis = new Jedis("192.168.24.128", 6379);String codeKey = "phone:VerifyCode" + phone + ":code";String redisCode = jedis.get(codeKey);if(redisCode.equals(code)){System.out.println("成功");}else {System.out.println("失败");}jedis.close();}
}
spring boot整合redis
引入相关依赖
<dependency><groupId>org.spr<artifactId>spri
</dependency>
<dependency><groupId>org.apa<artifactId>comm<version>2.11.1<
</dependency>
在application.properties中配置redis 配置
创建redis配置类
这里内容直接粘贴
创建测试类
访问本机指定地址