public static void test2(Jedis jedis) {
System.out.println("getrange 和 setrange 的使用");
jedis.flushAll();
jedis.set("key","abcdefghijk");
String result = jedis.getrange("key",2,5);
System.out.println("此处预期 result = cdef, 实际 result = " + result);
jedis.setrange("key",2,"xyz");
String value = jedis.get("key");
System.out.println("此处预期 value = abxyzfghijk, 实际 value = " + value);
}
运行结果:
APPEND
java复制代码
public static void test3(Jedis jedis) {
System.out.println("append 的使用");
jedis.flushAll();
jedis.set("key","abcdef");
jedis.append("key","ghij");
String value = jedis.get("key");
System.out.println("此处预期 value = abcdefghij, 实际 value = " + value);
}
运行结果:
INCR & DECR
java复制代码
public static void test4(Jedis jedis) {
System.out.println("incr 和 decr 的使用");
jedis.flushAll();
jedis.set("key","100");
long result = jedis.incr("key");
System.out.println("此处预期 result = 101, 实际 result = " + result);
String value = jedis.get("key");
System.out.println("此处预期 value = 101, 实际 value = " + value);
result = jedis.decr("key");
System.out.println("此处预期 result = 100, 实际 result = " + result);
value = jedis.get("key");
System.out.println("此处预期 value = 100, 实际 value = " + value);
}
运行结果:
List 类型命令
LPUSH & LRANG
java复制代码
public static void test1(Jedis jedis) {
System.out.println("lpush、rpush 和 lrange 的使用");
jedis.flushAll();
jedis.lpush("key","111","222","333");
jedis.rpush("key2","111","222","333");
List<String> result = jedis.lrange("key",0,-1);
System.out.println("采用 lpush 的 result = " +result);
List<String> result2 = jedis.lrange("key2",0,-1);
System.out.println("采用 rpush 的 result = " + result2);
}
运行结果:
LPOP & LPOP
java复制代码
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
public class RedisDemoList {
public static void test2(Jedis jedis) {
System.out.println("lpop、rpop 的使用");
jedis.flushAll();
jedis.rpush("key","111","222","333");
String result = jedis.lpop("key");
System.out.println("采用 lpop 的预期 result = 111, 实际 result = " + result);
result = jedis.rpop("key");
System.out.println("采用 rpop 的预期 result = 333, 实际 result = " + result);
}
public static void main(String[] args) {
JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
try (Jedis jedis = jedisPool.getResource()) {
test2(jedis);
}
}
}
public static void test2(Jedis jedis) {
System.out.println("sismembers 的使用");
jedis.flushAll();
jedis.sadd("key","111","222","333");
boolean result = jedis.sismember("key","111");
System.out.println("此处预期 result = true, 实际 result = " + result);
result = jedis.sismember("key","100");
System.out.println("此处预期 result = false, 实际 result = " + result);
}
运行结果:
SCARD
java复制代码
public static void test3(Jedis jedis) {
System.out.println("scard 的使用");
jedis.flushAll();
jedis.sadd("key","111","222","333");
long result = jedis.scard("key");
System.out.println("此处预期 result = 3, 实际 result = " + result);
}
运行结果:
SPOP
java复制代码
public static void test4(Jedis jedis) {
System.out.println("spop 的使用");
jedis.flushAll();
jedis.sadd("key","111","222","333","444","555");
String result = jedis.spop("key");
System.out.println("此处将随机删除一个元素, result = " + result);
}
运行结果:
SINTER
java复制代码
public static void test4(Jedis jedis) {
System.out.println("spop 的使用");
jedis.flushAll();
jedis.sadd("key","111","222","333","444","555");
String result = jedis.spop("key");
System.out.println("此处将随机删除一个元素, result = " + result);
}
运行结果:
SINTERSTORE
java复制代码
public static void test6(Jedis jedis) {
System.out.println("sinterstore 的使用");
jedis.flushAll();
jedis.sadd("key","111","222","333");
jedis.sadd("key2","111","222","444");
long len = jedis.sinterstore("key3","key","key2");
System.out.println("此处预期 len = 2, 实际 len = " + len);
Set<String> result = jedis.smembers("key3");
System.out.println("result = " + result);
}
运行结果:
Hash 类型命令
HSET & HGET
java复制代码
public static void test1(Jedis jedis) {
System.out.println("hset 和 hget 的使用");
jedis.flushAll();
// 一次性设置单个
jedis.hset("key","f1","111");
// 一次性设置多个
HashMap<String,String> fields = new HashMap<>();
fields.put("f2","222");
fields.put("f3","333");
jedis.hset("key",fields);
String result = jedis.hget("key","f1");
System.out.println("此处预期 result = 111, 实际 result = " + result);
result = jedis.hget("key","f100");
System.out.println("此处预期 result = null, 实际 result = " + result);
}
运行结果:
HEXISTS
java复制代码
public static void test2(Jedis jedis) {
System.out.println("hexists 的使用");
jedis.flushAll();
jedis.hset("key","f1","111");
jedis.hset("key","f2","222");
jedis.hset("key","f3","333");
boolean result = jedis.hexists("key","f1");
System.out.println("此处预期 result = ture, 实际 result = " + result);
result = jedis.hexists("key","f100");
System.out.println("此处预期 result = false, 实际 result = " + result);
}