Code -> GitHub
https://github.com/liufengji/redis_cli.git
1、key
@Test
public static void Jedis_key_API() {
Jedis jedis = new Jedis("192.168.67.143", 6379);
Set<String> keys = jedis.keys("*");
for (Iterator iterator = keys.iterator(); iterator.hasNext();) {
String key = (String) iterator.next();
System.out.println(key);
}
System.out.println("jedis.exists====>"+jedis.exists("k2"));
System.out.println(jedis.ttl("k1"));
jedis.close();
}
2、String
@Test
public static void Jedis_String_API() {
Jedis jedis = new Jedis("192.168.67.143", 6379);
System.out.println(jedis.get("k1"));
jedis.set("k4","k4_Redis");
System.out.println("----------------------------------------");
jedis.mset("str1","v1","str2","v2","str3","v3");
System.out.println(jedis.mget("str1","str2","str3"));
jedis.close();
}
3、List
@Test
public static void Jedis_List_API() {
Jedis jedis = new Jedis("192.168.67.143", 6379);
List<String> list = jedis.lrange("mylist",0,-1);
for (String element : list) {
System.out.println(element);
}
jedis.close();
}
4、Set
@Test
public static void Jedis_set_API() {
Jedis jedis = new Jedis("192.168.67.143", 6379);
jedis.sadd("orders","jd001");
jedis.sadd("orders","jd002");
jedis.sadd("orders","jd003");
Set<String> set1 = jedis.smembers("orders");
for (Iterator iterator = set1.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
System.out.println(string);
}
jedis.srem("orders","jd002");
jedis.close();
}
5、Hash
@Test
public static void Jedis_hash_API() {
Jedis jedis = new Jedis("192.168.67.143", 6379);
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","victor");
map.put("email","liufengji@aliyun.com");
jedis.hmset("hash2",map);
List<String> result = jedis.hmget("hash2", "telphone","email");
for (String element : result) {
System.out.println(element);
}
jedis.close();
}
6、ZSet
@Test
public static void Jedis_zset_API() {
Jedis jedis = new Jedis("192.168.67.143", 6379);
jedis.zadd("zset01",60d,"v1");
jedis.zadd("zset01",70d,"v2");
jedis.zadd("zset01",80d,"v3");
jedis.zadd("zset01",90d,"v4");
Set<String> s1 = jedis.zrange("zset01",0,-1);
for (Iterator iterator = s1.iterator(); iterator.hasNext();) {
String string = (String) iterator.next();
System.out.println(string);
}
jedis.close();
}