当前位置: 首页 > 科技观察

Redis系列之Java架构:通过文章点赞和案例排行学习Sortedset命令_0

时间:2023-03-20 11:37:14 科技观察

案例demo的功能是文章排名等,整个demo的总页面如下。准备工作首先定义一个keyprivatestaticfinalStringZSET_KEY="articleList",用于存放文章;redis操作对象privateRedisTemplateredisTemplate;//string命令操作对象privateValueOperationsvalueOperations;//zset命令操作对象privateZSetOperationszSetOperations;在行动中)。列表查询@RequestMapping(value="/getList/{sortType}",method=RequestMethod.GET)publicSetgetList(@PathVariableStringsortType){//如果没有数据,添加10条数据if(zSetOperations.size(ZSET_KEY)==0){for(inti=1;i<=10;i++){zSetOperations.add(ZSET_KEY,"文章:"+i,(int)(Math.random()*10+i));}}//ASC根据分数从小到大排序,DESC反之if("ASC".equals(sortType)){returnzSetOperations.rangeWithScores(ZSET_KEY,0,-1);}else{returnzSetOperations.reverseRangeWithScores(ZSET_KEY,0,-1);}}这里为了省去逐条添加数据的麻烦,在获取列表数据的时候加入了判断。当文章数据为0时,默认添加10条数据,设置随机分加索引。然后根据url中的参数sortType判断返回的数据是按分数升序还是降序排序。函数效果如下。命令介绍likeordislike如下,member,1);}else{zSetOperations.incrementScore(ZSET_KEY,member,-1);}returntrue;}根据类型判断是否加分或减分,当type为UP时表示喜欢,当为other(DOWN),表示向下。作用及效果如下命令引入排名升序java代码如下@RequestMapping(value="/rank/{type}/{member}",method=RequestMethod.GET)publicLongrank(@PathVariableStringmember,@PathVariableStringtype){Longrank=null;if("ASC".equals(type)){rank=zSetOperations.rank(ZSET_KEY,member);}else{rank=zSetOperations.reverseRank(ZSET_KEY,member);}returnrank;}根据类型决定升序还是降序,如果是ASC,调用rank方法获取升序,其他调用reverseRank获取降序。类似于下面的redis命令ZRANKarticleList"第1条"ZREVRANKarticleList"第1条"页面效果图下面的命令介绍其他获取属性的命令ZCARD命令返回key的有序集合元素的个数。ZCARD键返回值:当key存在时,返回有序集合中元素的个数,否则返回0。redis客户端执行的命令如下zaddzCardKey1onezcardzCardKey下面是java代码@TestpublicvoidzCard(){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的成员数)。ZCOUNTkeyminmax返回值:指定分数范围内的元素个数。redis客户端执行的命令如下:zaddzCountKey1one2two3three4fourzcountzCountKey23执行结果如下:下面是java代码@TestpublicvoidzCount(){jedis.zadd("zCountKey",1,"one");jedis.zadd("zCountKey",2,"二");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命令计算有序集合中指定成员中的成员个数(按照成员字典的正序排序),其中可以用-和+表示score的最小值和最大值ZLEXCOUNTkeyminmaxredis客户端执行的命令如下ZADDzLexCountKey2"b"1"a"3"c"5"e"4"d"ZLEXCOUNTzLexCountKey-+ZLEXCOUNTzLexCountKey-+ZLEXCOUNTzLexCountKey[b[d执行结果如下下面是java代码@TestpublicvoidzLexCount(){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成员的分值ZSCOREkeymember返回值:member成员的分值是redis客户端执行命令如下zaddzScoreKey1oneZSCOREzScoreKeyone下面是java代码@TestpublicvoidzScore(){jedis.zadd("zScoreKey",1,"one");System.out.println(jedis.zscore("zScoreKey",“一个”));系统。out.println(zSetOperations.score("zScoreKey","one"));}获取成员ZRANGEBYLEX命令返回指定成员范围内的成员,按照成员字典的正序排序。https://redis.io/commands/zrangebylexZRANGEBYLEXkeyminmax[LIMIToffsetcount]返回值:指定成员范围的元素列表。redis客户端执行的命令如下:ZADDzRangeByLexKey0ba0a0ab0aa0bZRANGEBYLEXzRangeByLexKey-+ZRANGEBYLEXzRangeByLexKey[aa(ba(ba)执行结果如下下面是java代码@TestpublicvoidzRangeByLex(){zSetOperations.add("zRangeByLexKeyz","SetOperations",0);.add("a",0);zSetOperations.add("zRangeByLexKey","ab",0);zSetOperations.add("zRangeByLexKey","aa",0);zSetOperations.add("zRangeByLexKey","b",0);System.out.println(jedis.zrangeByLex("zRangeByLexKey","-","+"));RedisZSetCommands.Rangerange=newRedisZSetCommands.Range();range.gte("aa");范围。lt("ba");System.out.println(zSetOperations.rangeByLex("zRangeByLexKey",range));}ZRANGEBYSCORE命令获取得分在范围内的数据。min和max可以是-inf和+infZRANGEBYSCOREkeyminmax[WITHSCORES]``[LIMIToffsetcount]redis客户端执行的命令如下:"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.Rangerange=newRedisZSetCommands.Range();System.out.println(zSetOperations.rangeByScore("zRangeByScoreKey",2,4));}移除相关命令ZREM命令ZREMkeymember[member...]返回值:有序集合的个数在redis客户端删除成员执行如下命令:ZADDzRemKey1"one"2"two"3"three"ZREMzRemKeyoneZRANGEzRemKey0-1的执行结果如下。下面是java代码@TestpublicvoidzRem(){zSetOperations.add("zRemKey","one",1);zSetOperations.add("zRemKey","two",2);zSetOperations.add("zRemKey","三",3);//jedis.zrem("zRemKey","one");zSetOperations.remove("zRemKey","one");System.out.println(zSetOperations.range("zRemKey",0,-1));}交集和并集ZINTERSTORE命令计算给定numkeys有序集的交集,并将结果放在目标中。在指定要计算的键和其他参数之前,必须给出键的个数(numberkeys)。默认情况下,结果中元素的分数是排序集中该元素分数的总和,前提是该元素存在于这两个排序集中。因为交集要求其成员必须是每个给定有序集合的成员,所以结果集中每个元素的得分等于输入有序集合的数量。有关WEIGHTS和AGGREGATE参数的说明,请参阅ZUNIONSTORE命令。如果目标存在,则将其覆盖。ZINTERSTOREdestinationnumkeyskey[key...][WEIGHTSweight[weight...]][AGGREGATESUM|MIN|MAX]返回值:结果有序集中目的地的元素个数。redis客户端执行的命令如下:ZADDzInterStoreKey11"one"2"two"ZADDzInterStoreKey21"one"2"two"3"three"ZINTERSTOREzInterStoreSumResult2zInterStoreKey1zInterStoreKey2WEIGHTS23ZRANGEzInterStoreSumResult0-1WITHSCORES执行结果如下java代码@TestpublicvoidsInterStore.“一”,1);zSetOperations.add(“zInterStoreKey1”,“二”,2);zSetOperations.add(“zInterStoreKey2”,“一”,1);zSetOperations.add(“zInterStoreKey2”,“二”,2);zSetOperations.add("zInterStoreKey2","three",3);ZParamszParams=newZParams();zParams.weightsByDouble(2,3);zParams.aggregate(ZParams.Aggregate.SUM);jedis.zinterstore("zInterStoreSumResult",zParams,"zInterStoreKey1","zInterStoreKey2");printTuple("zInterStoreSumResult",jedis.zrangeWithScores("zInterStoreSumResult",0,-1));}ZUNIONSTORE命令计算给出numkeys有序集合的并集,并将结果放在目的地。WEIGHTS参数相当于权重。默认为1。您可以为不同的键设置不同的权重。AGGREGATE参数使用默认参数SUM,您也可以选择MIN或MAX。该参数决定结果集的score取给定集合ZUNIONSTOREdestinationnumkeyskey[key...][WEIGHTSweight[weight...]][AGGREGATESUM|中的相加值、最小值或最大值MIN|MAX]redis客户端执行的命令如下ZADDzUnionStoreKey11"one"2"two"ZADDzUnionStoreKey21"one"2"two"3"three"ZUNIONSTOREzUnionStoreSumResult2zUnionStoreKey1zUnionStoreKey2WEIGHTS23ZUNIONSTOREzUnionStoreMinResult2zUnionStoreKey1zUnionStoreKey2WEIGHTS23AGGREGATEMINZUNIONSTOREzUnionStoreMaxResult2zUnionStoreKey1zUnionStoreKey2WEIGHTS23AGGREGATEMAX*ZRANGEzUnionStoreSumResult0-1WITHSCORESZRANGEzUnionStoreMinResult0-1WITHSCORESZRANGEzUnionStoreMaxResult0-1WITHSCORES执行结果如下下面是java代码@TestpublicvoidzUnionStore(){zSetOperations.add("zUnionStoreKey1","one",1);zSetOperations.add("zUnionStoreKey1","two",2);zSetOperations.add("zUnionStoreKey2","one",1);zSetOperations.add("zUnionStoreKey2","two",2);zSetOperations.add("zUnionStoreKey2","三",3);ZParamszParams=newZParams();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");//springzSetOperations.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));}