Jedis常用操作

1. Key

  • 查看服务器上 key 的值

Jedis常用操作

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

        System.out.println("===遍历key*===");
        Set<String> keys = jedis.keys("*");
        for (String key : keys) {
            System.out.println(key);
        }
        System.out.println("===添加一个name数据===");
        //添加
        jedis.set("name", "xdr");
        //获取
        String name = jedis.get("name");
        System.out.println(name);
        System.out.println("===查询k1是否存在===");
        System.out.println(jedis.exists("k1"));
        System.out.println("===查看k1过期时间===");
        System.out.println(jedis.ttl("k1"));
        System.out.println("===获取name的值===");
        System.out.println(jedis.get("name"));
    }

Jedis常用操作

2. String

       @Test
       public void demo2() {
       Jedis jedis = new Jedis("47.107.53.146", 6379);
       //设置多个 key-value
       jedis.mset("k1", "v1", "k2", "v2");
       List<String> mget = jedis.mget("k1", "k2");
       System.out.println(mget);
   }

Jedis常用操作

3. List

    @Test
    public void demo3() {
        Jedis jedis = new Jedis("47.107.53.146", 6379);
        //设置多个 key-value
        jedis.lpush("key1", "jack", "lucy", "mike");
        List<String> values = jedis.lrange("key1", 0, -1);
        System.out.println(values);
        
    }

Jedis常用操作

4. set

    @Test
    public void demo4() {
        Jedis jedis = new Jedis("47.107.53.146", 6379);
        //设置多个 key-value
        jedis.sadd("names", "jack", "lucy");

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

    }

Jedis常用操作

5. hash

    @Test
    public void demo5() {
        Jedis jedis = new Jedis("47.107.53.146", 6379);
        jedis.hset("users", "age", "20");
        String hget = jedis.hget("users", "age");
        System.out.println(hget);

    }

Jedis常用操作

6. zset

    @Test
    public void demo6() {
        Jedis jedis = new Jedis("47.107.53.146", 6379);
        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);
        }
    }

Jedis常用操作

上一篇:Navicat报错:2003 - Can‘t connect to MySQL server on ‘xxx‘ (10060 “Unknown error“)


下一篇:Jedis连接阿里云redis