- public class RedisDemoString {
- public static void main(String[] args) {
- JedisPool jedisPool = new JedisPool("tcp://127.0.0.1:8888");
- try (Jedis jedis = jedisPool.getResource()) {
- test1(jedis);
- }
- }
-
- public static void test1(Jedis jedis) {
- jedis.flushAll();
-
- jedis.mset("key1", "value1", "key2", "value2");
-
- List
values = jedis.mget("key1", "key2","key111"); - System.out.println(values);
- }
- }
此处多个key得到的value的顺序都是和key匹配的,如果某个key在redis上不存在,对应的value使用null表示
- private static void test2(Jedis jedis) {
- jedis.flushAll();
-
- jedis.set("key", "abcdefghijkl");
- String str1 = jedis.getrange("key",2,5);
- System.out.println(str1);
-
- jedis.setrange("key",2,"xyz");
- String str2 = jedis.get("key");
- System.out.println(str2);
-
- }
- private static void test3(Jedis jedis) {
- jedis.flushAll();
-
- jedis.set("key", "bar");
- jedis.append("key", "baz");
-
- System.out.println(jedis.get("key"));
- }
- private static void test4(Jedis jedis) {
- jedis.flushAll();
-
- jedis.set("foo", "bar");
- System.out.println(jedis.incr("key"));
- System.out.println(jedis.get("key"));
- }
- private static void test4(Jedis jedis) {
- jedis.flushAll();
-
- jedis.set("foo", "bar");
- System.out.println(jedis.incr("key"));
- System.out.println(jedis.get("key"));
-
- jedis.decr("key");
- System.out.println(jedis.get("key"));
- }
- private static void test1(Jedis jedis) {
- jedis.flushAll();
-
- jedis.lpush("key","111","222","333","444","555","666");
-
- System.out.println(jedis.lrange("key", 0, -1));
- }
- private static void test2(Jedis jedis) {
- jedis.flushAll();
-
- jedis.rpush("key","111","222","333","444","555","666");
- System.out.println(jedis.lrange("key", 0, -1));
-
- }
- private static void test3(Jedis jedis) {
- jedis.flushAll();
-
- jedis.rpush("key","111","222","333","444","555","666");
- jedis.rpop("key");
- System.out.println(jedis.lrange("key", 0, -1));
-
- System.out.println(jedis.lpop("key"));
- }
- private static void test4(Jedis jedis) {
- jedis.flushAll();
-
- List
str1 = jedis.blpop(5,"key"); - System.out.println(str1);
- //返回的结果是一个二元组,一个是从哪个key对应的list中删除的,一个是删除的元素是什么
- jedis.lpush("key","111","222","333","444","555","666");
-
- }
- private static void test5(Jedis jedis) {
- jedis.flushAll();
-
- jedis.rpush("key","111","222","333","444","555","666");
- System.out.println(jedis.llen("key"));
- }
- public static void test1(Jedis jedis) {
- jedis.flushAll();
-
- jedis.sadd("key","111","222","333","444","555","666");
- System.out.println(jedis.smembers("key"));
- }
- public static void test2(Jedis jedis) {
- jedis.flushAll();
-
- jedis.sadd("key","111","222","333","444","555","666");
- System.out.println(jedis.sismember("key", "111"));
- }
- public static void test3(Jedis jedis) {
- jedis.flushAll();
-
- jedis.sadd("key","111","222","333","444","555","666");
- System.out.println(jedis.scard("key"));
- }
- public static void test4(Jedis jedis) {
-
- jedis.flushAll();
-
- jedis.sadd("key","111","222","333","444","555","666");
- System.out.println(jedis.spop("key"));
- System.out.println(jedis.spop("key"));
- System.out.println(jedis.spop("key"));
- System.out.println(jedis.spop("key"));
- System.out.println(jedis.spop("key"));
- System.out.println(jedis.spop("key"));
- }
- public static void test5(Jedis jedis) {
- jedis.flushAll();
-
- jedis.sadd("key","111","222","333","444","555","666");
- jedis.sadd("key1","111","222","444");
-
- System.out.println(jedis.sinter("key", "key1"));
- }
- static void test6(Jedis jedis) {
- jedis.flushAll();
-
- jedis.sadd("key","111","222","333","444","555","666");
- jedis.sadd("key1","111","222","444");
-
- System.out.println(jedis.sinterstore("key3", "key", "key1"));
- System.out.println(jedis.smembers("key3"));
- }
- public static void test1(Jedis jedis) {
- jedis.flushAll();
-
- jedis.hset("key", "f1", "111");
- Map
fields = new HashMap<>(); -
- fields.put("f1", "111");
- fields.put("f2", "222");
-
- jedis.hmset("key", fields);
- System.out.println(jedis.hgetAll("key"));
- System.out.println(jedis.hdel("key", "f2"));
- System.out.println(jedis.hget("key","f100"));
- }
- public static void test2(Jedis jedis) {
- jedis.flushAll();
-
- jedis.hset("key", "f1", "111");
- jedis.hset("key", "f2", "22");
- jedis.hset("key", "f3", "333");
-
- System.out.println(jedis.exists("key"));
- System.out.println(jedis.exists("key","f100"));
- }
- public static void test3(Jedis jedis) {
- jedis.flushAll();
-
- jedis.hset("key", "f1", "111");
- jedis.hset("key", "f2", "22");
- jedis.hset("key", "f3", "333");
-
- System.out.println(jedis.hdel("key", "f1"));
- System.out.println(jedis.hdel("key", "f2"));
- System.out.println(jedis.hgetAll("key"));
- }
- public static void test4(Jedis jedis) {
- jedis.flushAll();
-
- jedis.hset("key", "f1", "111");
- jedis.hset("key", "f2", "22");
- jedis.hset("key", "f3", "333");
-
-
- System.out.println(jedis.hkeys("key"));
- System.out.println(jedis.hvals("key"));
- }
- public static void test4(Jedis jedis) {
- jedis.flushAll();
-
- Map
map = new HashMap<>(); - map.put("f1", "111");
- map.put("f2", "1222");
- map.put("f3", "113");
-
- jedis.hmset("key", map);
-
- List
keys = jedis.hmget("key","f1","f2","f3"); - System.out.println(keys);
- }
- public static void test1(Jedis jedis) {
- jedis.flushAll();
-
- jedis.zadd("key",11,"zahngsan");
- Map
zset = new HashMap<>(); -
- zset.put("ke22y", 20.0);
- zset.put("k232342ey", 204.0);
- zset.put("ke42432y", 220.0);
- jedis.zadd("key",zset);
-
- System.out.println(jedis.zrange("key", 0, -1));
- System.out.println(jedis.zrangeWithScores("key", 0, -1));
-
- }
- public static void test1(Jedis jedis) {
- jedis.flushAll();
-
- jedis.zadd("key",11,"zahngsan");
- Map
zset = new HashMap<>(); -
- zset.put("ke22y", 20.0);
- zset.put("k232342ey", 204.0);
- zset.put("ke42432y", 220.0);
- jedis.zadd("key",zset);
- List
membersWithScore = jedis.zrangeWithScores("key", 0, -1); -
- System.out.println(jedis.zrange("key", 0, -1));
- System.out.println(jedis.zrangeWithScores("key", 0, -1));
- String str1 = membersWithScore.get(0).getElement();
- Double db2 = membersWithScore.get(0).getScore();
- }
- public 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: " + len);
- }
- public 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", "lisi");
- System.out.println("n: " + n);
-
- List
result = jedis.zrangeWithScores("key", 0, -1); - System.out.println("result: " + result);
- }
- public 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", "zhangsan2");
- System.out.println("score: " + score);
- }
- public 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", "zhangsan2");
- System.out.println("rank: " + rank);
- }
Jedis这个库的本质就是针对Redis的各种命令进行了封装,调用某个方法,就相当于在Redis客户端敲下了对应的命令