目录
[SET & GET](#SET & GET)
[EXISTS & DEL](#EXISTS & DEL)
[EXPIRE & TTL](#EXPIRE & TTL)
[String 类型命令](#String 类型命令)
[MGET & MSET](#MGET & MSET)
[GETRANGE & SETRANGE](#GETRANGE & SETRANGE)
[INCR & DECR](#INCR & DECR)
[List 类型命令](#List 类型命令)
[LPUSH & LRANG](#LPUSH & LRANG)
[LPOP & LPOP](#LPOP & LPOP)
[BLPOP & BRPOP](#BLPOP & BRPOP)
[Set 类型命令](#Set 类型命令)
[SADD & SMEMBERS](#SADD & SMEMBERS)
[Hash 类型命令](#Hash 类型命令)
[HSET & HGET](#HSET & HGET)
[HKEYS & HVALS](#HKEYS & HVALS)
[HMGET & HMSET](#HMGET & HMSET)
[ZSet 类型命令](#ZSet 类型命令)
[ZADD & ZRANGE](#ZADD & ZRANGE)
引言
- Jedis 库的本质就是针对 Redis 的各种命令进行了封装
- 调用某个方法就相当于在 Redis 客户端中敲下了对应的命令
- 下文将介绍 Redis 命名在代码中的操作,但不会覆盖到所有的命令 ,只会挑一些比较重要的、代表性的命令
通用命令
SET & GET
javapublic static void test1(Jedis jedis) { System.out.println("get 和 set 的使用"); // 先清空数据库,要避免上一组测试的残留数据影响到下一组测试的结果 jedis.flushAll(); jedis.set("key","111"); jedis.set("key2","222"); // 通过创建 SetParams 的对象,给 set 命令设置参数和选项 SetParams params = new SetParams(); params.ex(10); // 此处设置 XX 选项 // 表示只有当 key 存在时才设置,key 不存在时直接返回 nil params.xx(); // 注意此时 set 的 key 在上述代码中已经设置过了 jedis.set("key","333",params); SetParams params2 = new SetParams(); params2.ex(10); params2.xx(); // 注意此时 set 的 key4 并未在上述代码中设置过 jedis.set("key4","444",params2); String value = jedis.get("key"); System.out.println("此处预期 value = 333, 实际 value = " + value); String value4 = jedis.get("key4"); System.out.println("此处预期 value4 = null, 实际 value4 = " + value4); }
运行结果:
EXISTS & DEL
javapublic static void test2(Jedis jedis) { System.out.println("exists 和 del 的使用"); jedis.flushAll(); jedis.set("key","111"); jedis.set("key2","222"); jedis.set("key3","333"); // 判断 key 是否存在 boolean result = jedis.exists("key"); System.out.println("此处预期 result = ture, 实际 result = " + result); // 在此处直接将 key 删除 long result2 = jedis.del("key"); System.out.println("此处预期 result2 = 1, 实际 result2 = " + result2); // 重新判断 key 是否存在 result = jedis.exists("key"); System.out.println("此处预期 result = false, 实际 result = " + result); // 使用 del 方法删除多个 key (变长参数) result2 = jedis.del("key2","key3"); System.out.println("此处预期 result2 = 2, 实际 result2 = " + result2); }
运行结果:
KEYS
javapublic static void test3(Jedis jedis) { System.out.println("keys 的使用"); jedis.flushAll(); jedis.set("key","111"); jedis.set("key2","222"); jedis.set("key3","333"); jedis.set("key4","444"); // 使用 keys 方法直接获取到 redis 中所有键 Set<String> keys = jedis.keys("*"); System.out.println(keys); }
运行结果:
EXPIRE & TTL
javapublic static void test4(Jedis jedis) throws InterruptedException { System.out.println("expire 和 ttl 的使用"); jedis.flushAll(); jedis.set("key","111"); // 使用 expire 方法给 key 设置过期时间 10秒 jedis.expire("key",10); // 休眠 3秒,再继续执行代码 Thread.sleep(3000); // 使用 ttl 方法获取到 key 的过期时间 long time = jedis.ttl("key"); System.out.println("此处预期 time = 7, 实际 time = " + time); }
运行结果:
TYPE
javapublic static void test5(Jedis jedis) throws InterruptedException { System.out.println("type 的使用"); jedis.flushAll(); jedis.set("key","111"); String type = jedis.type("key"); System.out.println("此处预期 type = sting , 实际 type = " + type); jedis.lpush("key2","111","222","333"); String type2 = jedis.type("key2"); System.out.println("此处预期 type2 = list , 实际 type2 = " + type2); jedis.hset("key3","f1","111"); String type3 = jedis.type("key3"); System.out.println("此处预期 type3 = hash , 实际 type3 = " + type3); jedis.sadd("key4","111","222","333"); String type4 = jedis.type("key4"); System.out.println("此处预期 type4 = set , 实际 type4 = " + type4); jedis.zadd("key5",10,"zhangsan"); String type5 = jedis.type("key5"); System.out.println("此处预期 type5 = zset , 实际 type5 = " + type5); }
运行结果:
String 类型命令
MGET & MSET
javapublic static void test1(Jedis jedis) { System.out.println("mget 和 mset 的使用"); jedis.flushAll(); jedis.mset("key1","111","key2","222","key3","333"); // 此处多个 key 得到的 value 的顺序都是和 key 匹配的 // 如果某个 key 在 redis 上不存在,对应的 value 使用 null 表示 List<String> values = jedis.mget("key1","key2","key100","key3"); System.out.println("values: " + values); }
运行结果:
GETRANGE & SETRANGE
javapublic 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
javapublic 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
javapublic 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
javapublic 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
javaimport 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); } } }
运行结果:
BLPOP & BRPOP
javaimport redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import java.util.List; public class RedisDemoList { public static void test3(Jedis jedis) { System.out.println("blpop 的使用"); jedis.flushAll(); // 返回结果是一个 "二元组" 有个是从哪个 key 对应的 list 中删除的,一个删除的元素是什么 List<String> results = jedis.blpop(100,"key"); System.out.println("results.get(0) = " + results.get(0)); System.out.println("results.get(1) = " + results.get(1)); } public static void main(String[] args) { JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888"); try (Jedis jedis = jedisPool.getResource()) { test3(jedis); } } }
运行结果:
LLEN
javaimport redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; public class RedisDemoList { public static void test4(Jedis jedis) { System.out.println("llen 的使用"); jedis.flushAll(); jedis.rpush("key","111","222","333"); long len = jedis.llen("key"); System.out.println("此处预期 len = 3, 实际 len = " + len); } public static void main(String[] args) { JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888"); try (Jedis jedis = jedisPool.getResource()) { // test3(jedis); test4(jedis); } } }
运行结果:
Set 类型命令
SADD & SMEMBERS
javapublic static void test1(Jedis jedis) { System.out.println("sadd 和 smembers 的使用"); jedis.flushAll(); jedis.sadd("key","111","222","333"); Set<String> result = jedis.smembers("key"); System.out.println("result: " + result); }
运行结果:
SISMEMBERS
javapublic 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
javapublic 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
javapublic 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
javapublic 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
javapublic 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
javapublic 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
javapublic 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); }
运行结果:
HDEL
javapublic static void test3(Jedis jedis) { System.out.println("hdel 的使用"); jedis.flushAll(); jedis.hset("key","f1","111"); jedis.hset("key","f2","222"); jedis.hset("key","f3","333"); long result = jedis.hdel("key","f1","f2"); System.out.println("此处预期 result = 1, 实际 result = " + result); boolean exists = jedis.hexists("key","f1"); System.out.println("此处预期 exists = false, 实际 exists = " + exists); exists = jedis.hexists("key","f2"); System.out.println("此处预期 exists = false, 实际 exists = " + exists); }
运行结果:
HKEYS & HVALS
javapublic static void test4(Jedis jedis) { System.out.println("hkeys 和 hvals 的使用"); jedis.flushAll(); jedis.hset("key","f1","111"); jedis.hset("key","f2","111"); jedis.hset("key","f3","333"); Set<String> fields = jedis.hkeys("key"); System.out.println("fields = " + fields); List<String> vals = jedis.hvals("key"); System.out.println("vals = " + vals); }
运行结果:
HMGET & HMSET
javapublic static void test5(Jedis jedis) { System.out.println("hmget 和 hmset 的使用"); jedis.flushAll(); HashMap<String,String> map = new HashMap<>(); map.put("f1","111"); map.put("f2","222"); map.put("f3","333"); jedis.hmset("key",map); List<String> vals = jedis.hmget("key","f3","f1","f2"); // 此时返回元素的顺序一定为 我们自己所指定 field 的先后顺序 System.out.println("此处预期 vals = [333,111,222], 实际 vals = " + vals); }
运行结果:
ZSet 类型命令
ZADD & ZRANGE
javapublic static void test1(Jedis jedis) { System.out.println("zadd 和 zrange 的使用"); jedis.flushAll(); jedis.zadd("key",10,"zhangsan"); Map<String,Double> map = new HashMap<>(); map.put("lisi",20.0); map.put("wangwu",30.0); jedis.zadd("key",map); List<String> members = jedis.zrange("key",0,-1); System.out.println("members = " + members); List<Tuple> membersWithScore = jedis.zrangeWithScores("key",0,-1); System.out.println("membersWithScore = " + membersWithScore); String member = membersWithScore.get(0).getElement(); double score = membersWithScore.get(0).getScore(); System.out.println("member = " + member + ", score" + score); }
运行结果:
ZCARD
javapublic static void test2(Jedis jedis) { System.out.println("zcard 的使用"); jedis.flushAll(); jedis.zadd("key",10,"zhangsan"); jedis.zadd("key",20,"lisi"); jedis.zadd("key",30,"wangwu"); long len = jedis.zcard("key"); System.out.println("此处预期 len = 3, 实际 len = " + len); }
运行结果:
ZREM
javapublic static void test3(Jedis jedis) { System.out.println("zrem 的使用"); jedis.flushAll(); jedis.zadd("key",10,"zhangsan"); jedis.zadd("key",20,"lisi"); jedis.zadd("key",30,"wangwu"); long n = jedis.zrem("key","zhangsan"); System.out.println("此处预期 n = 1, 实际 n = " + n); List<Tuple> membersWithScore = jedis.zrangeWithScores("key",0,-1); System.out.println("membersWithScore = " + membersWithScore); }
运行结果:
ZSCORE
javapublic static void test4(Jedis jedis) { System.out.println("zscore 的使用"); jedis.flushAll(); jedis.zadd("key",10,"zhangsan"); jedis.zadd("key",20,"lisi"); jedis.zadd("key",30,"wangwu"); Double score = jedis.zscore("key","zhangsan"); System.out.println("此处预期 score = 10.0, 实际 n = " + score); score = jedis.zscore("key","zhaoliu"); System.out.println("此处预期 score = null, 实际 n = " + score); }
运行结果:
ZRANK
javapublic static void test5(Jedis jedis) { System.out.println("zrank 的使用"); jedis.flushAll(); jedis.zadd("key",10,"zhangsan"); jedis.zadd("key",20,"lisi"); jedis.zadd("key",30,"wangwu"); Long rank = jedis.zrank("key","zhangsan"); System.out.println("此处预期 score = 1, 实际 n = " + rank); rank = jedis.zrank("key","zhaoliu"); System.out.println("此处预期 score = null, 实际 n = " + rank); }
运行结果: