目录
一、测试
1.引入Jedis所需要的jar包
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>4.3.0-m2</version>
</dependency>
</dependencies>
import redis.clients.jedis.Jedis;
public class JedisDemo1 {
public static void main(String[] args) {
//1.创建Jedis对象
Jedis jedis = new Jedis("192.168.115.100",6379);
//2.测试
String value = jedis.ping();
System.out.println(value);
}
}
🐟使用Jedis传入的两个参数的源码
public Jedis(String host, int port) {
this.commandObjects = new CommandObjects();
this.db = 0;
this.transaction = null;
this.isInMulti = false;
this.isInWatch = false;
this.pipeline = null;
this.dataSource = null;
this.connection = new Connection(host, port);
}
🐟操作key
①查看已经添加的所有数据
//操作key
@Test
public void demo1() throws InterruptedException {
//创建jedis对象
Jedis jedis = new Jedis("192.168.115.100",6379);
Set<String> keys = jedis.keys("*");
for (String key : keys){
System.out.println(key);
}
}
②数据添加及查看
//添加数据
jedis.set("name","lucy");
//获取数据
String name = jedis.get("name");
System.out.println(name);
③同时添加多个key-value并查看
//设置多个key和value
jedis.mset("k1","v1","k2","v2");
List<String> mget = jedis.mget("k1", "k2");
System.out.println(mget);
④Jedis操作list
//操作list
@Test
public void demo2(){
Jedis jedis = new Jedis("192.168.115.100",6379);
//添加数据
jedis.lpush("key1","zhangsan","lisi","wangwu");
//输出数据
List<String> key1 = jedis.lrange("key1", 0, -1);
System.out.println(key1);
⑤Jedis操作set
//操作set
@Test
public void demo3(){
Jedis jedis = new Jedis("192.168.115.100",6379);
//添加数据
jedis.sadd("names","zhangsan","lisi");
//输出数据
Set<String> names = jedis.smembers("names");
System.out.println(names);
}
⑥Jedis操作hash
//操作hash
@Test
public void demo4(){
Jedis jedis = new Jedis("192.168.115.100",6379);
//添加数据
jedis.hset("school","beijing","beida");
//输出数据
String school = jedis.hget("school", "beijing");
System.out.println(school);
}
⑦ Jedis操作zset
//操作zset
@Test
public void demo5(){
Jedis jedis = new Jedis("192.168.115.100",6379);
//添加数据
jedis.zadd("china",100,"xian");
//输出数据
List<String> china = jedis.zrange("china", 0, -1);
System.out.println(china);
}
2.连接Redis的注意事项
①禁用Linux的防火墙:Linux(CentOS7)里执行命令
systemcal stop/disable firewalld.service
②将本机访问保护模式设置为no
③更改完成之后重启redis服务
redis关于防火墙的操作
1、查看虚拟机防火墙状态
systemctl status firewalld
2、如何关闭防火墙
systemctl stop firewalld
3、如何启动防火墙
systemctl start firewalld
4、如何重启防火墙(先停止,再启动)
systemctl restart firewalld
二、Redis实例 —— 模拟验证码的发送
1.要求
2.分析 & 实现
①生成随机6位数字验证码
- 使用Random
//1.生成随机6位数字验证码
public static String getCode(){
Random random = new Random();
String code = "";
for (int i = 0;i < 6;i++){
int rand = random.nextInt(10);
code += rand;
}
return code;
}
②两分钟之内有效
- 把验证码放到redis里,设置过期时间为120秒
③每天只能输入3次
- incr每次发送之后 +1
- 当大于2(或等于3) 时,提示不能再次发送验证码
//2.每天只能输入3次,验证码放到redis中,设置过期时间
public static void verifyCode(String phone){
//连接redis
Jedis jedis = new Jedis("192.168.115.100",6379);
//拼接key
//①手机发送次数
String countKey = "VerifyCode" + phone + ":count";
//②验证码key
String codeKey = "VerifyCode" + phone + ":code";
//每天只能发三次
String count = jedis.get(countKey);
if (count == null){
//没有发送,说明为第一次发送
//设置发送次数是1
jedis.setex(countKey,24*60*60,"1");
}else if (Integer.parseInt(count) <= 2){
//发送次数加1
jedis.incr(countKey);
}else if(Integer.parseInt(count) > 2){
//不能再进行发送
System.out.println("今天的发送次数已用尽!!!");
jedis.close();
return; //使后续代码不再执行
}
//发送的验证码要放到redis中
String vCode = getCode();
jedis.setex(codeKey,120,vCode);
jedis.close();
}
④验证码是否一致
- 从redis中获取的验证码与输入的验证码进行比较,判断是否一致
//3.验证码校验
public static void getRedisCode(String phone,String code){
//连接redis
Jedis jedis = new Jedis("192.168.115.100",6379);
//验证码key
String codeKey = "VerifyCode" + phone + ":code";
String redisCode = jedis.get(codeKey);
//判断
if (redisCode.equals(code)){
System.out.println("success");
}else {
System.out.println("false!!!");
}
jedis.close();
}
3.最终效果
package com.atxupt.jedis;
import redis.clients.jedis.Jedis;
import java.nio.charset.StandardCharsets;
import java.util.Random;
public class PhoneCode {
public static void main(String[] args) {
//模拟验证码的发送
// verifyCode("12345678910");
//验证是否成功
getRedisCode("12345678910","368602");
}
//1.生成随机6位数字验证码
public static String getCode(){
Random random = new Random();
String code = "";
for (int i = 0;i < 6;i++){
int rand = random.nextInt(10);
code += rand;
}
return code;
}
//2.每天只能输入3次,验证码放到redis中,设置过期时间
public static void verifyCode(String phone){
//连接redis
Jedis jedis = new Jedis("192.168.115.100",6379);
//拼接key
//①手机发送次数
String countKey = "VerifyCode" + phone + ":count";
//②验证码key
String codeKey = "VerifyCode" + phone + ":code";
//每天只能发三次
String count = jedis.get(countKey);
if (count == null){
//没有发送,说明为第一次发送
//设置发送次数是1
jedis.setex(countKey,24*60*60,"1");
}else if (Integer.parseInt(count) <= 2){
//发送次数加1
jedis.incr(countKey);
}else if(Integer.parseInt(count) > 2){
//不能再进行发送
System.out.println("今天的发送次数已用尽!!!");
jedis.close();
return; //使后续代码不再执行
}
//发送的验证码要放到redis中
String vCode = getCode();
jedis.setex(codeKey,120,vCode);
jedis.close();
}
//3.验证码校验
public static void getRedisCode(String phone,String code){
//连接redis
Jedis jedis = new Jedis("192.168.115.100",6379);
//验证码key
String codeKey = "VerifyCode" + phone + ":code";
String redisCode = jedis.get(codeKey);
//判断
if (redisCode.equals(code)){
System.out.println("success");
}else {
System.out.println("false!!!");
}
jedis.close();
}
}