{"id":201426,"date":"2025-05-10T08:19:55","date_gmt":"2025-05-10T00:19:55","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201426\/"},"modified":"2025-05-10T08:19:55","modified_gmt":"2025-05-10T00:19:55","slug":"redis%e5%ae%9e%e7%8e%b0%e9%aa%8c%e8%af%81%e7%a0%81%e5%8f%91%e9%80%81%e5%b9%b6%e9%99%90%e5%88%b6%e6%af%8f%e6%97%a5%e5%8f%91%e9%80%81%e6%ac%a1%e6%95%b0%e7%9a%84%e7%a4%ba%e4%be%8b%e4%bb%a3%e7%a0%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201426\/","title":{"rendered":"Redis\u5b9e\u73b0\u9a8c\u8bc1\u7801\u53d1\u9001\u5e76\u9650\u5236\u6bcf\u65e5\u53d1\u9001\u6b21\u6570\u7684\u793a\u4f8b\u4ee3\u7801"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Redis\u5b9e\u73b0\u9a8c\u8bc1\u7801\u53d1\u9001\u5e76\u9650\u5236\u6bcf\u65e5\u53d1\u9001\u6b21\u6570\u7684\u793a\u4f8b\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\u5b9e\u73b0\u9a8c\u8bc1\u7801\u53d1\u9001\u5e76\u9650\u5236\u6bcf\u65e5\u53d1\u9001\u6b21\u6570\u7684\u793a\u4f8b\u4ee3\u7801\u300b\uff0c\u804a\u804a\u53d1\u9001\u3001Redis\u9a8c\u8bc1\u7801\u3001\u9650\u5236\u6b21\u6570\uff0c\u6211\u4eec\u4e00\u8d77\u6765\u770b\u770b\u5427\uff01<\/p>\n<h2>1\u3001\u529f\u80fd<\/h2>\n<ul>\n<li>\u8f93\u5165\u624b\u673a\u53f7\uff0c\u70b9\u51fb\u53d1\u9001\u540e\u968f\u673a\u751f\u6210\u516d\u4f4d\u6570\u5b57\u7801\uff0c2\u5206\u949f\u6709\u6548<\/li>\n<li>\u8f93\u5165\u9a8c\u8bc1\u7801\uff0c\u70b9\u51fb\u9a8c\u8bc1\uff0c\u8fd4\u56de\u6210\u529f\u6216\u5931\u8d25<\/li>\n<li>\u6bcf\u4e2a\u624b\u673a\u53f7\u6bcf\u5929\u53ea\u80fd\u8f933\u6b21<\/li>\n<\/ul>\n<h2>2\u3001\u5206\u6790<\/h2>\n<ul>\n<li>\u6bcf\u4e2a\u624b\u673a\u6bcf\u5929\u53ea\u80fd\u8f933\u6b21\uff1aincr\u6bcf\u6b21\u53d1\u9001\u4e4b\u540e+1\uff0c\u5f53\u503c\u4e3a3\u65f6\u63d0\u793a\u4e0d\u80fd\u53d1\u9001\uff0c\u8fc7\u671f\u65f6\u95f4\u4e3a\u5f53\u5929\u7ed3\u675f<\/li>\n<li>\u968f\u673a\u751f\u62106\u4f4d\u6570\u5b57\u9a8c\u8bc1\u7801\uff1aRandomUtil\uff08hutool\uff09<\/li>\n<li>\u9a8c\u8bc1\u78012\u5206\u949f\u6709\u6548\uff1a\u653e\u5165redis\u91cc\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f42\u5206\u949f<\/li>\n<li>\u5224\u65ad\u9a8c\u8bc1\u7801\u662f\u5426\u4e00\u81f4\uff1a\u4eceredis\u91cc\u83b7\u53d6\u9a8c\u8bc1\u7801\u548c\u8f93\u5165\u7684\u9a8c\u8bc1\u7801\u8fdb\u884c\u6bd4\u5bf9<\/li>\n<\/ul>\n<h2>3\u3001\u5b9e\u73b0<\/h2>\n<pre>package cn.ken.blog.controller.common;\n\nimport cn.hutool.core.date.DateUnit;\nimport cn.hutool.core.date.DateUtil;\nimport cn.hutool.core.util.RandomUtil;\nimport cn.ken.blog.common.constant.Constants;\nimport cn.ken.blog.common.domain.Result;\nimport cn.ken.blog.common.enums.ErrorCodeEnum;\nimport org.springframework.beans.factory.annotation.Autowired;\nimport org.springframework.data.redis.core.RedisTemplate;\nimport org.springframework.scheduling.annotation.Scheduled;\nimport org.springframework.util.ObjectUtils;\nimport org.springframework.web.bind.annotation.GetMapping;\nimport org.springframework.web.bind.annotation.RestController;\n\nimport java.util.Date;\nimport java.util.concurrent.TimeUnit;\n\n\/**\n&nbsp;* \u9a8c\u8bc1\u7801\u63a7\u5236\u5668\n&nbsp;* @author Ken-Chy129\n&nbsp;* @date 2022\/4\/17 20:28\n&nbsp;*\/\n@RestController\n@SuppressWarnings(value = { \"unchecked\", \"rawtypes\" })\npublic class CaptureController {\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; @Autowired\n&nbsp; &nbsp; private RedisTemplate redisTemplate;\n&nbsp; &nbsp;&nbsp;\n&nbsp; &nbsp; \/\/ \u751f\u6210\u9a8c\u8bc1\u7801\n&nbsp; &nbsp; @GetMapping(\"getNumCode\")\n&nbsp; &nbsp; public Result\n   \n     getNumCode(String phone) {\n&nbsp; &nbsp; &nbsp; &nbsp; String captureLimitKey = Constants.CAPTCHA_LIMIT_KEY + phone;\n&nbsp; &nbsp; &nbsp; &nbsp; Integer counts = (Integer) redisTemplate.opsForValue().get(captureLimitKey);\n&nbsp; &nbsp; &nbsp; &nbsp; if (ObjectUtils.isEmpty(counts)) {\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u4eca\u5929\u7b2c\u4e00\u6b21\u9a8c\u8bc1\uff0c\u6545\u4e4b\u524d\u7f13\u5b58\u4e2d\u65e0\u8be5\u952e\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u8ddd\u79bb\u4eca\u5929\u7ed3\u675f\u5269\u4e0b\u591a\u5c11\u6beb\u79d2\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; long expire = DateUtil.endOfDay(new Date()).between(new Date(), DateUnit.MS);\n&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.opsForValue().set(captureLimitKey, 1, expire, TimeUnit.MILLISECONDS);\n&nbsp; &nbsp; &nbsp; &nbsp; } else if (counts ().error(ErrorCodeEnum.OVER_LIMITS);\n&nbsp; &nbsp; &nbsp; &nbsp; }\n&nbsp; &nbsp; &nbsp; &nbsp; \/\/ \u751f\u6210\u9a8c\u8bc1\u7801\n&nbsp; &nbsp; &nbsp; &nbsp; String code = RandomUtil.randomNumbers(6); \/\/ \u968f\u673a\u751f\u6210\u516d\u4f4d\u6570\n&nbsp; &nbsp; &nbsp; &nbsp; String captureCodeKey = Constants.CAPTCHA_CODE_KEY + phone;\n&nbsp; &nbsp; &nbsp; &nbsp; redisTemplate.opsForValue().set(captureCodeKey, code, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);\n&nbsp; &nbsp; &nbsp; &nbsp; return new Result\n    \n     ().success(captureCodeKey + \":\" + code); &nbsp; &nbsp; } &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; \/\/ \u9a8c\u8bc1\u9a8c\u8bc1\u7801 &nbsp; &nbsp; @GetMapping(\"verify\") &nbsp; &nbsp; public Result\n     \n       verify(String phone, String code) { &nbsp; &nbsp; &nbsp; &nbsp; String captureCodeKey = Constants.CAPTCHA_CODE_KEY + phone; &nbsp; &nbsp; &nbsp; &nbsp; String realCode = (String) redisTemplate.opsForValue().get(captureCodeKey); &nbsp; &nbsp; &nbsp; &nbsp; if (ObjectUtils.isEmpty(realCode)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; \/\/ redis\u4e2d\u4e0d\u5b58\u5728\u8be5\u7528\u6237\u751f\u6210\u7684\u9a8c\u8bc1\u7801\uff0c\u8bc1\u660e\u9a8c\u8bc1\u7801\u4ee5\u8fc7\u671f\u9500\u6bc1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Result\n      \n       ().error(ErrorCodeEnum.OVERDUE_CODE); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; &nbsp; &nbsp; if (realCode.equals(code)) { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Result\n       \n        ().success(\"\u9a8c\u8bc1\u6210\u529f\"); &nbsp; &nbsp; &nbsp; &nbsp; } else { &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return new Result\n        \n         ().error(ErrorCodeEnum.ERROR_CODE); &nbsp; &nbsp; &nbsp; &nbsp; } &nbsp; &nbsp; } &nbsp; &nbsp;&nbsp; \/\/ &nbsp; &nbsp;@Scheduled(cron = \"0 0 12 * * ?\") \/\/ &nbsp; &nbsp;private void clear() { \/\/ &nbsp; &nbsp; &nbsp; &nbsp;redisTemplate.delete() \/\/ &nbsp; &nbsp;} }\n        \n       \n      \n     \n    \n   <\/pre>\n<pre>\/\/ Constants\u7c7b\n\n\/**\n&nbsp;* \u9a8c\u8bc1\u7801 redis key\n&nbsp;*\/\npublic static final String CAPTCHA_CODE_KEY = \"captcha_codes:\";\n\n\/**\n&nbsp;* \u6bcf\u65e5\u9650\u5236 redis key\n&nbsp;*\/\npublic static final String CAPTCHA_LIMIT_KEY = \"captcha_limits:\";\n\/**\n&nbsp;* \u9a8c\u8bc1\u7801\u6709\u6548\u671f\uff08\u5206\u949f\uff09\n&nbsp;*\/\npublic static final Integer CAPTCHA_EXPIRATION = 2;<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5b9e\u73b0\u9a8c\u8bc1\u7801\u53d1\u9001\u5e76\u9650\u5236\u6bcf&#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-201426","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201426","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=201426"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201426\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201426"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201426"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201426"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}