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;
    }
}
复制代码

 

 

posted @   咸鱼翻身日记  阅读(158)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
点击右上角即可分享
微信分享提示