0. 相关包:
- jedis-2.8.1.jar
- commons-pool2-2.4.2.jar
- 下载链接
1. String 类型的操作
- 代码
package com.hjf.redis; import redis.clients.jedis.Jedis; import java.util.Iterator; import java.util.List; import java.util.Set; /** * @author Jiang锋时刻 * @create 2020-07-26 14:16 */ public class TestJedisByString { public static void main(String[] args) { // 连接Redis服务 Jedis jedis = new Jedis("node01", 6379); //测试Jedis连通性 String ping = jedis.ping(); System.out.println("测试: " + ping); // 等价于: set k1 333 jedis.set("k1", "333"); // 等价于: setex K1 V1 20 // 等价于: set k1 v1 ex 20 jedis.setex("k1", 20, "v1"); // 相当于: set k4 abc nx jedis.setnx("k1", "abc"); // 获取k1的值 // 相当于: get k1 String v1 = jedis.get("k1"); System.out.println("k1的值为: " + v1); // 查询key是否存在 System.out.println(jedis.exists("k1")); System.out.println(jedis.exists("key1")); // 相当于: strlen k1 Long len1 = jedis.strlen("k1"); System.out.println(len1); // 获取redis中所有的key // 方式1: System.out.println("redis中的所有keys: "); Set<String> keys = jedis.keys("*"); // 遍历 for (String key: keys) { System.out.println(key); } // 方式2 Set<String> keys = jedis.keys("key*"); Iterator<String> iterator = keys.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } // 相当于: flushall jedis.flushAll(); // 相当于: flushdb jedis.flushDB(); // 相当于: type k1 String type = jedis.type("k1"); System.out.println(type); // 相当于: append k1 abc jedis.append("k1", "abc"); String k1 = jedis.get("k1"); System.out.println(k1); // 相当于: getrange k1 2 5 System.out.println(jedis.getrange("k1", 2, 5)); // 相当于: setrange k1 3 wwww jedis.setrange("k1", 3, "wwww"); System.out.println(jedis.getrange("k1", 0, -1)); // 相当于: mset key1 value1 key2 value2 key3 value3 jedis.mset("key1", "value1", "key2", "value2", "key3", "value3"); // 相当于: mget key1 key2 key3 List<String> list = jedis.mget("key1", "key2", "key3"); System.out.println(list); // 遍历2 Iterator<String> iterator = list.iterator(); while (iterator.hasNext()) { System.out.println(iterator.next()); } // 关闭redis jedis.close(); } }
2. List
-
代码
package com.hjf.redis; import redis.clients.jedis.BinaryClient; import redis.clients.jedis.Jedis; /** * @author Jiang锋时刻 * @create 2020-07-26 17:31 */ public class TestJedisForList { public static void main(String[] args) { Jedis jedis = new Jedis("node01", 6379); // 等价于: lpush l1 a b jedis.lpush("l1", "a", "b"); // 等价于: rpush l1 c d jedis.rpush("l1", "c", "d"); // 等价于: lrange l1 0 -1 System.out.println(jedis.lrange("l1", 0, -1)); // 等价于: lpop l1 jedis.lpop("l1"); System.out.println(jedis.lrange("l1", 0, -1)); // 等价于: lindex l1 1 System.out.println(jedis.lindex("l1", 1)); // 相当于: linsert l1 before c m // 在value等c之前插入m jedis.linsert("l1", BinaryClient.LIST_POSITION.BEFORE, "c", "m"); // 相当于: linsert l1 after c n // 在value等c之后插入n jedis.linsert("l1", BinaryClient.LIST_POSITION.AFTER, "c", "n"); System.out.println(jedis.lrange("l1", 0, -1)); // 相当于: lrem l1 1 m // 从左向右删除1个m jedis.lrem("l1", 1, "m"); // 相当于: lrem l1 -1 m // 从右向左删除1个n jedis.lrem("l1", -1, "n" ); System.out.println(jedis.lrange("l1", 0, -1)); jedis.close(); } }
3. Set
-
代码
package com.hjf.redis; import redis.clients.jedis.Jedis; public class TestJedisForSet { public static void main(String[] args) { Jedis jedis = new Jedis("node01", 6379); // 相当于: sadd s1 a b c jedis.sadd("s1", "a", "b", "c"); // 相当于: smembers s1 System.out.println(jedis.smembers("s1")); jedis.sadd("s1", "d", "e"); System.out.println(jedis.smembers("s1")); // 相当于: sismember s1 a System.out.println(jedis.sismember("s1", "a")); System.out.println(jedis.sismember("s1", "E")); // 相当于: srandmember s1 System.out.println(jedis.srandmember("s1")); // 相当于: srandmember s1 3 System.out.println(jedis.srandmember("s1", 3)); // 相当于: scard s1 System.out.println(jedis.scard("s1")); // 相当于: srem s1 e jedis.srem("s1", "e"); System.out.println(jedis.smembers("s1")); // 相当于: spop s1 jedis.spop("s1"); System.out.println(jedis.smembers("s1")); jedis.sadd("s1", "a", "b", "c", "d"); jedis.sadd("s2", "c", "d", "e", "f"); System.out.println("s1: " + jedis.smembers("s1")); System.out.println("s2: " + jedis.smembers("s2")); // 相当于: sdiff s1 s2 System.out.println("s1 和 s2 的差集: " + jedis.sdiff("s1", "s2")); // 相当于: sinter s1 s2 System.out.println("s1 和 s2 的交集: " + jedis.sinter("s1", "s2")); // 相当于: sunion s1 s2 System.out.println("s1 和 s2 的并集: " + jedis.sunion("s1", "s2")); } }
4. Zset
-
代码
package com.hjf.redis; import redis.clients.jedis.Jedis; /** * @author Jiang锋时刻 * @create 2020-07-26 21:05 */ public class TestJedisForZset { public static void main(String[] args) { Jedis jedis = new Jedis("node01", 6379); // 相当于: zadd z1 1 A jedis.zadd("z1", 1, "A"); jedis.zadd("z1", 3, "C"); jedis.zadd("z1", 2, "B"); // 相当于: zrange z1 0 -1 System.out.println(jedis.zrange("z1", 0, -1)); // 相当于: zrangebyscore z1 0 2 // 不能使用: zrangebyscore z1 0 -1, 读取不到数据 System.out.println(jedis.zrangeByScore("z1", 0, 2)); // 相当于: zrevrangebyscore z1 3 0 System.out.println(jedis.zrevrangeByScore("z1", 3, 0)); // 相当于: zincrby z1 2 B jedis.zincrby("z1", 2, "B"); System.out.println(jedis.zrange("z1", 0, -1)); // 相当于: zrem z1 B jedis.zrem("z1", "B"); System.out.println(jedis.zrange("z1", 0, -1)); // 相当于: zcount z1 0 3 // 不能使用: zcount z1 0 -1, 读取不到数据 System.out.println(jedis.zcount("z1", 0, 3)); // 相当于: zrank z1 B System.out.println(jedis.zrank("z1", "B")); System.out.println(jedis.zrank("z1", "Z")); jedis.close(); } }
5. Hash
-
代码
package com.hjf.redis; import redis.clients.jedis.Jedis; import java.util.HashMap; /** * @author Jiang锋时刻 * @create 2020-07-26 21:39 */ public class TestJedisForHash { public static void main(String[] args) { Jedis jedis = new Jedis("node01", 6379); // 相当于: hset h1 name 张三 jedis.hset("h1", "name", "张三"); jedis.hset("h1", "age", "24"); jedis.hset("h1", "score", "88"); // 相当于: hget h1 name System.out.println(jedis.hget("h1", "name")); // 相当于: hmset h2 name 李四 age 19 score 98 HashMap<String, String> hashMap = new HashMap<>(); hashMap.put("name", "李四"); hashMap.put("age", "19"); hashMap.put("score", "98"); jedis.hmset("h2", hashMap); // 相当于: hmget h2 name age score System.out.println(jedis.hmget("h2", "name", "age", "score")); // 相当于: hgetall h2 System.out.println(jedis.hgetAll("h2")); // 相当于: hexists h2 name System.out.println(jedis.hexists("h2", "name")); System.out.println(jedis.hexists("h2", "country")); // 相当于: hkeys h2 System.out.println(jedis.hkeys("h2")); // 相当于: hvals h2 System.out.println(jedis.hvals("h2")); } }