• java操作redis


    jedis操作

    顾名思义就是通过java来操作redis

    测试能否连接redis

    1.创建一个普通的maven工程命名为jedis_redisdemo

    image-20220813220115870

    导入jar包

        <dependencies>
            <dependency>
                <groupId>redis.clientsgroupId>
                <artifactId>jedisartifactId>
                <version>3.2.0version>
            dependency>
        dependencies>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    2.在src/main/java下创建对应的包和类,类名JedisDemo1

    image-20220813220422783

    书写类中代码,目的是测试能否连通我们服务器上的redis

    package com.qcby.jedis;
    
    import redis.clients.jedis.Jedis;
    
    public class JedisDemo1 {
        public static void main(String[] args) {
            //创建Jedis对象,注意修改成自己的服务器ip
            Jedis jedis = new Jedis("127.0.0.1",6379);
            //配置redis的密码
            jedis.auth("cjj");
            //测试
            String value = jedis.ping();
            System.out.println(value);
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15

    在测试之前首先要注释掉服务器的bind,否则只能本机访问

    image-20220813220935167

    关闭保护模式,将protected-mode修改为no

    image-20220813221029015

    修改完成后将旧的redis线程杀死然后重新启动修改后的配置文件

    image-20220813221414580

    3.暂时关闭防火墙

    firewall指令

    #查看状态
    systemctl status firewalld
    # 开启
    service firewalld start
    # 重启
    service firewalld restart
    # 关闭
    service firewalld stop
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    4.出现pong证明连通成功

    image-20220813222136975

    测试key

    添加一个demo1方法

        public static void demo1(Jedis jedis){
            jedis.set("k1", "v1");
            jedis.set("k2", "v2");
            jedis.set("k3", "v3");
    
            Set<String> keys = jedis.keys("*");
            System.out.println("key键值个数:");
            System.out.println(keys.size());
            System.out.println("key键值有哪些:");
            for (String key : keys) {
                System.out.println(key);
            }
            System.out.println("是否存在k1");
            System.out.println(jedis.exists("k1"));
            System.out.println("k1过期时间");
            System.out.println(jedis.ttl("k1"));
            System.out.println("k1的值");
            System.out.println(jedis.get("k1"));
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    执行结果

    image-20220813225000136

    测试String

    代码:

    public static void demo2(Jedis jedis){
            jedis.mset("str1","v1","str2","v2","str3","v3");
            System.out.println(jedis.mget("str1","str2","str3"));
        }
    
    • 1
    • 2
    • 3
    • 4

    结果

    image-20220813225642536

    测试List

    代码

        public static void demo3(Jedis jedis){
            jedis.lpush("mylist","a","b","c");
            List<String> list = jedis.lrange("mylist",0,-1);
            for (String element : list) {
                System.out.println(element);
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    结果

    image-20220813230031977

    测试hash

    代码:

        public static void demo4(Jedis jedis){
            jedis.sadd("orders", "order01");
            jedis.sadd("orders", "order02");
            jedis.sadd("orders", "order03");
            jedis.sadd("orders", "order04");
            Set<String> smembers = jedis.smembers("orders");
            for (String order : smembers) {
                System.out.println(order);
            }
            jedis.srem("orders", "order02");
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    结果:

    image-20220813230131162

    测试set

    代码:

        public static void demo5(Jedis jedis){
            jedis.hset("hash1","userName","lisi");
            System.out.println(jedis.hget("hash1","userName"));
            Map<String,String> map = new HashMap<String,String>();
            map.put("telphone","13810169999");
            map.put("address","atguigu");
            map.put("email","abc@163.com");
            jedis.hmset("hash2",map);
            List<String> result = jedis.hmget("hash2", "telphone","email");
            for (String element : result) {
                System.out.println(element);
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    结果:

    测试zset

    代码

        public static void demo6(Jedis jedis){
            jedis.zadd("zset01", 100d, "z3");
            jedis.zadd("zset01", 90d, "l4");
            jedis.zadd("zset01", 80d, "w5");
            jedis.zadd("zset01", 70d, "z6");
    
            Set<String> zrange = jedis.zrange("zset01", 0, -1);
    
            for (String e : zrange) {
                System.out.println(e);
            }
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    结果

    image-20220813230322207

    测试完后要关闭jedis

  • 相关阅读:
    商业管理和经济学哪个好ib?
    事件总线--EvenBus
    日语 11 12
    4.2——Node.js的npm和包
    SpringCloud环境搭建 --- Rest使用
    Docker Compose介绍和安装
    MCE | Hippo 途径与靶向策略
    【HMS core】【FAQ】HMS Toolkit典型问题合集1
    微信小程序预约视频号直播
    ElasticSearch集群配置
  • 原文地址:https://blog.csdn.net/perturb/article/details/126376271