Redis知識體系總結(2021版)

專注的阿熊發表於2021-05-26

2 、五大資料型別程式碼例項

package com.guor.redis;

import redis.clients.jedis.Jedis;

import java.util.List;

import java.util.Set;

public class JedisTest01 {

     public static void main(String[] args) {

         test05();

     }

     private static void test01(){

         Jedis jedis = new Jedis("192.168.194.131", 6379);

         String value = jedis.ping();

         System.out.println(value);

         // 新增

         jedis.set("name","GooReey");

         // 獲取

         String name = jedis.get("name");

         System.out.println(name);

         jedis.set("age","30");

         jedis.set("city","dalian");

         // 獲取全部的 key

         Set<String> keys = jedis.keys("*");

         for(String key : keys){

             System.out.println(key+" --> "+jedis.get(key));

         }

         // 加入多個 key value

         jedis.mset("name1","zs","name2","ls","name3","ww");

         List<String> mget = jedis.mget("name1", "name2");

         System.out.println(mget);//[zs, ls]

     }

     //list

     private static void test02(){

         Jedis jedis = new Jedis("192.168.194.131", 6379);

         jedis.lpush("key1","01","02","03");

         List<String> values = jedis.lrange("key1",0,-1);

         System.out.println(values);//[03, 02, 01]

     }

     //set

     private static void test03(){

         Jedis jedis = new Jedis("192.168.194.131", 6379);

         jedis.sadd("username","zs","ls","ww");

         Set<String> names = jedis.smembers("username");

         System.out.println(names);//[ww, zs, ls]

     }

     //hash

     private static void test04(){

         Jedis jedis = new Jedis("192.168.194.131", 6379);

         jedis.hset("users","age", "20");

         String hget = jedis.hget("users","age");

         System.out.println(hget);

     }

     //zset

     private static void test05(){

         Jedis jedis = new Jedis("192.168.194.131", 6379);

         jedis.zadd("china",100d,"shanghai");

         Set<String> names = jedis.zrange("china",0,-1);

         System.out.println(names);//[shanghai]

     }

}

3 、手機驗證碼功能程式碼例項

package com.guor.redis;

import redis.clients.jedis.Jedis;

import java.util.Random;

public class PhoneCode {

     public static void main(String[] args) {

         verifyCode("10086");//795258

         getRedisCode("10086","795258");//success.

     }

     //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;//849130

     }

     //2 、每個手機每天只能傳送三次,外匯跟單gendan5.com驗證碼放到 redis 中,設定過期時間

     public static void verifyCode(String phone){

         Jedis jedis = new Jedis("192.168.194.131", 6379);

         // 拼接 key

         // 手機傳送次數 key

         String countKey = "VerifyCode" + phone + ":count";

         // 驗證碼 key

         String codeKey = "VerifyCode" + phone + ":code";

         // 每個手機每天只能傳送三次

         String count = jedis.get(countKey);

         if(count == null){

             // 設定過期時間

             jedis.setex(countKey,24*60*60,"1");

         }else if(Integer.parseInt(count)<=2){

             // 傳送次數 +1

             jedis.incr(countKey);

         }else if(Integer.parseInt(count)>2){

             System.out.println(" 今天的傳送次數已經超過三次 ");

             jedis.close();

         }

         String vCode = getCode();

         jedis.setex(codeKey,120,vCode);

         jedis.close();

     }

     //3 、驗證碼校驗

     public static void getRedisCode(String phone, String code){

         // redis 中獲取驗證碼

         Jedis jedis = new Jedis("192.168.194.131", 6379);

         // 驗證碼 key

         String codeKey = "VerifyCode" + phone + ":code";

         String redisCode = jedis.get(codeKey);

         if(redisCode.equals(code)){

             System.out.println("success.");

         }else{

             System.out.println("error");

         }

         jedis.close();

     }

}


來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/69946337/viewspace-2774087/,如需轉載,請註明出處,否則將追究法律責任。

相關文章