欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 编程资源 > 编程问答 >内容正文

编程问答

5 Jedis 操作

发布时间:2025/3/19 编程问答 38 豆豆
生活随笔 收集整理的这篇文章主要介绍了 5 Jedis 操作 小编觉得挺不错的,现在分享给大家,帮大家做个参考.

文章目录

      • 1 pom.xml
      • 2 JedisTest
      • 3 手机验证码

1 pom.xml

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.ccb</groupId><artifactId>jedis</artifactId><version>1.0-SNAPSHOT</version><dependencies><dependency><groupId>redis.clients</groupId><artifactId>jedis</artifactId><version>3.2.0</version></dependency><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>4.12</version><scope>compile</scope></dependency></dependencies> </project>

2 JedisTest

package com.ccb;import org.junit.Test; import redis.clients.jedis.Jedis;import java.util.List; import java.util.Set;public class JedisTest {public static void main(String[] args) {// 创建Jedis 对象Jedis jedis = new Jedis("172.16.201.128",6379);// 测试String value = jedis.ping();System.out.println(value);}@Testpublic void testString(){Jedis jedis = new Jedis("172.16.201.128",6379);// String 添加jedis.set("name","zhoumin");// String 获取String name = jedis.get("name");System.out.println(name);// 设置多个key-valuejedis.mset("k1","v1","k2","v2");List<String> mget = jedis.mget("k1", "k2");System.out.println(mget);// 获取keySet<String> keys = jedis.keys("*");for (String key: keys) {System.out.println(key);}}@Testpublic void testList(){Jedis jedis = new Jedis("172.16.201.128",6379);jedis.lpush("k11","lucy","mary","zhoumin");List<String> k11 = jedis.lrange("k11", 0, -1);System.out.println(k11);}@Testpublic void testSet(){Jedis jedis = new Jedis("172.16.201.128",6379);jedis.sadd("names","lucy","mary");Set<String> names = jedis.smembers("names");System.out.println(names);}@Testpublic void testHash(){Jedis jedis = new Jedis("172.16.201.128",6379);jedis.hset("user", "age", "20");String hget = jedis.hget("user", "age");System.out.println(hget);}@Testpublic void testZset(){Jedis jedis = new Jedis("172.16.201.128",6379);jedis.zadd("china",100,"shanghai");Set<String> china = jedis.zrange("china", 0, -1);System.out.println(china);} }

3 手机验证码

预期效果:
1 输入手机号,点击发送后随机生成6位数字码,2分钟有效
2 输入验证码,点击验证,返回成功或失败
3 每个手机号每天只能输入3次

分析:
1 生成随机6位数字验证码
random

2 验证码在2分钟内有效
把验证码放到redis里面,设置过期时间120秒

3 判断验证码是否一致
从redis里面获取验证码和输入的验证码进行比较

4 每个手机号每天只能发送3次验证码
incr 每次发送后+1
大于2的时候,提示不能发送

package com.ccb;import redis.clients.jedis.Jedis;import java.util.Random;public class PhoneCode {public static void main(String[] args) {// 通过手机号获取验证码// 127.0.0.1:6379> get VerifyCode18771105555:code// "769594"// verifyCode("18771105555");getRedisCode("18771105555","596653");}// 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){// 连接redisJedis jedis = new Jedis("172.16.201.128",6379);// 拼接key// 手机验证码发送次数keyString countKey = "VerifyCode" + phone + ":count";// 验证码keyString codeKey = "VerifyCode" + phone + ":code" ;String count = jedis.get(countKey);if (count == null){// 没有发送次数,第一次发送// 设置发送次数是1jedis.setex(countKey,24*60*60 ,"1");}else if (Integer.parseInt(count) <= 2){// 发送次数 +1jedis.incr(countKey);}else if (Integer.parseInt(count) > 2){// 发送三次,不能再发送System.out.println("今天的发送数次已经超过三次了");jedis.close();}// 发送的验证码放到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("172.16.201.128",6379);// 验证码keyString codeKey = "VerifyCode" + phone + ":code" ;String redisCode = jedis.get(codeKey);if (redisCode.equals(code)){System.out.println("成功");}else {System.out.println("失败");}jedis.close();}}

1 首先执行发送验证码verifyCode到redis中,得到key VerifyCode18771105555:code

[chengwen@localhost ~]$ redis-server /etc/redis.conf [chengwen@localhost ~]$ redis-cli 127.0.0.1:6379> keys * (empty array) 127.0.0.1:6379> keys *1) "VerifyCode18771105555:count"2) "names"3) "china"4) "VerifyCode18771105555:code"5) "sname"

2 根据key 获取到验证码的值

127.0.0.1:6379> get VerifyCode18771105555:code "596653" 127.0.0.1:6379>

3 根据getRedisCode(“18771105555”,“596653”)进行验证码校验

成功Process finished with exit code 0

4 当达到三次以后再次发送验证码时

今天的发送数次已经超过三次了Process finished with exit code 0

总结

以上是生活随笔为你收集整理的5 Jedis 操作的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。