Posted in Redis onSeptember 25, 2021
目录
jedis的创建
1.先启动redis 如果报
那么说明你redis服务器服务器端还没打开
//启动服务端
redis-server /etc/redis.conf
//启动客户端
redis-cli
如果启动成功,就是这样
2.创建一个maven工程
导入Jedis依赖
<dependencies>
<dependency>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>3.2.0</version>
</dependency>
</dependencies>
3.创建一个class
public class JedisDemo1 {
public static void main(String[] args) {
Jedis jedis = new Jedis("xx.xxx.xx.xx",6379);
//如果redis配置了密码就在这里输入不然会连接失败
jedis.auth("xxxx");
String value = jedis.ping();
System.out.println(value);
}
}
如果是第一次连接会很大概率报错
这时候有两种方法第一种是关闭防火墙,但是这种不太好,其实只要开放6379端口连接就可以了
jedis实现模拟验证码
public class PhoneCode {
public static void main(String[] args) {
verifyCode("12345678900");
//校验验证码是否正确
// GetRedisCode("12345678900","940487");
}
//1.生成六位验证码
public static String getCode(){
Random random = new Random();
String Code = "";
for (int i = 0; i < 6; i++) {
int i1 = random.nextInt(10);
Code = Code +i1;
}
return Code;
}
//2.每个手机每天只能发三次,验证码放到redis中,设置过期时间
public static void verifyCode(String phone){
//连接redis
Jedis jedis = new Jedis("172.18.17.215",6379);
jedis.auth("1052600676");
//先自定义两个key,从而通过后面步骤给key赋一个value
//手机发送次数
String CountKey = phone+"count";
//验证码key
String CodeKey = phone + "code";
//每个手机每天只能发送三次
//通过key查看是否有value
String count = jedis.get(CountKey);
if (count == null){
//没有发送次数,说明是第一次发送,那就设置发送次数为1
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;
}
//设置过期时间
//value
String codee = getCode();
jedis.setex(CodeKey,120,codee);
jedis.close();
}
//3.验证码校验
public static void GetRedisCode(String phone,String code){
//连接redis
Jedis jedis = new Jedis("172.18.17.215",6379);
jedis.auth("1052600676");
//验证码key
String CodeKey = phone + "code";
String RedisCode = jedis.get(CodeKey);
if (RedisCode.equals(code)){
System.out.println("成功");
}else{
System.out.println("失败");
}
jedis.close();
}
}
通过这些操作实现验证码发送
超过三次
相关数据类型测试
Key
jedis.set("k1", "v1");
jedis.set("k2", "v2");
jedis.set("k3", "v3");
Set<String> keys = jedis.keys("*");
System.out.println(keys.size());
for (String key : keys) {
System.out.println(key);
}
System.out.println(jedis.exists("k1"));
System.out.println(jedis.ttl("k1"));
System.out.println(jedis.get("k1"));
String
jedis.mset("str1","v1","str2","v2","str3","v3");
System.out.println(jedis.mget("str1","str2","str3"));
List
List<String> list = jedis.lrange("mylist",0,-1);
for (String element : list) {
System.out.println(element);
}
set
jedis.sadd("orders", "order01");
jedis.sadd("orders", "order02");
jedis.sadd("orders", "order03");
jedis.sadd("orders", "order04");
Set<String> smembers = jedis.smembers("orders");
for (String order : smembers) {
System.out.println(order);
}
jedis.srem("orders", "order02");
hash
jedis.hset("hash1","userName","lisi");
System.out.println(jedis.hget("hash1","userName"));
Map<String,String> map = new HashMap<String,String>();
map.put("telphone","13810169999");
map.put("address","atguigu");
map.put("email","abc@163.com");
jedis.hmset("hash2",map);
List<String> result = jedis.hmget("hash2","telphone","email");
for (String element : result) {
System.out.println(element);
}
zset
jedis.zadd("zset01", 100d, "z3");
jedis.zadd("zset01", 90d, "l4");
jedis.zadd("zset01", 80d, "w5");
jedis.zadd("zset01", 70d, "z6");
Set<String> zrange = jedis.zrange("zset01", 0, -1);
for (String e : zrange) {
System.out.println(e);
}
到此这篇关于Jedis操作Redis实现模拟验证码发送的文章就介绍到这了,更多相关Redis验证码发送内容请搜索三水点靠木以前的文章或继续浏览下面的相关文章希望大家以后多多支持三水点靠木!
Jedis操作Redis实现模拟验证码发送功能
- Author -
Andrew0219- Original Sources -
声明:登载此文出于传递更多信息之目的,并不意味着赞同其观点或证实其描述。
Reply on: @reply_date@
@reply_contents@