5 Jedis 操作
生活随笔
收集整理的這篇文章主要介紹了
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的時候,提示不能發送
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 04 當達到三次以后再次發送驗證碼時
今天的發送數次已經超過三次了Process finished with exit code 0總結
以上是生活随笔為你收集整理的5 Jedis 操作的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 4 Redis的发布订阅
- 下一篇: 1 Zookeeper安装