jedis的创建
1.先启动redis 如果报
那么说明你redis服务器服务器端还没打开
1
2
3
4
|
//启动服务端 redis-server /etc/redis.conf //启动客户端 redis-cli |
如果启动成功,就是这样
2.创建一个maven工程
导入jedis依赖
1
2
3
4
5
6
7
|
<dependencies> <dependency> <groupid>redis.clients</groupid> <artifactid>jedis</artifactid> <version>3.2.0</version> </dependency> </dependencies> |
3.创建一个class
1
2
3
4
5
6
7
8
9
|
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实现模拟验证码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
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
1
2
3
|
jedis.mset("str1","v1","str2","v2","str3","v3"); system.out.println(jedis.mget("str1","str2","str3")); |
list
1
2
3
4
5
|
list<string> list = jedis.lrange("mylist",0,-1); for (string element : list) { system.out.println(element); } |
set
1
2
3
4
5
6
7
8
9
10
11
12
|
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
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
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
1
2
3
4
5
6
7
8
9
10
|
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验证码发送内容请搜索服务器之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持服务器之家!
原文链接:https://blog.csdn.net/Andrew0219/article/details/120329969