{"id":201179,"date":"2025-05-10T08:08:50","date_gmt":"2025-05-10T00:08:50","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201179\/"},"modified":"2025-05-10T08:08:50","modified_gmt":"2025-05-10T00:08:50","slug":"redis-zset%e5%ae%9e%e7%8e%b0%e6%bb%91%e5%8a%a8%e7%aa%97%e5%8f%a3%e9%99%90%e6%b5%81%e7%9a%84%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201179\/","title":{"rendered":"redis\u00a0zset\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u9650\u6d41\u7684\u4ee3\u7801"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>redis&nbsp;zset\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u9650\u6d41\u7684\u4ee3\u7801<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>IT\u884c\u4e1a\u76f8\u5bf9\u4e8e\u4e00\u822c\u4f20\u7edf\u884c\u4e1a\uff0c\u53d1\u5c55\u66f4\u65b0\u901f\u5ea6\u66f4\u5feb\uff0c\u4e00\u65e6\u505c\u6b62\u4e86\u5b66\u4e60\uff0c\u5f88\u5feb\u5c31\u4f1a\u88ab\u884c\u4e1a\u6240\u6dd8\u6c70\u3002\u6240\u4ee5\u6211\u4eec\u9700\u8981\u8e0f\u8e0f\u5b9e\u5b9e\u7684\u4e0d\u65ad\u5b66\u4e60\uff0c\u7cbe\u8fdb\u81ea\u5df1\u7684\u6280\u672f\uff0c\u5c24\u5176\u662f\u521d\u5b66\u8005\u3002\u4eca\u5929golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u6574\u7406\u4e86\u300aredis&nbsp;zset\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u9650\u6d41\u7684\u4ee3\u7801\u300b\uff0c\u804a\u804a\u6ed1\u52a8\u7a97\u53e3\u3001\u9650\u6d41\u3001rediszset\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<h2>\u9650\u6d41<\/h2>\n<p>\u9700\u6c42\u80cc\u666f\uff1a\u540c\u4e00\u7528\u62371\u5206\u949f\u5185\u767b\u5f55\u5931\u8d25\u6b21\u6570\u8d85\u8fc73\u6b21\uff0c\u9875\u9762\u6dfb\u52a0\u9a8c\u8bc1\u7801\u767b\u5f55\u9a8c\u8bc1\uff0c\u4e5f\u5373\u662f\u9650\u6d41\u7684\u601d\u60f3\u3002<\/p>\n<p>\u5e38\u89c1\u7684\u9650\u6d41\u7b97\u6cd5\uff1a\u56fa\u5b9a\u7a97\u53e3\u8ba1\u6570\u5668\uff1b\u6ed1\u52a8\u7a97\u53e3\u8ba1\u6570\u5668\uff1b\u6f0f\u6876\uff1b\u4ee4\u724c\u6876\u3002\u672c\u7bc7\u9009\u62e9\u7684\u6ed1\u52a8\u7a97\u53e3\u8ba1\u6570\u5668<\/p>\n<h2>redis zset\u7279\u6027<\/h2>\n<p>Redis \u6709\u5e8f\u96c6\u5408(sorted set)\u548c\u96c6\u5408(set)\u4e00\u6837\u4e5f\u662f string \u7c7b\u578b\u5143\u7d20\u7684\u96c6\u5408,\u4e14\u4e0d\u5141\u8bb8\u91cd\u590d\u7684\u6210\u5458\u3002\u4e0d\u540c\u7684\u662f\u6bcf\u4e2a\u5143\u7d20\u90fd\u4f1a\u5173\u8054\u4e00\u4e2a double \u7c7b\u578b\u7684\u5206\u6570(score)\u3002redis \u6b63\u662f\u901a\u8fc7\u5206\u6570\u6765\u4e3a\u96c6\u5408\u4e2d\u7684\u6210\u5458\u8fdb\u884c\u4ece\u5c0f\u5230\u5927\u7684\u6392\u5e8f\u3002<\/p>\n<p>\u53ef\u53c2\u8003java\u7684LinkedHashMap\u548cHashMap\uff0c\u90fd\u662f\u901a\u8fc7\u591a\u7ef4\u62a4\u53d8\u91cf\u4f7f\u65e0\u5e8f\u7684\u96c6\u5408\u53d8\u6210\u6709\u5e8f\u7684\u3002\u533a\u522b\u662fLinkedHashMap\u5185\u90e8\u662f\u591a\u7ef4\u62a4\u4e862\u4e2a\u6210\u5458\u53d8\u91cfEntry before, after\u7528\u4e8e\u53cc\u5411\u94fe\u8868\u7684\u8fde\u63a5\uff0credis zset\u662f\u591a\u7ef4\u62a4\u4e86\u4e00\u4e2ascore\u53d8\u91cf\u5b8c\u6210\u987a\u5e8f\u7684\u6392\u5217\u3002 <\/p>\n<p>\u6709\u5e8f\u96c6\u5408\u7684\u6210\u5458\u662f\u552f\u4e00\u7684,\u4f46\u5206\u6570(score)\u53ef\u4ee5\u91cd\u590d\u3002<\/p>\n<h2>\u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5<\/h2>\n<p>\u6ed1\u52a8\u7a97\u53e3\u7b97\u6cd5\u601d\u60f3\u5c31\u662f\u8bb0\u5f55\u4e00\u4e2a\u6ed1\u52a8\u7684\u65f6\u95f4\u7a97\u53e3\u5185\u7684\u64cd\u4f5c\u6b21\u6570\uff0c\u64cd\u4f5c\u6b21\u6570\u8d85\u8fc7\u9608\u503c\u5219\u8fdb\u884c\u9650\u6d41\u3002<\/p>\n<p>\u7f51\u4e0a\u627e\u7684\u56fe\uff1a<\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167244660863af8290d6a83.png\" class=\"aligncenter\"><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167244660963af82911c335.jpg\" class=\"aligncenter\"><\/p>\n<p style=\"text-align:center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221231\/167244660963af8291a486d.png\" class=\"aligncenter\"><\/p>\n<h2>java\u4ee3\u7801\u5b9e\u73b0<\/h2>\n<p>key\u4f7f\u7528\u7528\u6237\u7684\u767b\u5f55\u540d\uff0cvalue\u6570\u636e\u7c7b\u578b\u4f7f\u7528zset\uff0czset\u7684score\u4f7f\u7528\u5f53\u524d\u767b\u5f55\u65f6\u95f4\u6233\uff0cvalue\u4e5f\u4f7f\u7528\u5f53\u524d\u767b\u5f55\u65f6\u95f4\u6233\u3002<\/p>\n<p>key\u867d\u7136\u6211\u7528\u7684\u767b\u5f55\u540d(\u5df2\u6ee1\u8db3\u6211\u7684\u9700\u6c42)\uff0c\u4f46\u5efa\u8bae\u5b9e\u9645\u5e94\u7528\u65f6\u4f7f\u7528uid\u7b49\u5177\u6709\u552f\u4e00\u6807\u8bc6\u7684\u5b57\u6bb5\u3002zset\u8981\u6c42value\u552f\u4e00\u4e0d\u53ef\u91cd\u590d\uff0c\u6240\u4ee5\u5f53\u524d\u65f6\u95f4\u6233\u9700\u4e0d\u9700\u8981\u518d\u6dfb\u52a0\u4e00\u968f\u673a\u6570\u6765\u505a\u552f\u4e00\u6807\u8bc6\u5f85\u9a8c\u8bc1\u3002<\/p>\n<pre>import java.util.concurrent.TimeUnit;\nimport lombok.extern.slf4j.Slf4j;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.data.redis.core.ZSetOperations;\nimport org.springframework.stereotype.Component;\n \n\/**\n * redis\u4f7f\u7528zset\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u8ba1\u6570\n * key\uff1asliding_window_\u7528\u6237\u767b\u5f55\u540d\n * value(zset)\uff1avalue=\u5f53\u524d\u65f6\u95f4\u6233,score=\u5f53\u524d\u65f6\u95f4\u6233\n *\n * @author zhaoshuxiang\n * @date 2022\/3\/2\n *\/\n@Component\n@Slf4j\npublic class SlidingWindowCounter {\n    \/**\n     * redis key\u524d\u7f00\n     *\/\n    private static final String SLIDING_WINDOW = \"sliding_window_\";\n    @Autowired\n    private RedisTemplate redisTemplate;\n     * \u5224\u65adkey\u7684value\u4e2d\u7684\u6709\u6548\u8bbf\u95ee\u6b21\u6570\u662f\u5426\u8d85\u8fc7\u6700\u5927\u9650\u5b9a\u503cmaxCount\n     * \u5224\u65ad\u4e0e\u6570\u91cf\u589e\u957f\u5206\u5f00\u5904\u7406\n     *\n     * @param key            redis key\n     * @param windowInSecond \u7a97\u53e3\u95f4\u9694\uff0c\u79d2\n     * @param maxCount       \u6700\u5927\u8ba1\u6570\n     * @return \u662f or \u5426\n    public boolean overMaxCount(String key, int windowInSecond, long maxCount) {\n        key = SLIDING_WINDOW + key;\n        log.info(\"redis key = {}\", key);\n        \/\/ \u5f53\u524d\u65f6\u95f4\n        long currentMs = System.currentTimeMillis();\n        \/\/ \u7a97\u53e3\u5f00\u59cb\u65f6\u95f4\n        long windowStartMs = currentMs - windowInSecond * 1000L;\n        \/\/ \u6309score\u7edf\u8ba1key\u7684value\u4e2d\u7684\u6709\u6548\u6570\u91cf\n        Long count = redisTemplate.opsForZSet().count(key, windowStartMs, currentMs);\n        \/\/ \u5df2\u8bbf\u95ee\u6b21\u6570 &gt;= \u6700\u5927\u53ef\u8bbf\u95ee\u503c\n        return count &gt;= maxCount;\n    }\n     * \u5224\u65adkey\u7684value\u4e2d\u7684\u6709\u6548\u8bbf\u95ee\u6b21\u6570\u662f\u5426\u8d85\u8fc7\u6700\u5927\u9650\u5b9a\u503cmaxCount\uff0c\u82e5\u6ca1\u8d85\u8fc7\uff0c\u8c03\u7528increment\u65b9\u6cd5\uff0c\u5c06\u7a97\u53e3\u5185\u7684\u8bbf\u95ee\u6570\u52a0\u4e00\n     * \u5224\u65ad\u4e0e\u6570\u91cf\u589e\u957f\u540c\u6b65\u5904\u7406\n     * @return \u53ef\u8bbf\u95ee or \u4e0d\u53ef\u8bbf\u95ee\n    public boolean canAccess(String key, int windowInSecond, long maxCount) {\n        \/\/\u6309key\u7edf\u8ba1\u96c6\u5408\u4e2d\u7684\u6709\u6548\u6570\u91cf\n        Long count = redisTemplate.opsForZSet().zCard(key);\n        if (count \n<h2>\u8865\u5145\uff1aRedis zSet\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u5bf9\u77ed\u4fe1\u8fdb\u884c\u9632\u5237\u9650\u6d41<\/h2>\n<h3>\u524d\u8a00<\/h3>\n<p>\u3000\u3000\u4e3b\u8981\u9488\u5bf9\u76ee\u524d\u7ebf\u4e0a\u77ed\u4fe1\u88ab\u811a\u672c\u6076\u610f\u76d7\u5237\u7684\u60c5\u51b5\uff0c\u7528Redis\u5b9e\u73b0\u6ed1\u52a8\u7a97\u53e3\u9650\u6d41<\/p>\n<h3>\u793a\u4f8b\u4ee3\u7801<\/h3>\n<pre>public void checkCurrentWindowValue(String telNum) {\n&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; &nbsp; &nbsp; String windowKey = CommonConstant.getNnSmsWindowKey(telNum);\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u6233\n&nbsp; &nbsp; &nbsp; &nbsp; long currentTime = System.currentTimeMillis();\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/1\u5c0f\u65f6\uff0c\u9ed8\u8ba4\u53ea\u80fd\u53d15\u6b21\uff0c\u53c2\u6570smsWindowMax\u505a\u6210\u53ef\u914d\u7f6e\u9879\uff0c\u914d\u7f6e\u5230Nacos\u914d\u7f6e\u4e2d\u5fc3\uff0c\u53ef\u4ee5\u52a8\u6001\u8c03\u6574\n&nbsp; &nbsp; &nbsp; &nbsp; if (RedisUtil.hasKey(windowKey)) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u53c2\u6570smsWindowTime\u8868\u793a\u9650\u5236\u7684\u7a97\u53e3\u65f6\u95f4\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u8fd9\u91cc\u83b7\u53d6\u5f53\u524d\u65f6\u95f4\u4e0e\u9650\u5236\u7a97\u53e3\u65f6\u95f4\u4e4b\u95f4\u7684\u77ed\u4fe1\u53d1\u9001\u6b21\u6570\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Optional\n    \n      optional = Optional.ofNullable(RedisUtil.zCount(windowKey, currentTime - smsWindowTime, currentTime));\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (optional.isPresent()) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long count = optional.get();\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count &gt;= smsWindowMax) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; log.error(\"==========&gt;\u5f53\u524d\u53f7\u7801\uff1a{} \u77ed\u4fe1\u53d1\u9001\u592a\u9891\u7e41\uff0c{}\", telNum, count);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new ServiceException(MidRetCode.umid_10060);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; StringBuilder sb =new StringBuilder();\n&nbsp; &nbsp; &nbsp; &nbsp; String windowEle = sb.append(telNum).append(\":\").append(currentTime).toString();\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u6dfb\u52a0\u5f53\u524d\u53d1\u9001\u5143\u7d20\u5230zSet\u4e2d(\u7531\u4e8e\u4fdd\u8bc1\u5143\u7d20\u552f\u4e00\uff0c\u8fd9\u91cc\u5c06\u5143\u7d20\u52a0\u4e0a\u4e86\u5f53\u524d\u65f6\u95f4\u6233)\n&nbsp; &nbsp; &nbsp; &nbsp; RedisUtil.zAdd(windowKey, windowEle, currentTime);\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/\u8bbe\u7f6e2\u500d\u7a97\u53e3Key\uff1awindowKey \u7684\u8fc7\u671f\u65f6\u95f4\n&nbsp; &nbsp; &nbsp; &nbsp; RedisUtil.expire(windowKey, smsWindowTime*2, TimeUnit.MILLISECONDS);\n&nbsp; &nbsp; }\n    <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>redis&nbsp;zset\u5b9e&#46;&#46;&#46;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-201179","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201179","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=201179"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201179\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201179"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201179"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201179"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}