【Redis-06】Redis短信发送案例&Jedis学习

【Redis-06】Redis短信发送案例&Jedis学习

1. 案例说明

1、输入手机号,点击发送后随机生成6位数字码,2分钟有效
2、输入验证码,点击验证,返回成功或失败
3、每个手机号每天只能输入3次

2. 相关代码

package com.atguigu.jedis;

import redis.clients.jedis.Jedis;

import java.util.Random;

/**
 * @author fanzhen1@lixiang.com
 * @version 1.0
 * @date 2022/12/20
 */
public class PhoneCode {
    public static void main(String[] args) {
//        verifyCode("18801171255");

        //模拟验证码校验
        getRedisCode("13678765435","121123");
    }

    /**
     * 生成6位数字验证码
     *
     * @return
     */
    private static String getCode() {
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 6; i++) {
            int randomNum = random.nextInt(10);
            code += randomNum;
        }

        return code;
    }

    /**
     * 每个手机每天只能发送三次,验证码放在redis中,设置过期时间120秒
     */
    private static void verifyCode(String phone) {
        // 连接redis
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        // 拼接key
        // 手机发送次数key
        String countKey = "VertifyCode" + phone + ":count";
        // 验证码key
        String codeKey = "VertifyCode" + 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) {
            // 发送三次,不能再发送
            jedis.close();
        }

        // 发送的验证码放在redis中
        String vCode = getCode();
        jedis.setex(codeKey, 120, vCode);
        jedis.close();
    }

    /**
     * 验证码校验
     */
    public static void getRedisCode(String phone, String code) {
        // 从redis获取验证码
        // 连接redis
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        String codeKey = "VertifyCode" + phone + ":code";
        String redisCode = jedis.get(codeKey);

        // 校验
        if (redisCode.equals(code)) {
            // 成功
        } else {
            // 校验失败
        }

        jedis.close();
    }
}

<?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>org.example</groupId>
    <artifactId>jedis_redisDemo</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <!-- 设置 JDK 版本为 1.8 -->
        <maven.compiler.target>1.8</maven.compiler.target>
        <maven.compiler.source>1.8</maven.compiler.source>
        <!-- 设置编码为 UTF-8 -->
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <maven.compiler.encoding>UTF-8</maven.compiler.encoding>
    </properties>

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

        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.13.1</version>
            <scope>compile</scope>
        </dependency>
        <dependency>
            <groupId>org.apache.commons</groupId>
            <artifactId>commons-lang3</artifactId>
            <version>3.8.1</version>
        </dependency>
    </dependencies>
</project>

3.Jedis

package com.atguigu.jedis;

import org.junit.Test;
import redis.clients.jedis.Jedis;

import java.util.List;
import java.util.Set;

/**
 * @author fanzhen1@lixiang.com
 * @version 1.0
 * @date 2022/12/20
 */
public class JedisDemo1 {
    public static void main(String[] args) {
        // 创建jedis对象
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        // 测试 返回值如果是PONG则说明连接上了
        String ping = jedis.ping();
        System.out.println(ping);

        jedis.close();
    }

    /**
     * 操作key
     */
    @Test
    public void operateKey() {
        // 创建jedis对象
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.set("v1", "100");
        jedis.set("v2", "100");
        jedis.set("v3","999");

        jedis.incr("v3");
        System.out.println(jedis.get("v3"));

        // 设置多个key-value
        jedis.mset("k1", "v1", "k2", "v2");
        List<String> mGet = jedis.mget("k1", "k2");
        System.out.println(mGet);

        // 获取所有key
        Set<String> keys = jedis.keys("*");
        keys.forEach(System.out::println);

        System.out.println(jedis.exists("v1"));
        System.out.println(jedis.ttl("v1"));
        System.out.println(jedis.get("v1"));

        jedis.close();
    }

    /**
     * 操作List
     */
    @Test
    public void operateList() {
        // 创建jedis对象
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        jedis.rpush("key1", "lucy", "mary", "jack");
        List<String> valueList = jedis.lrange("key1", 0, -1);
        System.out.println(valueList);

        System.out.println(jedis.lindex("key1", 1));
        System.out.println(jedis.llen("key1"));

        jedis.close();
    }

    /**
     * 操作set
     */
    @Test
    public void operateSet() {
        Jedis jedis = new Jedis("127.0.0.1", 6379);

        jedis.sadd("name","lucy","jack","jack");
        Set<String> smembers = jedis.smembers("name");
        System.out.println(smembers);

        jedis.srem("name","jack");
        System.out.println(jedis.smembers("name"));

        jedis.close();
    }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

boy快快长大

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值