{"id":3572,"date":"2018-12-15T16:58:45","date_gmt":"2018-12-15T08:58:45","guid":{"rendered":"http:\/\/www.xttblog.com\/?p=3572"},"modified":"2018-12-15T16:58:45","modified_gmt":"2018-12-15T08:58:45","slug":"redis-%e4%b8%adzrevrangebyscorezrangebyscorewithscores-%e4%bd%bf%e7%94%a8%e4%b8%8d%e5%bd%93%e5%af%bc%e8%87%b4%e7%9a%84%e5%88%86%e9%a1%b5-bug%ef%bc%81","status":"publish","type":"post","link":"https:\/\/www.xttblog.com\/?p=3572","title":{"rendered":"Redis \u4e2dZREVRANGEBYSCORE(zrangeByScoreWithScores) \u4f7f\u7528\u4e0d\u5f53\u5bfc\u81f4\u7684\u5206\u9875 Bug\uff01"},"content":{"rendered":"<p>\n\t\u6700\u8fd1\u6211\u4eec\u7684\u7535\u5546\u7cfb\u7edf\u4e2d\u5728\u5927\u4fc3\u65f6\u51fa\u73b0\u4e86\u4e00\u4e2a\u5206\u9875 Bug\uff0c\u95ee\u9898\u4ea7\u751f\u7684\u539f\u56e0\u5c31\u662f Redis \u7684&nbsp;ZREVRANGEBYSCORE \u6307\u4ee4\u4f7f\u7528\u4e0d\u5f53\u5bfc\u81f4\u7684\u3002\u8868\u73b0\u5728 java \u4ee3\u7801\u4e2d\u5c31\u662f&nbsp;zrangeByScoreWithScores \u65b9\u6cd5\u4f7f\u7528\u4e0d\u5f53\u3002\u672c\u6587\u6765\u8bf4\u8bf4\u8fd9\u4e2a Sorted Set \u6709\u5e8f\u96c6\u5408\u5b9e\u73b0\u5206\u9875\u4ea7\u751f\u7684 Bug \u95ee\u9898\u3002\n<\/p>\n<p>\n\t\u9996\u5148\uff0c\u6211\u4eec\u7ebf\u4e0a\u7684\u7535\u5546\u7cfb\u7edf\u6b63\u5728\u8fdb\u884c\u4e00\u4e2a\u5927\u4fc3\u6d3b\u52a8\u3002\u4e70\u5bb6\u975e\u5e38\u7684\u591a\uff0c\u5bfc\u81f4\u4e00\u65f6\u95f4\u4ea7\u751f\u4e86\u975e\u5e38\u591a\u7684\u8bc4\u8bba\u3002\u5f00\u53d1\u4eba\u5458\u5728\u5b58\u50a8\uff08\u4fdd\u5b58\uff09\u8bc4\u8bba\u65f6\uff0c\u4f7f\u7528 Redis \u4e2d\u7684&nbsp;SortSet \u8fd9\u4e2a\u6570\u636e\u7ed3\u6784\u3002\u6bcf\u65b0\u589e\u4e00\u6761\u8bc4\u8bba\uff0c\u4ed6\u90fd\u901a\u8fc7&nbsp;zadd \u547d\u4ee4\u5c06\u8d77\u4fdd\u5b58\u81f3 Redis \u548c MySQL \u4e2d\uff0c\u67e5\u8be2\u65f6\u76f4\u63a5\u5728 Redis \u4e2d\u53d6\u51fa\u6765\u3002\u4ee3\u7801\u62bd\u8c61\u4e00\u4e0b\uff0c\u5982\u4e0b\u6240\u793a\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nboolean operated;\r\ntry (Jedis jedis = pool.getResource()) {\r\n    operated = jedis.zadd(COMMUNITY_PRAISE + commentId, \r\n    \t-System.currentTimeMillis() \/ 1000, \r\n    \taccountId) == 1;\r\n}\r\nif (operated) {\r\n    commentDao.incrPraiseCount(comment);\r\n}<\/pre>\n<p>\n\t\u987a\u4fbf\u8bf4\u4e00\u4e0b\uff0cSystem.currentTimeMillis() \u662f\u6beb\u79d2\uff0cSystem.currentTimeMillis() \/ 1000 \u5c31\u76f8\u5f53\u4e8e\u7cbe\u786e\u5230\u79d2\u3002\n<\/p>\n<p>\n\t\u8fd9\u4e2a\u6d3b\u52a8\u662f\u5927\u4fc3\uff0c\u53c2\u4e0e\u6d3b\u52a8\u7684\u9700\u8981\u8bc4\u8bba\u3002\u6d3b\u52a8\u6bd4\u9884\u671f\u7684\u70ed\uff0c\u5bfc\u81f4\u540c\u4e00\u79d2\u7684\u8bc4\u8bba\u5c31\u6bd4\u8f83\u591a\uff0c\u5bfc\u81f4&nbsp;zrangeByScoreWithScores \u5728\u53d6\u51fa\u8bc4\u8bba\u5217\u8868\u65f6\uff0c\u9047\u5230\u540c\u4e00\u79d2\u7684\u8d85\u8fc7 10 \u6761\u7684\u8bc4\u8bba\uff0c\u5c31\u4f1a\u51fa\u73b0\u91cd\u590d\u6570\u636e\u3002\n<\/p>\n<p>\n\t\u53d6\u51fa\u8bc4\u8bba\u5217\u8868\u7684\u903b\u8f91\u5927\u81f4\u62bd\u8c61\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:java;\">\r\nSet&lt;Tuple&gt; praisedIds = jedis.zrangeByScoreWithScores(COMMUNITY_PRAISE + id, \r\n\tlastScore, 0, 0, 10);\r\nList&lt;Comment&gt; result = new ArrayList&lt;&gt;();\r\nfor (Tuple praisedId : praisedIds) {\r\n\t\/\/ \u5176\u4ed6\u903b\u8f91\r\n    result.add(comment);\r\n}\r\nreturn result;<\/pre>\n<p>\n\t\u770b\u5230\u6ca1\uff1f\u4ed6\u6bcf\u6b21\u62ff&nbsp;lastScore \u8bb0\u5f55\uff0c\u4e5f\u5c31\u662f\u4e0a\u4e00\u9875\u8bc4\u8bba\u7684\u6700\u540e\u4e00\u6761\u6570\u636e\u7684\u65f6\u95f4\u3002\u6765\u83b7\u53d6\u65b0\u7684 10 \u6761\u8bc4\u8bba\u6570\u636e\u3002\u7531\u4e8e\u540c\u4e00\u79d2\u8bc4\u8bba\u7684\u8bb0\u5f55\u8d85\u8fc7 10 \u6761\u540e\uff0c\u5bfc\u81f4\u6bcf\u6b21 APP \u6bcf\u6b21\u4e0b\u62c9\u5237\u65b0\u83b7\u53d6\u5230\u91cd\u590d\u6570\u636e\u7684\u6982\u7387\u6bd4\u8f83\u9ad8\u3002\u4e8e\u662f\u5c31\u6709\u5ba2\u6237\u6295\u8bc9\uff0c\u770b\u8bc4\u8bba\u4e2d\u7684\u6570\u636e\u90fd\u662f\u4e00\u6837\u7684\u3002\u65e0\u8bba\u600e\u4e48\u5237\u65b0\u90fd\u662f 10 \u6761\u4e00\u6837\u7684\u6570\u636e\uff0c\u548c\u4e0a\u4e00\u9875\u7684\u6570\u636e\u4e00\u6837\u3002\n<\/p>\n<p>\n\t\u8fd9\u4e2a&nbsp;zrangeByScoreWithScores \u5176\u5b9e\u5c31\u662f Redis \u4e2d\u7684&nbsp;ZREVRANGEBYSCORE \u6307\u4ee4\u3002\u5b8c\u6574\u6307\u4ee4\u5982\u4e0b\uff1a\n<\/p>\n<pre class=\"brush:bash;\">\r\nZREVRANGEBYSCORE key max min WITHSCORES LIMIT offset count<\/pre>\n<p>\n\t<img decoding=\"async\" alt=\"Redis zrevrangebyscore \u6307\u4ee4\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/redis-zrevrangebyscore.png\" style=\"width: 611px;height: 668px\" \/>\n<\/p>\n<p>\n\t\u5176\u4e2d\u8fd9\u4e2a limit \u662f\u53ef\u4ee5\u5206\u9875\u7684\u3002\u8fd9\u4e2a\u5206\u9875\u5bf9\u4e0b\u62c9\u5237\u65b0\u4e5f\u6709\u4e00\u4e2a\u95ee\u9898\uff0c\u90a3\u5c31\u662f\u5728\u4f60\u4e0b\u62c9\u5237\u65b0\u65f6\uff0c\u521a\u597d\u65b0\u589e\u4e86\u51e0\u4e2a\u8bc4\u8bba\uff0c\u6240\u4ee5\u5237\u65b0\u51fa\u6765\u7684\u6570\u636e\u4e5f\u53ef\u80fd\u6709\u91cd\u590d\u7684\u6570\u636e\u3002\n<\/p>\n<p>\n\t\u540e\u6765\u8fd9\u4e2a\u7a0b\u5e8f\u5458\u9488\u5bf9\u8fd9\u4e2a\u95ee\u9898\u6709\u6539\u4e86\u4e00\u4e0b\uff0c\u6bcf\u6b21\u62ff\u5230&nbsp;lastScore \u540e\u52a0 1\u3002\u6211\u770b\u5230\u8fd9\u4e2a\u4ee3\u7801\u540e\uff0c\u53c8\u627e\u4ed6\u4e86\uff0c\u8fd9\u4e2a\u505a\u6cd5\u80af\u5b9a\u4e0d\u884c\u7684\u3002\u4e5f\u5c31\u662f\u8bf4\u4f60\u6bcf\u6b21\u62ff\u5230\u4e0a\u4e00\u9875\u6700\u540e\u4e00\u6761\u7684&nbsp;lastScore \u540e\uff0c\u518d\u52a0 1\u3002\u5c31\u76f8\u5f53\u4e0e\u65f6\u95f4\u4e0a\u52a0\u4e86 1 \u79d2\uff0c\u4e2d\u95f4\u80af\u5b9a\u4f1a\u6f0f\u6389\u4e00\u4e9b\u6570\u636e\u7684\u3002\n<\/p>\n<p>\n\t\u90a3\u4e48\u8be5\u600e\u4e48\u529e\u5462\uff1f\u5982\u679c\u4f60\u60f3\u4f7f\u7528&nbsp;lastScore \u8fdb\u884c\u5206\u9875\uff0c\u5c31\u5fc5\u987b\u4fdd\u8bc1\u5b83\u552f\u4e00\u3002\u4e0d\u7136\u5c31\u6709\u6982\u7387\u53d1\u751f\u5237\u65b0\u5217\u8868\u65f6\uff0c\u51fa\u73b0\u91cd\u590d\u6570\u636e\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"Redis \u5728\u65f6\u4e8b\u65b0\u95fb\u4e2d\u7684\u5e94\u7528\" src=\"http:\/\/www.redis.cn\/images\/redis_zrevrangebyscores1.png\" style=\"width: 356px;height: 291px\" \/>\n<\/p>\n<p>\n\t\u6211\u4eec\u8fd9\u4e2a\u548c\u65f6\u4e8b\u65b0\u95fb\u8fd8\u6709\u4e9b\u4e0d\u4e00\u6837\uff0c\u65b0\u95fb\u7684\u5199\u5165\u65f6\u95f4\u91cd\u590d\u6982\u7387\u6ca1\u6709\u6211\u4eec\u8fd9\u4e2a\u9ad8\u3002\u5c24\u5176\u662f\u6211\u4eec\u5728\u5927\u4fc3\u7684\u6d3b\u52a8\u5f53\u4e2d\u3002\n<\/p>\n<p>\n\t\u6700\u540e\u8bf4\u4e00\u4e0b\u8fd9\u4e2a\u95ee\u9898\u7684\u89e3\u51b3\u529e\u6cd5\u3002\u4e00\u79cd\u5c31\u662f\u4fdd\u8bc1&nbsp;lastScore \u552f\u4e00\uff0c\u6216\u8005\u8bf4\u65f6\u95f4\u5728\u8fdb\u4e00\u6b65\u7cbe\u786e\u3002\u8fd8\u6709\u4e00\u79cd\u5c31\u662f&nbsp;lastScore + limit \u7684\u7ec4\u5408\u6765\u5b9e\u73b0\u5206\u9875\u3002\u6216\u8005\u5c31\u662f\u5355\u7eaf\u7684\u4f7f\u7528 limit \u6765\u5b9e\u73b0\uff0c\u4f46\u662f\u4e5f\u8981\u6ce8\u610f\u4e0d\u80fd\u5237\u65b0\u51fa\u91cd\u590d\u6570\u636e\u3002\n<\/p>\n<p>\n\t<img decoding=\"async\" alt=\"SortSet \u7ed3\u6784\" src=\"http:\/\/nnblog-storage.b0.upaiyun.com\/img\/redis.png\" style=\"width: 826px;height: 587px\" \/>\n<\/p>\n<p>\n\t\u6211\u4eec\u7684\u505a\u6cd5\uff0c\u6700\u7ec8\u662f\u53c2\u8003\u4e86\u4e0a\u56fe\u3002\n<\/p>\n<p>\n\t\u5c06\u6bcf\u4e2a\u4e3b\u9898\u7684 topicId \u4f5c\u4e3a set \u7684 key\uff0c\u5c06\u4e0e\u8be5\u4e3b\u9898\u5173\u8054\u7684\u8bc4\u8bba\u7684 createDate \u548c commentId \u5206\u522b\u4f5c\u4e3a set \u7684 score \u548c member\uff0ccommentId \u7684\u987a\u5e8f\u5c31\u6839\u636e createDate \u7684\u5927\u5c0f\u8fdb\u884c\u6392\u5217\u3002\n<\/p>\n<p>\n\t\u5f53\u9700\u8981\u67e5\u8be2\u67d0\u4e2a\u4e3b\u9898\u67d0\u4e00\u9875\u7684\u8bc4\u8bba\u65f6\uff0c\u5c31\u53ef\u4e3b\u9898\u7684 topicId \u901a\u8fc7\u6307\u4ee4 zrevrange topicId (page-1)&times;10 (page-1)&times;10+perPage \u8fd9\u6837\u5c31\u80fd\u627e\u51fa\u67d0\u4e2a\u4e3b\u9898\u4e0b\u67d0\u4e00\u9875\u7684\u6309\u65f6\u95f4\u6392\u597d\u987a\u5e8f\u7684\u6240\u6709\u8bc4\u8bba\u7684 commintId\u3002page \u4e3a\u67e5\u8be2\u7b2c\u51e0\u9875\u7684\u9875\u7801\uff0cperPage \u4e3a\u6bcf\u9875\u663e\u793a\u7684\u6761\u6570\u3002\n<\/p>\n<p>\n\t\u5f53\u627e\u5230\u6240\u6709\u8bc4\u8bba\u7684 commentId \u540e\uff0c\u5c31\u53ef\u4ee5\u628a\u8fd9\u4e9b commentId \u4f5c\u4e3a key \u53bb Hash \u7ed3\u6784\u4e2d\u53bb\u67e5\u8be2\u8be5\u6761\u8bc4\u8bba\u5bf9\u5e94\u7684\u5185\u5bb9\u3002\n<\/p>\n<p>\n\t\u8fd9\u6837\u5c31\u5229\u7528 SortSet \u548c Hash \u4e24\u79cd\u7ed3\u6784\u5728 Redis \u4e2d\u8fbe\u5230\u4e86\u5206\u9875\u548c\u6392\u5e8f\u7684\u76ee\u7684\u3002<\/p>\n<!-- <audio autoplay=\"autoplay\" height=\"100\" width=\"100\" hidden=\"true\"><source src=\"https:\/\/c.coka.la\/orAvPR.mp3\" type=\"audio\/mp3\"\/><\/audio><img decoding=\"async\" src=\"https:\/\/images.weserv.nl\/?url=https:\/\/ws3.sinaimg.cn\/large\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/> --><p><!-- <img decoding=\"async\" src=\"https:\/\/raw.githubusercontent.com\/xmt1139057136\/xttblog\/master\/8fa5dcfcgy1g4em3rzar5j20ib089ae0.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/>--><img decoding=\"async\" src=\"https:\/\/saas.51autoparts.cn\/api\/admin\/sys-file\/oss\/file?fileName=84db51f8561e448f9a4fda2eff0811f9.jpg\" alt=\"\u4e1a\u4f59\u8349\u516c\u4f17\u53f7\"\/><\/p><p>\u6700\u540e\uff0c\u6b22\u8fce\u5173\u6ce8\u6211\u7684\u4e2a\u4eba\u5fae\u4fe1\u516c\u4f17\u53f7\uff1a\u4e1a\u4f59\u8349\uff08yyucao\uff09\uff01<b>\u53ef\u52a0\u4f5c\u8005\u5fae\u4fe1\u53f7\uff1a<font color=\"red\">xttblog2<\/font>\u3002\u5907\u6ce8\uff1a\u201c1\u201d\uff0c\u6dfb\u52a0\u535a\u4e3b\u5fae\u4fe1\u62c9\u4f60\u8fdb\u5fae\u4fe1\u7fa4\u3002\u5907\u6ce8\u9519\u8bef\u4e0d\u4f1a\u540c\u610f\u597d\u53cb\u7533\u8bf7\u3002<\/b>\u518d\u6b21\u611f\u8c22\u60a8\u7684\u5173\u6ce8\uff01\u540e\u7eed\u6709\u7cbe\u5f69\u5185\u5bb9\u4f1a\u7b2c\u4e00\u65f6\u95f4\u53d1\u7ed9\u60a8\uff01\u539f\u521b\u6587\u7ae0\u6295\u7a3f\u8bf7\u53d1\u9001\u81f3532009913@qq.com\u90ae\u7bb1\u3002\u5546\u52a1\u5408\u4f5c\u4e5f\u53ef\u6dfb\u52a0\u4f5c\u8005\u5fae\u4fe1\u8fdb\u884c\u8054\u7cfb\uff01<\/p><p>\u672c\u6587\u539f\u6587\u51fa\u5904\uff1a<a href=\"https:\/\/www.xttblog.com\">\u4e1a\u4f59\u8349<\/a>\uff1a &raquo; <a href=\"https:\/\/www.xttblog.com\/?p=3572\">Redis \u4e2dZREVRANGEBYSCORE(zrangeByScoreWithScores) \u4f7f\u7528\u4e0d\u5f53\u5bfc\u81f4\u7684\u5206\u9875 Bug\uff01<\/a><\/p><!-- <div class=\"bd-reward-stl\"><button id=\"bdRewardBtn\"><span><\/span><\/button><\/div> -->","protected":false},"excerpt":{"rendered":"<p>\u6700\u8fd1\u6211\u4eec\u7684\u7535\u5546\u7cfb\u7edf\u4e2d\u5728\u5927\u4fc3\u65f6\u51fa\u73b0\u4e86\u4e00\u4e2a\u5206\u9875 Bug\uff0c\u95ee\u9898\u4ea7\u751f\u7684\u539f\u56e0\u5c31\u662f Redis \u7684&nbsp;ZREVRA [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[1829,1779,669,1828,857],"class_list":["post-3572","post","type-post","status-publish","format-standard","hentry","category-java","tag-hash","tag-hashset","tag-redis","tag-sortset","tag-857"],"_links":{"self":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3572","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3572"}],"version-history":[{"count":1,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3572\/revisions"}],"predecessor-version":[{"id":3573,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=\/wp\/v2\/posts\/3572\/revisions\/3573"}],"wp:attachment":[{"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3572"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3572"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.xttblog.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3572"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}