上期我们学习了如何使用Java连接到redis,这期我们来学习如何在java中使用redis中的一些命令
1. set/get
可以看到jedis类中提供了很多set方法
public static void test1(Jedis jedis) {jedis.flushAll();jedis.set("key1", "v1");jedis.set("key2", "v2");jedis.set("key3", "v3");System.out.println(jedis.get("key2"));}
调用结果:
可以看到 成功输出了v2
我们也可以使用
String set(String var1, String var2, SetParams var3);
来使用带参数的set命令,我们需要先创建一个SetParams对象,这个对象中有一些参数相关的方法:
public static void test1(Jedis jedis) {jedis.flushAll();jedis.set("key1", "v1");jedis.set("key2", "v2");jedis.set("key3", "v3");SetParams params = new SetParams();params.ex(10);//设置超时时间params.xx();//设置key存在才能创建jedis.set("key2", "666", params);System.out.println(jedis.get("key2"));System.out.println(jedis.ttl("key2"));}
2. exists / del
public static void test2(Jedis jedis) {jedis.flushAll();jedis.set("key1", "v1");jedis.set("key2", "v2");jedis.set("key3", "v3");System.out.println(jedis.exists("key3"));jedis.del("key3");System.out.println(jedis.exists("key3"));}
这里的del方法是支持变长参数的可以在参数列表中写多个参数来一次删除多个
3. keys
public static void test3(Jedis jedis) {jedis.flushAll();jedis.set("key1", "v1");jedis.set("key2", "v2");jedis.set("key3", "v3");Set<String> set = jedis.keys("*");System.out.println(set);}
4. expire / pexire / type
public static void test4(Jedis jedis) throws InterruptedException {jedis.flushAll();jedis.set("key1", "v1");jedis.set("key2", "123");jedis.set("key3", "v3");jedis.expire("key1", 5);jedis.pexpire("key2", 3000);sleep(2000);System.out.println(jedis.ttl("key1"));System.out.println(jedis.ttl("key2"));System.out.println(jedis.ttl("key3"));System.out.println("key1 type: " + jedis.type("key1"));}
5. String 类型命令
5.1 megt / mset
public static void string1(Jedis jedis) throws InterruptedException {jedis.flushAll();jedis.mset("key1", "v1", "key2", "v2", "key3", "v3");List<String> list = jedis.mget("key1", "key666", "key2", "key3");System.out.println(list);}
5.2 getrange /setrange
public static void string2(Jedis jedis) throws InterruptedException {jedis.flushAll();jedis.set("key1", "012345678");System.out.println(jedis.getrange("key1", 0, -1));System.out.println(jedis.getrange("key1", 2, 6));jedis.setrange("key1", 2, "abx");System.out.println(jedis.get("key1"));}
5.3 append / incr /incrby / decr /decrby
public static void string3(Jedis jedis) throws InterruptedException {jedis.flushAll();jedis.set("key1", "012345678");System.out.println(jedis.append("key1", "abc"));System.out.println(jedis.get("key1"));jedis.set("key2", "100");System.out.println(jedis.incr("key2"));System.out.println(jedis.incrBy("key2", 12));System.out.println(jedis.decrBy("key2", 12));System.out.println(jedis.decr("key2"));}
6. list命令
6.1 lpush / rpush / lrange
public static void list1(Jedis jedis) {jedis.flushAll();jedis.lpush("list1", "111", "222", "333");jedis.rpush("list1", "000");System.out.println(jedis.lrange("list1", 0, -1));}
6.2 lpop /rpop
public static void list2(Jedis jedis) {jedis.flushAll();jedis.lpush("list1", "111", "222", "333");jedis.rpush("list1", "000");System.out.println(jedis.lrange("list1", 0, -1));System.out.println(jedis.lpop("list1"));System.out.println(jedis.lrange("list1", 0, -1));System.out.println(jedis.rpop("list1"));System.out.println(jedis.lrange("list1", 0, -1));}
6.3 blpop / brpop
public static void list3(Jedis jedis) {jedis.flushAll();jedis.lpush("list1", "111", "222", "333");jedis.rpush("list1", "000");System.out.println(jedis.blpop(3, "list1"));System.out.println(jedis.brpop(3, "list1"));}
6.4 linsert / lindex / lset
public static void list4(Jedis jedis) {jedis.flushAll();jedis.lpush("list1", "111", "222", "333");jedis.rpush("list1", "000");System.out.println(jedis.lrange("list1", 0, -1));System.out.println(jedis.lindex("list1", 2));System.out.println(jedis.lset("list1", 2, "666"));System.out.println(jedis.linsert("list1", ListPosition.BEFORE, "666", "999"));System.out.println(jedis.lrange("list1", 0, -1));}
7. set命令
7.1 sadd / smembers / sismember / scard / spop
public static void set1(Jedis jedis) {jedis.flushAll();jedis.sadd("set1","000", "111", "222", "333");System.out.println(jedis.smembers("set1"));System.out.println(jedis.sismember("set1", "111"));System.out.println(jedis.sismember("set1", "666"));System.out.println(jedis.scard("set1"));System.out.println(jedis.spop("set1", 2));System.out.println(jedis.smembers("set1"));}
7.2 sinter / sinterstore
public static void set2(Jedis jedis) {jedis.flushAll();jedis.sadd("set1","000", "111", "222", "333");jedis.sadd("set2","000", "111", "55", "666");System.out.println(jedis.sinter("set1", "set2"));System.out.println(jedis.sinterstore("set3", "set1", "set2"));System.out.println(jedis.smembers("set3"));}
8. hash类型命令
8.1 hset / hmset / hget / hmget / hdel
public static void hash1(Jedis jedis) {jedis.flushAll();jedis.hset("hash1", "filed1", "v1");Map<String, String> map = new HashMap<>();map.put("filed2", "v2");map.put("filed3", "v3");map.put("filed4", "v4");jedis.hmset("hash1", map);System.out.println(jedis.hmget("hash1", "filed1", "filed2", "filed3", "filed4"));System.out.println(jedis.hdel("hash1", "filed", "filed1"));System.out.println(jedis.hmget("hash1", "filed1", "filed2", "filed3", "filed4"));}
8.2 hkeys / hvals / hgetall / hlen
public static void hash2(Jedis jedis) {jedis.flushAll();jedis.hset("hash1", "filed1", "v1");Map<String, String> map = new HashMap<>();map.put("filed2", "v2");map.put("filed3", "v3");map.put("filed4", "v4");jedis.hmset("hash1", map);System.out.println(jedis.hlen("hash1"));System.out.println(jedis.hkeys("hash1"));System.out.println(jedis.hvals("hash1"));System.out.println(jedis.hgetAll("hash1"));}
8.3 hincrby / hincrbyfloat
public static void hash3(Jedis jedis) {jedis.flushAll();Map<String, String> map = new HashMap<>();map.put("filed2", "12");map.put("filed3", "10");map.put("filed4", "er");jedis.hmset("hash1", map);System.out.println(jedis.hincrBy("hash1", "filed2", 23));System.out.println(jedis.hincrByFloat("hash1", "filed3", -12.5));System.out.println(jedis.hgetAll("hash1"));
// System.out.println(jedis.hincrBy("hash1", "filed4", 1)); // 报错}
9. zset命令
9.1 zadd / zrange / zrevrange
public static void zset1(Jedis jedis) {jedis.flushAll();jedis.zadd("zset1", 9, "a");Map<String, Double> map = new HashMap<>();map.put("b", 8D);map.put("c", 7D);map.put("d", 6D);jedis.zadd("zset1", 9, "a");jedis.zadd("zset1", map);System.out.println(jedis.zrange("zset1", 0, -1));System.out.println(jedis.zrangeWithScores("zset1", 0, -1));System.out.println(jedis.zrevrange("zset1", 0, -1));System.out.println(jedis.zrevrangeWithScores("zset1", 0, -1));}
9.2 zcard / zcount / zrangebyscore
public static void zset2(Jedis jedis) {jedis.flushAll();jedis.zadd("zset1", 9, "a");Map<String, Double> map = new HashMap<>();map.put("b", 8D);map.put("c", 7D);map.put("d", 6D);jedis.zadd("zset1", 9, "a");jedis.zadd("zset1", map);System.out.println(jedis.zcard("zset1"));System.out.println(jedis.zcount("zset1", 6, 8));System.out.println(jedis.zrangeByScore("zset1", 6, 8));System.out.println(jedis.zrangeByScoreWithScores("zset1", 6, 8));}
9.3 zpopmax / zpopmin / bzpopmax / bzpopmin
public static void zset3(Jedis jedis) {jedis.flushAll();jedis.zadd("zset1", 9, "a");Map<String, Double> map = new HashMap<>();map.put("b", 8D);map.put("c", 7D);map.put("d", 6D);jedis.zadd("zset1", 9, "a");jedis.zadd("zset1", map);System.out.println(jedis.zrangeWithScores("zset1", 0, -1));System.out.println(jedis.zpopmax("zset1", 2));System.out.println(jedis.bzpopmin(1000, "zset1"));System.out.println(jedis.zrangeWithScores("zset1", 0, -1));}
9.4 zrank / zrevrank / zscore / zrem
public static void zset4(Jedis jedis) {jedis.flushAll();jedis.zadd("zset1", 9, "a");Map<String, Double> map = new HashMap<>();map.put("b", 8D);map.put("c", 7D);map.put("d", 6D);jedis.zadd("zset1", 9, "a");jedis.zadd("zset1", map);System.out.println(jedis.zrank("zset1", "a"));System.out.println(jedis.zrevrank("zset1", "a"));System.out.println(jedis.zscore("zset1", "a"));System.out.println(jedis.zrem("zset1", "a", "b"));System.out.println(jedis.zrangeWithScores("zset1", 0, -1));}
9.5 zinterstore / zunionstore
public static void zset5(Jedis jedis) {jedis.flushAll();Map<String, Double> map = new HashMap<>();map.put("a", 9D);map.put("b", 8D);map.put("c", 7D);map.put("d", 6D);jedis.zadd("zset1", map);Map<String, Double> map2 = new HashMap<>();map2.put("a", 99D);map2.put("b", 0D);map2.put("e", 7D);map2.put("f", 6D);jedis.zadd("zset2", map2);//求交集保留大的score, key1权重为1, key2权重为2System.out.println(jedis.zinterstore("zset3", new ZParams().weights(1, 2).aggregate(ZParams.Aggregate.MAX), "zset1", "zset2"));//求交集保留大的score, key1权重为1, key2权重为2System.out.println(jedis.zunionstore("zset4", new ZParams().weights(1, 2).aggregate(ZParams.Aggregate.MAX), "zset1", "zset2"));System.out.println(jedis.zrangeWithScores("zset3", 0, -1));System.out.println(jedis.zrangeWithScores("zset4", 0, -1));}