RedisJedis测试

5/17/2019 redis笔记

# 创建动态的工程

# 创建测试程序

package com.atguigu.jedis;
import redis.clients.jedis.Jedis;
    public class Demo01 {
        public static void main(String\[\] args) {
        Jedis jedis = new Jedis("192.168.137.3",6379);
        String pong = jedis.ping();
        System.out.println("连接成功:"+pong);
        jedis.close();
    }
}
1
2
3
4
5
6
7
8
9
10

# 测试相关数据类型

# Jedis-API: Key

jedis.set("k1", "v1");
jedis.set("k2", "v2");
jedis.set("k3", "v3");
Set <string>keys = jedis.keys("*");
System.out.println(keys.size());
for (String key : keys) {
    System.out.println(key);
}
System.out.println(jedis.exists("k1"));
System.out.println(jedis.ttl("k1"));
System.out.println(jedis.get("k1"));
1
2
3
4
5
6
7
8
9
10
11

# Jedis-API: String

jedis.mset("str1","v1","str2","v2","str3","v3");
System.out.println(jedis.mget("str1","str2","str3"));
1
2

# Jedis-API: List

List <string>list = jedis.lrange("mylist",0,-1);
for (String element : list) {
    System.out.println(element);
}
1
2
3
4

# Jedis-API: set

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

# Jedis-API: hash

jedis.hset("hash1","userName","lisi");
System.out.println(jedis.hget("hash1","userName"));
Map&lt;String,String&gt; map = new HashMap&lt;String,String&gt;();
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

# Jedis-API: zset

jedis.zadd("zset01", 100d, "z3");
jedis.zadd("zset01", 90d, "l4");
jedis.zadd("zset01", 80d, "w5");
jedis.zadd("zset01", 70d, "z6");</string></string></string></string>

Set <string>zrange = jedis.zrange("zset01", 0, -1);
for (String e : zrange) {
System.out.println(e);
}

8.  
1
2
3
4
5
6
7
8
9
10
11
Last Updated: 3/23/2022, 10:00:11 AM