Redis(jedis操作)

本文涉及的产品
云数据库 Tair(兼容Redis),内存型 2GB
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
简介: 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;
    }
}
相关实践学习
基于Redis实现在线游戏积分排行榜
本场景将介绍如何基于Redis数据库实现在线游戏中的游戏玩家积分排行榜功能。
云数据库 Redis 版使用教程
云数据库Redis版是兼容Redis协议标准的、提供持久化的内存数据库服务,基于高可靠双机热备架构及可无缝扩展的集群架构,满足高读写性能场景及容量需弹性变配的业务需求。 产品详情:https://www.aliyun.com/product/kvstore &nbsp; &nbsp; ------------------------------------------------------------------------- 阿里云数据库体验:数据库上云实战 开发者云会免费提供一台带自建MySQL的源数据库&nbsp;ECS 实例和一台目标数据库&nbsp;RDS实例。跟着指引,您可以一步步实现将ECS自建数据库迁移到目标数据库RDS。 点击下方链接,领取免费ECS&amp;RDS资源,30分钟完成数据库上云实战!https://developer.aliyun.com/adc/scenario/51eefbd1894e42f6bb9acacadd3f9121?spm=a2c6h.13788135.J_3257954370.9.4ba85f24utseFl
目录
相关文章
|
4天前
|
NoSQL 网络协议 Java
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
21 7
|
5天前
|
NoSQL Java 网络安全
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
[Redis] 渐进式遍历+使用jedis操作Redis+使用Spring操作Redis
|
1天前
|
JSON NoSQL Java
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
这篇文章介绍了在Java中使用Redis客户端的几种方法,包括Jedis、SpringDataRedis和SpringBoot整合Redis的操作。文章详细解释了Jedis的基本使用步骤,Jedis连接池的创建和使用,以及在SpringBoot项目中如何配置和使用RedisTemplate和StringRedisTemplate。此外,还探讨了RedisTemplate序列化的两种实践方案,包括默认的JDK序列化和自定义的JSON序列化,以及StringRedisTemplate的使用,它要求键和值都必须是String类型。
redis的java客户端的使用(Jedis、SpringDataRedis、SpringBoot整合redis、redisTemplate序列化及stringRedisTemplate序列化)
|
1月前
|
NoSQL Java Linux
Jedis测试redis。(redis在linux虚拟机中)
该博客文章提供了使用Jedis客户端连接Linux虚拟机中的Redis服务器的步骤,包括Maven依赖配置、测试用例编写以及测试结果的截图。
|
1月前
|
缓存 NoSQL Java
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
【Azure Redis 缓存】定位Java Spring Boot 使用 Jedis 或 Lettuce 无法连接到 Redis的网络连通性步骤
|
1月前
|
缓存 NoSQL Java
【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection los
【Azure Redis 缓存 Azure Cache For Redis】当使用Jedis客户端连接Redis时候,遇见JedisConnectionException: Could not get a resource from the pool / Redis connection los
|
2月前
|
NoSQL Linux Redis
Redis性能优化问题之想确认Redis延迟变大是否因为fork耗时导致的,如何解决
Redis性能优化问题之想确认Redis延迟变大是否因为fork耗时导致的,如何解决
|
3月前
|
缓存 NoSQL Redis
redis管道操作(节省网络IO开销)
pipeline中发送的每个command都会被server立即执行,如果执行失败,将会在此后的响应中得到信息;也就是pipeline并不是表达“所有command都一起成功”的语义,管道中前面命令失败,后面命令不会有影响,继续执行。
38 1
|
3月前
|
NoSQL Java Redis
如何在 Java 中操作这些 Redis 数据结构的基本方法
如何在 Java 中操作这些 Redis 数据结构的基本方法
33 2
|
2月前
|
Java Redis 数据安全/隐私保护
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码
Redis14----Redis的java客户端-jedis的连接池,jedis本身是线程不安全的,并且频繁的创建和销毁连接会有性能损耗,最好用jedis连接池代替jedis,配置端口,密码