1. 程式人生 > 其它 >Redis_Jedis:測試連線以及一個簡單的手機驗證碼功能

Redis_Jedis:測試連線以及一個簡單的手機驗證碼功能

一:Jedis所需要的jar包

<dependency>
    <groupId>redis.clients</groupId>
    <artifactId>jedis</artifactId>
    <version>3.2.0</version>
</dependency>

連線Redis注意事項

禁用Linux的防火牆:Linux(CentOS7)裡執行命令

systemctl stop/disable firewalld.service

redis.conf中註釋掉bind 127.0.0.1

,然後 protected-mode no

建立測試程式:

public class JedisDemo {

    public static void main(String[] args) {
        //建立Jedis物件
        Jedis jedis = new Jedis("192.168.187.131", 6379);
        //測試
        String value = jedis.ping();
        System.out.println(value);
        jedis.close();
    }
}

測試相關資料型別:

/**
     * 操作key string
     */
    @Test
    public void demo1() {
        //建立Jedis物件
        Jedis jedis = new Jedis("192.168.187.131", 6379);

        //新增
        jedis.set("name","lucy");

        //獲取
        String name = jedis.get("name");
        System.out.println(name);

        //設定多個key-value
        jedis.mset("k1","v1","k2","v2");
        List
<String> mget = jedis.mget("k1", "k2"); System.out.println(mget); Set<String> keys = jedis.keys("*"); for (String key: keys) { System.out.println(key); } jedis.close(); } /** * 操作List */ @Test public void demo2() { //建立Jedis物件 Jedis jedis = new Jedis("192.168.187.131", 6379); jedis.lpush("key1","lucy","mary","jack"); List<String> values = jedis.lrange("key1", 0, -1); System.out.println(values); jedis.close(); } /** * 操作set */ @Test public void demo3() { //建立Jedis物件 Jedis jedis = new Jedis("192.168.187.131", 6379); jedis.sadd("names","lucy"); jedis.sadd("names","marry"); Set<String> names = jedis.smembers("names"); System.out.println(names); jedis.close(); } /** * 操作hash */ @Test public void demo4() { //建立Jedis物件 Jedis jedis = new Jedis("192.168.187.131", 6379); jedis.hset("users","age","20"); String hget = jedis.hget("users", "age"); System.out.println(hget); jedis.close(); } @Test public void demo5() { //建立Jedis物件 Jedis jedis = new Jedis("192.168.187.131", 6379); jedis.zadd("china",100d,"shanghai"); Set<String> china = jedis.zrange("china", 0, -1); System.out.println(china); jedis.close(); }

二:完成一個手機驗證功能

要求:

1、輸入手機號,點擊發送後隨機生成6位數字碼,2分鐘有效

2、輸入驗證碼,點選驗證,返回成功或失敗

3、每個手機號每天只能輸入3次

package com.lzr.jedis;

import redis.clients.jedis.Jedis;

import java.util.Random;

/**
 * @author LZR
 * @create 2022-03-05-20:54
 */
public class PhoneCode {

    public static void main(String[] args) {
        //模擬驗證碼傳送
        verifyCode("13678765435");


    }

    /**
     * 3.驗證碼校驗
     */
    public static void getRedisCode(String phone,String code) {
        //從redis獲取驗證碼
        Jedis jedis = new Jedis("192.168.187.131", 6379);

        //驗證碼key
        String codeKey = "VerifyCode"+phone+":code";
        String redisCode = jedis.get(codeKey);
        //判斷
        if (redisCode.equals(code)) {
            System.out.println("成功");
        } else {
            System.out.println("失敗");
        }
        jedis.close();
    }

    /**
     * 2.每個手機每天只能傳送三次,驗證碼放到redis中,設定過期時間120
     */
    private static void verifyCode(String phone) {
        //連線redis
        Jedis jedis = new Jedis("192.168.187.131", 6379);

        //拼接key
        //手機發送次數key
        String countKey = "VerifyCode"+phone+":count";
        //驗證碼key
        String codeKey = "VerifyCode"+phone+":code";

        //每個手機每天只能傳送三次
        String count = jedis.get(countKey);
        if (count == null) {
            //沒有傳送次數,第一次傳送
            //設定傳送次數是1
            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();
        }

        //傳送驗證碼放到redis裡面
        String vcode = getCode();
        jedis.setex(codeKey,120,vcode);
        jedis.close();
    }


    /**
     * 1.生成6位數字驗證碼
     */
    private 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;
    }
}