Jedis操作Redis实现模拟验证码发送功能
程序员文章站
2022-06-16 22:24:05
目录keystringlistjedis的创建1.先启动redis 如果报那么说明你redis服务器服务器端还没打开//启动服务端redis-server /etc/redis.conf//启动客户端...
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验证码发送内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!