Java架構之Redis系列:通過文章點贊排名案例學習Sortedset命令
前言
這一篇文章將講述Redis中的sortedset類型命令,同樣也是通過demo來講述,其他部分這里就不在贅述了。
案例
demo功能是文章點贊排名等等,整個demo的大致頁面如下。

準備工作
首先定義一個存儲文章的key
- private static final String ZSET_KEY = "articleList";
redis操作對象
- private RedisTemplate redisTemplate;
- //string 命令操作對象
- private ValueOperations valueOperations;
- //zset 命令操作對象
- private ZSetOperations zSetOperations;
sortedset在Redis中的結構可以看下圖(圖片來源于Redis in Action)。

列表查詢
- @RequestMapping(value = "/getList/{sortType}", method = RequestMethod.GET)
- public Set getList(@PathVariable String sortType) {
- //如果沒有數(shù)據(jù),則添加10條數(shù)據(jù)
- if (zSetOperations.size(ZSET_KEY) == 0){
- for (int i = 1; i <= 10; i++) {
- zSetOperations.add(ZSET_KEY,"文章:"+i, (int)(Math.random()*10+i));
- }
- }
- //ASC根據(jù)分數(shù)從小到大排序,DESC反之
- if ("ASC".equals(sortType)){
- return zSetOperations.rangeWithScores(ZSET_KEY, 0, -1);
- } else {
- return zSetOperations.reverseRangeWithScores(ZSET_KEY, 0, -1);
- }
- }
這里為了省去一個個添加數(shù)據(jù)的麻煩,就在獲取列表數(shù)據(jù)中加了個判斷。當文章數(shù)據(jù)為0時,默認添加10條數(shù)據(jù),設置隨機score加上所在的索引。
然后根據(jù)url中的參數(shù)sortType來決定返回的數(shù)據(jù)是按照分數(shù)升序還是降序排序。功能效果如下

命令介紹

贊或踩
java代碼如下
- @RequestMapping(value = "/star", method = RequestMethod.POST)
- public boolean starOrUnStar(String member, String type) {
- if ("UP".equals(type)){
- zSetOperations.incrementScore(ZSET_KEY, member, 1);
- } else {
- zSetOperations.incrementScore(ZSET_KEY, member, -1);
- }
- return true;
- }
根據(jù)type決定是否加減分數(shù),當type為UP時表示贊,為其他(DOWN)時表示踩。功能效果如下

命令介紹

升降序排名
java代碼如下
- @RequestMapping(value = "/rank/{type}/{member}", method = RequestMethod.GET)
- public Long rank(@PathVariable String member, @PathVariable String type) {
- Long rank = null;
- if ("ASC".equals(type)){
- rank = zSetOperations.rank(ZSET_KEY, member);
- } else {
- rank = zSetOperations.reverseRank(ZSET_KEY, member);
- }
- return rank;
- }
根據(jù)type決定是升序排名還是降序排名,如果是ASC則調用rank方法獲取升序排名,其他則調用reverseRank獲取降序排名。與下方redis命令類似
- ZRANK articleList "文章1"
- ZREVRANK articleList "文章1"
頁面效果圖如下

命令介紹

其他命令
獲取屬性

ZCARD命令
返回key的有序集元素個數(shù)。
ZCARD key
返回值:key存在的時候,返回有序集的元素個數(shù),否則返回0。
redis客戶端執(zhí)行的命令如下
- zadd zCardKey 1 one
- zcard zCardKey
下面是java代碼
- @Test
- public void zCard() {
- jedis.zadd("zCardKey",1, "one");
- jedis.zadd("zCardKey",2, "two");
- System.out.println(jedis.zcard("zCardKey"));
- System.out.println(zSetOperations.size("zCardKey"));
- }
ZCOUNT命令
返回有序集key中,score值在min和max之間(默認包括score值等于min或max)的成員數(shù)量。
ZCOUNT key min max
返回值:指定分數(shù)范圍的元素個數(shù)。
redis客戶端執(zhí)行的命令如下
- zadd zCountKey 1 one 2 two 3 three 4 four
- zcount zCountKey 2 3
執(zhí)行結果如下

下面是java代碼
- @Test
- public void zCount() {
- jedis.zadd("zCountKey",1, "one");
- jedis.zadd("zCountKey",2, "two");
- jedis.zadd("zCountKey",3, "three");
- jedis.zadd("zCountKey",4, "four");
- System.out.println(jedis.zcount("zCountKey",2, 3));
- System.out.println(zSetOperations.count("zCountKey",2, 3));
- }
ZLEXCOUNT命令
計算有序集合中指定成員之間的成員數(shù)量(按成員字典正序排序),可以使用 - 和 + 表示score最小值和最大值
ZLEXCOUNT key min max
redis客戶端執(zhí)行的命令如下
- ZADD zLexCountKey 2 "b" 1 "a" 3 "c" 5 "e" 4 "d"
- ZLEXCOUNT zLexCountKey - +
- ZLEXCOUNT zLexCountKey [b [d
執(zhí)行結果如下

下面是java代碼
- @Test
- public void zLexCount() {
- zSetOperations.add("zLexCountKey", "b", 2);
- zSetOperations.add("zLexCountKey", "a", 1);
- zSetOperations.add("zLexCountKey", "c", 3);
- zSetOperations.add("zLexCountKey", "e", 5);
- zSetOperations.add("zLexCountKey", "d", 4);
- System.out.println(jedis.zlexcount("zLexCountKey", "-", "+"));
- System.out.println(jedis.zlexcount("zLexCountKey", "[b", "[d"));
- }
ZSCORE命令
返回有序集key中,成員member的score值。
ZSCORE key member
返回值:成員member的score值
redis客戶端執(zhí)行的命令如下
- zadd zScoreKey 1 one
- ZSCORE zScoreKey one
下面是java代碼
- @Test
- public void zScore() {
- jedis.zadd("zScoreKey",1, "one");
- System.out.println(jedis.zscore("zScoreKey", "one"));
- System.out.println(zSetOperations.score("zScoreKey", "one"));
- }
獲取成員

ZRANGEBYLEX命令
返回指定成員區(qū)間內的成員,按成員字典正序排序。
https://redis.io/commands/zrangebylex
ZRANGEBYLEX key min max [LIMIT offset count]
返回值:指定成員范圍的元素列表。
redis客戶端執(zhí)行的命令如下
- ZADD zRangeByLexKey 0 ba 0 a 0 ab 0 aa 0 b
- ZRANGEBYLEX zRangeByLexKey - +
- ZRANGEBYLEX zRangeByLexKey [aa (ba
執(zhí)行結果如下

下面是java代碼
- @Test
- public void zRangeByLex() {
- zSetOperations.add("zRangeByLexKey", "ba", 0);
- zSetOperations.add("zRangeByLexKey", "a", 0);
- zSetOperations.add("zRangeByLexKey", "ab", 0);
- zSetOperations.add("zRangeByLexKey", "aa", 0);
- zSetOperations.add("zRangeByLexKey", "b", 0);
- System.out.println(jedis.zrangeByLex("zRangeByLexKey", "-", "+"));
- RedisZSetCommands.Range range = new RedisZSetCommands.Range();
- range.gte("aa");
- range.lt("ba");
- System.out.println(zSetOperations.rangeByLex("zRangeByLexKey",range));
- }
ZRANGEBYSCORE命令
獲取score在范圍之內的數(shù)據(jù)。min和max可以是-inf和+inf
ZRANGEBYSCORE key min max [WITHSCORES]``[LIMIT offset count]
redis客戶端執(zhí)行的命令如下
- ZADD zRangeByScoreKey 1 ba 2 a 3 ab 4 aa 5 b
- ZRANGEBYSCORE zRangeByScoreKey -inf +inf
- ZRANGEBYSCORE zRangeByScoreKey 2 4
執(zhí)行結果如下

下面是java代碼
- @Test
- public void zRangeByScore() {
- zSetOperations.add("zRangeByScoreKey", "ba", 1);
- zSetOperations.add("zRangeByScoreKey", "a", 2);
- zSetOperations.add("zRangeByScoreKey", "ab", 3);
- zSetOperations.add("zRangeByScoreKey", "aa", 4);
- zSetOperations.add("zRangeByScoreKey", "b", 5);
- System.out.println(jedis.zrangeByScore("zRangeByScoreKey", "-inf", "+inf"));
- RedisZSetCommands.Range range = new RedisZSetCommands.Range();
- System.out.println(zSetOperations.rangeByScore("zRangeByScoreKey", 2, 4));
- }
移除相關命令

ZREM命令
ZREM key member [member ...]
返回值:有序集合中刪除的成員個數(shù)
redis客戶端執(zhí)行的命令如下
- ZADD zRemKey 1 "one" 2 "two" 3 "three"
- ZREM zRemKey one
- ZRANGE zRemKey 0 -1
執(zhí)行結果如下

下面是java代碼
- @Test
- public void zRem() {
- zSetOperations.add("zRemKey", "one", 1);
- zSetOperations.add("zRemKey", "two", 2);
- zSetOperations.add("zRemKey", "three", 3);
- //jedis.zrem("zRemKey", "one");
- zSetOperations.remove("zRemKey", "one");
- System.out.println(zSetOperations.range("zRemKey", 0 , -1));
- }
交并集

ZINTERSTORE命令
計算給定的numkeys個有序集合的交集,并且把結果放到destination中。
在給定要計算的key和其它參數(shù)之前,必須先給定key個數(shù)(numberkeys)。
默認情況下,結果中一個元素的分數(shù)是有序集合中該元素分數(shù)之和,前提是該元素在這些有序集合中都存在。因為交集要求其成員必須是給定的每個有序集合中的成員,結果集中的每個元素的分數(shù)和輸入的有序集合個數(shù)相等。
對于WEIGHTS和AGGREGATE參數(shù)的描述,參見命令ZUNIONSTORE。
如果destination存在,就把它覆蓋。
ZINTERSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...]] [AGGREGATE SUM|MIN|MAX]
返回值:結果有序集合destination中元素個數(shù)。
redis客戶端執(zhí)行的命令如下
- ZADD zInterStoreKey1 1 "one" 2 "two"
- ZADD zInterStoreKey2 1 "one" 2 "two" 3 "three"
- ZINTERSTORE zInterStoreSumResult 2 zInterStoreKey1 zInterStoreKey2 WEIGHTS 2 3
- ZRANGE zInterStoreSumResult 0 -1 WITHSCORES
執(zhí)行結果如下

下面是java代碼
- @Test
- public void zInterStore() {
- zSetOperations.add("zInterStoreKey1", "one", 1);
- zSetOperations.add("zInterStoreKey1", "two", 2);
- zSetOperations.add("zInterStoreKey2", "one", 1);
- zSetOperations.add("zInterStoreKey2", "two", 2);
- zSetOperations.add("zInterStoreKey2", "three", 3);
- ZParams zParams = new ZParams();
- zParams.weightsByDouble(2, 3);
- zParams.aggregate(ZParams.Aggregate.SUM);
- jedis.zinterstore("zInterStoreSumResult", zParams, "zInterStoreKey1", "zInterStoreKey2");
- printTuple("zInterStoreSumResult", jedis.zrangeWithScores("zInterStoreSumResult", 0, -1));
- }
ZUNIONSTORE命令
計算給定的numkeys個有序集合的并集,并且把結果放到destination中。
WEIGHTS參數(shù)相當于權重,默認就是1,可以給不同的key設置不同的權重
AGGREGATE參數(shù)默認使用的參數(shù)SUM,還可以選擇MIN或者MAX。這個參數(shù)決定結果集的score是取給定集合中的相加值、最小值還是最大值
ZUNIONSTORE destination numkeys key [key ...] [WEIGHTS weight [weight ...] ] [AGGREGATE SUM|MIN|MAX]
redis客戶端執(zhí)行的命令如下
- ZADD zUnionStoreKey1 1 "one" 2 "two"
- ZADD zUnionStoreKey2 1 "one" 2 "two" 3 "three"
- ZUNIONSTORE zUnionStoreSumResult 2 zUnionStoreKey1 zUnionStoreKey2 WEIGHTS 2 3
- ZUNIONSTORE zUnionStoreMinResult 2 zUnionStoreKey1 zUnionStoreKey2 WEIGHTS 2 3 AGGREGATE MIN
- ZUNIONSTORE zUnionStoreMaxResult 2 zUnionStoreKey1 zUnionStoreKey2 WEIGHTS 2 3 AGGREGATE MAX
- *
- ZRANGE zUnionStoreSumResult 0 -1 WITHSCORES
- ZRANGE zUnionStoreMinResult 0 -1 WITHSCORES
- ZRANGE zUnionStoreMaxResult 0 -1 WITHSCORES
執(zhí)行結果如下

下面是java代碼
- @Test
- public void zUnionStore() {
- zSetOperations.add("zUnionStoreKey1", "one", 1);
- zSetOperations.add("zUnionStoreKey1", "two", 2);
- zSetOperations.add("zUnionStoreKey2", "one", 1);
- zSetOperations.add("zUnionStoreKey2", "two", 2);
- zSetOperations.add("zUnionStoreKey2", "three", 3);
- ZParams zParams = new ZParams();
- zParams.weightsByDouble(2, 3);
- zParams.aggregate(ZParams.Aggregate.SUM);
- jedis.zunionstore("zUnionStoreSumResult", zParams, "zUnionStoreKey1", "zUnionStoreKey2");
- //求最小值
- zParams.aggregate(ZParams.Aggregate.MIN);
- jedis.zunionstore("zUnionStoreMinResult", zParams, "zUnionStoreKey1", "zUnionStoreKey2");
- //求最大值
- zParams.aggregate(ZParams.Aggregate.MAX);
- jedis.zunionstore("zUnionStoreMaxResult", zParams, "zUnionStoreKey1", "zUnionStoreKey2");
- //spring
- zSetOperations.unionAndStore("zUnionStoreKey1", "zUnionStoreKey2", "zUnionStoreResult");
- printTuple("zUnionStoreSumResult", jedis.zrangeWithScores("zUnionStoreSumResult", 0, -1));
- printTuple("zUnionStoreMinResult", jedis.zrangeWithScores("zUnionStoreMinResult", 0, -1));
- printTuple("zUnionStoreMaxResult", jedis.zrangeWithScores("zUnionStoreMaxResult", 0, -1));
- printTuple("zUnionStoreResult", jedis.zrangeWithScores("zUnionStoreResult", 0, -1));
- }