{"id":201542,"date":"2025-05-10T10:14:33","date_gmt":"2025-05-10T02:14:33","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201542\/"},"modified":"2025-05-10T10:14:33","modified_gmt":"2025-05-10T02:14:33","slug":"redis%e5%ae%9e%e7%8e%b0%e9%99%90%e6%b5%81%e5%99%a8%e7%9a%84%e4%b8%89%e7%a7%8d%e6%96%b9%e6%b3%95%e5%b0%8f%e7%bb%93","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201542\/","title":{"rendered":"Redis\u5b9e\u73b0\u9650\u6d41\u5668\u7684\u4e09\u79cd\u65b9\u6cd5(\u5c0f\u7ed3)"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5b9e\u73b0\u9650\u6d41\u5668\u7684\u4e09\u79cd\u65b9\u6cd5(\u5c0f\u7ed3)<\/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\u5b9e\u73b0\u9650\u6d41\u5668\u7684\u4e09\u79cd\u65b9\u6cd5(\u5c0f\u7ed3)\u300b\uff0c\u804a\u804aRedis\u9650\u6d41\u5668\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<h2>\u65b9\u6cd5\u4e00\uff1a\u57fa\u4e8eRedis\u7684setnx\u7684\u64cd\u4f5c<\/h2>\n<p>\u6211\u4eec\u5728\u4f7f\u7528Redis\u7684\u5206\u5e03\u5f0f\u9501\u7684\u65f6\u5019\uff0c\u5927\u5bb6\u90fd\u77e5\u9053\u662f\u4f9d\u9760\u4e86setnx\u7684\u6307\u4ee4\uff0c\u5728CAS\uff08Compare and swap\uff09\u7684\u64cd\u4f5c\u7684\u65f6\u5019\uff0c\u540c\u65f6\u7ed9\u6307\u5b9a\u7684key\u8bbe\u7f6e\u4e86\u8fc7\u671f\u5b9e\u8df5\uff08expire\uff09\uff0c\u6211\u4eec\u5728\u9650\u6d41\u7684\u4e3b\u8981\u76ee\u7684\u5c31\u662f\u4e3a\u4e86\u5728\u5355\u4f4d\u65f6\u95f4\u5185\uff0c\u6709\u4e14\u4ec5\u6709N\u6570\u91cf\u7684\u8bf7\u6c42\u80fd\u591f\u8bbf\u95ee\u6211\u7684\u4ee3\u7801\u7a0b\u5e8f\u3002\u6240\u4ee5\u4f9d\u9760setnx\u53ef\u4ee5\u5f88\u8f7b\u677e\u7684\u505a\u5230\u8fd9\u65b9\u9762\u7684\u529f\u80fd\u3002<\/p>\n<p>\u6bd4\u5982\u6211\u4eec\u9700\u8981\u572810\u79d2\u5185\u9650\u5b9a20\u4e2a\u8bf7\u6c42\uff0c\u90a3\u4e48\u6211\u4eec\u5728setnx\u7684\u65f6\u5019\u53ef\u4ee5\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f410\uff0c\u5f53\u8bf7\u6c42\u7684setnx\u6570\u91cf\u8fbe\u523020\u65f6\u5019\u5373\u8fbe\u5230\u4e86\u9650\u6d41\u6548\u679c\u3002\u4ee3\u7801\u6bd4\u8f83\u7b80\u5355\u5c31\u4e0d\u505a\u5c55\u793a\u4e86\u3002<\/p>\n<p>\u5f53\u7136\u8fd9\u79cd\u505a\u6cd5\u7684\u5f0a\u7aef\u662f\u5f88\u591a\u7684\uff0c\u6bd4\u5982\u5f53\u7edf\u8ba11-10\u79d2\u7684\u65f6\u5019\uff0c\u65e0\u6cd5\u7edf\u8ba12-11\u79d2\u4e4b\u5185\uff0c\u5982\u679c\u9700\u8981\u7edf\u8ba1N\u79d2\u5185\u7684M\u4e2a\u8bf7\u6c42\uff0c\u90a3\u4e48\u6211\u4eec\u7684Redis\u4e2d\u9700\u8981\u4fdd\u6301N\u4e2akey\u7b49\u7b49\u95ee\u9898\u3002<\/p>\n<p>\u5728\u5177\u4f53\u5b9e\u73b0\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u62e6\u622a\u5668HandlerInterceptor \uff1a<\/p>\n<pre>public class RequestCountInterceptor implements HandlerInterceptor {\n\n&nbsp; &nbsp; private LimitPolicy limitPolicy;\n\n&nbsp; &nbsp; public RequestCountInterceptor(LimitPolicy limitPolicy) {\n&nbsp; &nbsp; &nbsp; &nbsp; this.limitPolicy = limitPolicy;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; @Override\n&nbsp; &nbsp; public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {\n&nbsp; &nbsp; &nbsp; &nbsp; if (!limitPolicy.canDo()) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; return true;\n&nbsp; &nbsp; }\n}<\/pre>\n<p>\u540c\u65f6\u6dfb\u52a0\u4e00\u4e2a\u914d\u7f6eLimitConfiguration\uff1a<\/p>\n<pre>@Configuration\npublic class LimitConfiguration implements WebMvcConfigurer {\n    @Override\n    public void addInterceptors(InterceptorRegistry registry) {\n        registry.addInterceptor(new RequestCountInterceptor(new RedisLimit1())).addPathPatterns(\"\/my\/increase\");\n    }\n}<\/pre>\n<p>\u8fd9\u6837\u6bcf\u6b21\u5728\/my\/increase\u8bf7\u6c42\u5230\u8fbeController\u4e4b\u524d\u6309\u7b56\u7565RedisLimit1\u8fdb\u884c\u9650\u6d41\uff0c\u539f\u5148Controller\u91cc\u9762\u7684\u4ee3\u7801\u5c31\u4e0d\u7528\u4fee\u6539\u4e86\uff1a<\/p>\n<pre>@RestController\n@RequestMapping(\"my\")\npublic class MyController {\n    int i = 0;\n    @RequestMapping(\"\/increase\")\n    public int increase() {\n        return i++;\n    }\n}<\/pre>\n<p>\u5177\u4f53\u7684\u9650\u6d41\u903b\u8f91\u4ee3\u7801\u662f\u5728RedisLimit1\u7c7b\u4e2d\uff1a<\/p>\n<pre>\/**\n* \u65b9\u6cd5\u4e00\uff1a\u57fa\u4e8eRedis\u7684setnx\u7684\u64cd\u4f5c\n*\/\npublic class RedisLimit1 extends LimitPolicy {\n\n&nbsp; &nbsp; static {\n&nbsp; &nbsp; &nbsp; &nbsp; setNxExpire();\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; private static boolean setNxExpire() {\n&nbsp; &nbsp; &nbsp; &nbsp; SetParams setParams = new SetParams();\n&nbsp; &nbsp; &nbsp; &nbsp; setParams.nx();\n&nbsp; &nbsp; &nbsp; &nbsp; setParams.px(TIME);\n&nbsp; &nbsp; &nbsp; &nbsp; String result = jedis.set(KEY, COUNT + \"\", setParams);\n\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (SUCCESS.equals(result)) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; return false;\n&nbsp; &nbsp; }\n\n&nbsp; &nbsp; @Override\n&nbsp; &nbsp; public boolean canDo() {\n\n&nbsp; &nbsp; &nbsp; &nbsp; if (setNxExpire()) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u8bbe\u7f6e\u6210\u529f\uff0c\u8bf4\u660e\u539f\u5148\u4e0d\u5b58\u5728\uff0c\u6210\u529f\u8bbe\u7f6e\u4e3aCOUNT\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;\n&nbsp; &nbsp; &nbsp; &nbsp; } else {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/\u8bbe\u7f6e\u5931\u8d25\uff0c\u8bf4\u660e\u5df2\u7ecf\u5b58\u5728\uff0c\u76f4\u63a5\u51cf1\uff0c\u5e76\u4e14\u8fd4\u56de\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return jedis.decrBy(KEY, 1) &gt; 0;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; }\n}\n\npublic abstract class LimitPolicy {\n&nbsp; &nbsp; public static final int COUNT = 10; \/\/10 request\n&nbsp; &nbsp; public static final int TIME= 10*1000 ; \/\/ 10s\n&nbsp; &nbsp; public static final String SUCCESS = \"OK\";\n&nbsp; &nbsp; static Jedis jedis = new Jedis();\n&nbsp; &nbsp; abstract boolean canDo();\n}<\/pre>\n<p>\u8fd9\u6837\u5b9e\u73b0\u7684\u4e00\u4e2a\u6548\u679c\u662f\u6bcf\u79d2\u6700\u591a\u8bf7\u6c4210\u6b21\u3002<\/p>\n<h2>\u65b9\u6cd5\u4e8c\uff1a\u57fa\u4e8eRedis\u7684\u6570\u636e\u7ed3\u6784zset<\/h2>\n<p>\u5176\u5b9e\u9650\u6d41\u6d89\u53ca\u7684\u6700\u4e3b\u8981\u7684\u5c31\u662f\u6ed1\u52a8\u7a97\u53e3\uff0c\u4e0a\u9762\u4e5f\u63d0\u52301-10\u600e\u4e48\u53d8\u62102-11\u3002\u5176\u5b9e\u4e5f\u5c31\u662f\u8d77\u59cb\u503c\u548c\u672b\u7aef\u503c\u90fd\u5404+1\u5373\u53ef\u3002<br \/>\u800c\u6211\u4eec\u5982\u679c\u7528Redis\u7684list\u6570\u636e\u7ed3\u6784\u53ef\u4ee5\u8f7b\u800c\u6613\u4e3e\u7684\u5b9e\u73b0\u8be5\u529f\u80fd<br \/>\u6211\u4eec\u53ef\u4ee5\u5c06\u8bf7\u6c42\u6253\u9020\u6210\u4e00\u4e2azset\u6570\u7ec4\uff0c\u5f53\u6bcf\u4e00\u6b21\u8bf7\u6c42\u8fdb\u6765\u7684\u65f6\u5019\uff0cvalue\u4fdd\u6301\u552f\u4e00\uff0c\u53ef\u4ee5\u7528UUID\u751f\u6210\uff0c\u800cscore\u53ef\u4ee5\u7528\u5f53\u524d\u65f6\u95f4\u6233\u8868\u793a\uff0c\u56e0\u4e3ascore\u6211\u4eec\u53ef\u4ee5\u7528\u6765\u8ba1\u7b97\u5f53\u524d\u65f6\u95f4\u6233\u4e4b\u5185\u6709\u591a\u5c11\u7684\u8bf7\u6c42\u6570\u91cf\u3002\u800czset\u6570\u636e\u7ed3\u6784\u4e5f\u63d0\u4f9b\u4e86zrange\u65b9\u6cd5\u8ba9\u6211\u4eec\u53ef\u4ee5\u5f88\u8f7b\u6613\u7684\u83b7\u53d6\u52302\u4e2a\u65f6\u95f4\u6233\u5185\u6709\u591a\u5c11\u8bf7\u6c42<\/p>\n<pre>\/**\n* \u65b9\u6cd5\u4e8c\uff1a\u57fa\u4e8eRedis\u7684\u6570\u636e\u7ed3\u6784zset\n*\/\npublic class RedisLimit2 extends LimitPolicy {\n&nbsp; &nbsp; public static final String KEY2 = \"LIMIT2\";\n\n&nbsp; &nbsp; @Override\n&nbsp; &nbsp; public boolean canDo() {\n&nbsp; &nbsp; &nbsp; &nbsp; Long currentTime = new Date().getTime();\n&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(currentTime);\n&nbsp; &nbsp; &nbsp; &nbsp; if (jedis.zcard(KEY2) &gt; 0) { \/\/ \u8fd9\u91cc\u4e0d\u80fd\u7528get\u5224\u65ad\uff0c\u4f1a\u62a5\u9519\uff1aWRONGTYPE Operation against a key holding the wrong kind of value\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Integer count = jedis.zrangeByScore(KEY2, currentTime - TIME, currentTime).size(); \/\/ \u6ce8\u610f\u8fd9\u91cc\u4f7f\u7528zrangeByScore\uff0c\u4ee5\u65f6\u95f4\u4f5c\u4e3ascore\u3002zrange key start stop \u547d\u4ee4\u7684start\u548cstop\u662f\u5e8f\u53f7\u3002\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(count);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count != null &amp;&amp; count &gt; COUNT) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; jedis.zadd(KEY2, Double.valueOf(currentTime), UUID.randomUUID().toString());\n&nbsp; &nbsp; &nbsp; &nbsp; return true;\n\n&nbsp; &nbsp; }\n}<\/pre>\n<p>\u901a\u8fc7\u4e0a\u8ff0\u4ee3\u7801\u53ef\u4ee5\u505a\u5230\u6ed1\u52a8\u7a97\u53e3\u7684\u6548\u679c\uff0c\u5e76\u4e14\u80fd\u4fdd\u8bc1\u6bcfN\u79d2\u5185\u81f3\u591aM\u4e2a\u8bf7\u6c42\uff0c\u7f3a\u70b9\u5c31\u662fzset\u7684\u6570\u636e\u7ed3\u6784\u4f1a\u8d8a\u6765\u8d8a\u5927\u3002\u5b9e\u73b0\u65b9\u5f0f\u76f8\u5bf9\u4e5f\u662f\u6bd4\u8f83\u7b80\u5355\u7684\u3002<\/p>\n<h2>\u65b9\u6cd5\u4e09\uff1a\u57fa\u4e8eRedis\u7684\u4ee4\u724c\u6876\u7b97\u6cd5<\/h2>\n<p>\u63d0\u5230\u9650\u6d41\u5c31\u4e0d\u5f97\u4e0d\u63d0\u5230\u4ee4\u724c\u6876\u7b97\u6cd5\u4e86\u3002\u4ee4\u724c\u6876\u7b97\u6cd5\u63d0\u53ca\u5230\u8f93\u5165\u901f\u7387\u548c\u8f93\u51fa\u901f\u7387\uff0c\u5f53\u8f93\u51fa\u901f\u7387\u5927\u4e8e\u8f93\u5165\u901f\u7387\uff0c\u90a3\u4e48\u5c31\u662f\u8d85\u51fa\u6d41\u91cf\u9650\u5236\u4e86\u3002\u4e5f\u5c31\u662f\u8bf4\u6211\u4eec\u6bcf\u8bbf\u95ee\u4e00\u6b21\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u4eceRedis\u4e2d\u83b7\u53d6\u4e00\u4e2a\u4ee4\u724c\uff0c\u5982\u679c\u62ff\u5230\u4ee4\u724c\u4e86\uff0c\u90a3\u5c31\u8bf4\u660e\u6ca1\u8d85\u51fa\u9650\u5236\uff0c\u800c\u5982\u679c\u62ff\u4e0d\u5230\uff0c\u5219\u7ed3\u679c\u76f8\u53cd\u3002<br \/>\u4f9d\u9760\u4e0a\u8ff0\u7684\u601d\u60f3\uff0c\u6211\u4eec\u53ef\u4ee5\u7ed3\u5408Redis\u7684List\u6570\u636e\u7ed3\u6784\u5f88\u8f7b\u6613\u7684\u505a\u5230\u8fd9\u6837\u7684\u4ee3\u7801\uff0c\u53ea\u662f\u7b80\u5355\u5b9e\u73b0 \u4f9d\u9760List\u7684leftPop\u6765\u83b7\u53d6\u4ee4\u724c\u3002<\/p>\n<p>\u9996\u5148\u914d\u7f6e\u4e00\u4e2a\u5b9a\u65f6\u4efb\u52a1\uff0c\u901a\u8fc7redis\u7684list\u7684rpush\u65b9\u6cd5\u6bcf\u79d2\u63d2\u5165\u4e00\u4e2a\u4ee4\u724c\uff1a<\/p>\n<pre>@Configuration      \/\/1.\u4e3b\u8981\u7528\u4e8e\u6807\u8bb0\u914d\u7f6e\u7c7b\uff0c\u517c\u5907Component\u7684\u6548\u679c\u3002\n@EnableScheduling   \/\/ 2.\u5f00\u542f\u5b9a\u65f6\u4efb\u52a1\npublic class SaticScheduleTask {\n    \/\/3.\u6dfb\u52a0\u5b9a\u65f6\u4efb\u52a1\n    @Scheduled(fixedRate = 1000)\n    private void configureTasks() {\n        LimitPolicy.jedis.rpush(\"LIMIT3\", UUID.randomUUID().toString());\n    }\n}\n<\/pre>\n<p>\u9650\u6d41\u65f6\uff0c\u901a\u8fc7list\u7684lpop\u65b9\u6cd5\u4eceredis\u4e2d\u83b7\u53d6\u5bf9\u5e94\u7684\u4ee4\u724c\uff0c\u5982\u679c\u83b7\u53d6\u6210\u529f\u8868\u660e\u53ef\u4ee5\u6267\u884c\u8bf7\u6c42\uff1a<\/p>\n<pre>\/**\n* \u65b9\u6cd5\u4e09\uff1a\u4ee4\u724c\u6876\n*\/\npublic class RedisLimit3 extends LimitPolicy {\n&nbsp; &nbsp; public static final String KEY3 = \"LIMIT3\";\n\n&nbsp; &nbsp; @Override\n&nbsp; &nbsp; public boolean canDo() {\n\n&nbsp; &nbsp; &nbsp; &nbsp; Object result = jedis.lpop(KEY3);\n&nbsp; &nbsp; &nbsp; &nbsp; if (result == null) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; return true;\n&nbsp; &nbsp; }\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5b9e\u73b0\u9650\u6d41\u5668\u7684\u4e09\u79cd\u65b9\u6cd5(&#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-201542","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201542","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=201542"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201542\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201542"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201542"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201542"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}