5 Jedis 操作
生活随笔
收集整理的這篇文章主要介紹了
5 Jedis 操作
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
文章目錄
- 1 pom.xml
- 2 JedisTest
- 3 手機(jī)驗(yàn)證碼
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) {// 創(chuàng)建Jedis 對(duì)象Jedis jedis = new Jedis("172.16.201.128",6379);// 測(cè)試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);// 設(shè)置多個(gè)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 手機(jī)驗(yàn)證碼
預(yù)期效果:
1 輸入手機(jī)號(hào),點(diǎn)擊發(fā)送后隨機(jī)生成6位數(shù)字碼,2分鐘有效
2 輸入驗(yàn)證碼,點(diǎn)擊驗(yàn)證,返回成功或失敗
3 每個(gè)手機(jī)號(hào)每天只能輸入3次
分析:
1 生成隨機(jī)6位數(shù)字驗(yàn)證碼
random
2 驗(yàn)證碼在2分鐘內(nèi)有效
把驗(yàn)證碼放到redis里面,設(shè)置過(guò)期時(shí)間120秒
3 判斷驗(yàn)證碼是否一致
從redis里面獲取驗(yàn)證碼和輸入的驗(yàn)證碼進(jìn)行比較
4 每個(gè)手機(jī)號(hào)每天只能發(fā)送3次驗(yàn)證碼
incr 每次發(fā)送后+1
大于2的時(shí)候,提示不能發(fā)送
1 首先執(zhí)行發(fā)送驗(yàn)證碼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 根據(jù)key 獲取到驗(yàn)證碼的值
127.0.0.1:6379> get VerifyCode18771105555:code "596653" 127.0.0.1:6379>3 根據(jù)getRedisCode(“18771105555”,“596653”)進(jìn)行驗(yàn)證碼校驗(yàn)
成功Process finished with exit code 04 當(dāng)達(dá)到三次以后再次發(fā)送驗(yàn)證碼時(shí)
今天的發(fā)送數(shù)次已經(jīng)超過(guò)三次了Process finished with exit code 0總結(jié)
以上是生活随笔為你收集整理的5 Jedis 操作的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 4 Redis的发布订阅
- 下一篇: 6 Springboot 整合Redis