Redis(jedis操作)


Redis的发布和订阅

Redis发布订阅(pub/sub)是一种消息通信模式:发送者(pub)发送消息,订阅者(sub)接受消息
Redis客户端可以订阅任意数量的频道

发布订阅命令行实现

打开一个客户端订阅channel1:
在这里插入图片描述
打开另一个客户端,给channel1发布消息hello:
在这里插入图片描述
打开第一客户端可以看到发送的消息:
在这里插入图片描述

Jedis操作

第一步:在pom文件下引入依赖

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

操作key string

   @Test
    public void demo1(){
        //创建Jedis对象
        Jedis jedis = new Jedis("192.168.80.130",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);
        }
    }

操作list

 @Test
    public void demo2(){
        //创建Jedis对象
        Jedis jedis = new Jedis("192.168.80.130",6379);

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

操作set

@Test
    public void demo3(){
        //创建Jedis对象
        Jedis jedis = new Jedis("192.168.80.130",6379);

        jedis.sadd("names","lucy");
        jedis.sadd("names","mary");

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

        System.out.println(names);
    }

操作hash

    @Test
    public void demo4(){
        //创建Jedis对象
        Jedis jedis = new Jedis("192.168.80.130",6379);

        jedis.hset("users","age","20");
        String hget = jedis.hget("users","age");
        System.out.println(hget);
    }

操作zset

 @Test
    public void demo5(){
        //创建Jedis对象
        Jedis jedis = new Jedis("192.168.80.130",6379);

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

        Set<String> china = jedis.zrange("china",0,-1);
        System.out.println(china);
    }

Jedis案例-模拟验证码发送

代码示例:

public class PhoneCode {
    public static void main(String[] args) {
        //模拟验证码发送
       verifyCode("13087576298");

     //   getRedisCode("13087576298","695603");
    }

    //3 验证码校验
    public static void getRedisCode(String phone,String code){
        //从redis获取验证码
        Jedis jedis = new Jedis("192.168.80.130",6379);
        //验证码key
        String codeKey = "VerifyCode"+phone+":code";
        String redisCode = jedis.get(codeKey);
        //判断
        if(redisCode.equals(code)){
            System.out.println("成功");
        }else{
            System.out.println("失败");
        }
    }

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

        //拼接key
        //手机发送次数key
        String countKey = "VerifyCode"+phone+":count";
        //验证码
        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();
            return;
        }

        //发送验证码放到redis里面
        String vcode = getCode();
        jedis.setex(codeKey,120,vcode);
        jedis.close();
    }

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

评论 6
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

杨宸杨

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

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

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

打赏作者

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

抵扣说明:

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

余额充值