日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

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

编程问答

5 Jedis 操作

發布時間:2025/3/19 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 操作的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。