{"id":44124,"date":"2024-12-01T17:28:31","date_gmt":"2024-12-01T09:28:31","guid":{"rendered":"https:\/\/fwq.ai\/blog\/44124\/"},"modified":"2024-12-01T17:28:31","modified_gmt":"2024-12-01T09:28:31","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:\/\/fwq.ai\/blog\/44124\/","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><i><\/i>0\u6d4f\u89c8<\/span><br \/>\n<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<p>\u7ec8\u4e8e\u4ecb\u7ecd\u5b8c\u5566\uff01\u5c0f\u4f19\u4f34\u4eec\uff0c\u8fd9\u7bc7\u5173\u4e8e\u300aRedis\u5b9e\u73b0\u9a8c\u8bc1\u7801\u53d1\u9001\u5e76\u9650\u5236\u6bcf\u65e5\u53d1\u9001\u6b21\u6570\u7684\u793a\u4f8b\u4ee3\u7801\u300b\u7684\u4ecb\u7ecd\u5e94\u8be5\u8ba9\u4f60\u6536\u83b7\u591a\u591a\u4e86\u5427\uff01\u6b22\u8fce\u5927\u5bb6\u6536\u85cf\u6216\u5206\u4eab\u7ed9\u66f4\u591a\u9700\u8981\u5b66\u4e60\u7684\u670b\u53cb\u5427~golang\u5b66\u4e60\u7f51\u516c\u4f17\u53f7\u4e5f\u4f1a\u53d1\u5e03\u6570\u636e\u5e93\u76f8\u5173\u77e5\u8bc6\uff0c\u5feb\u6765\u5173\u6ce8\u5427\uff01<\/p>\n<p>     \u7248\u672c\u58f0\u660e \u672c\u6587\u8f6c\u8f7d\u4e8e\uff1a\u811a\u672c\u4e4b\u5bb6 \u5982\u6709\u4fb5\u72af\uff0c\u8bf7\u8054\u7cfb \u5220\u9664  <\/p>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Redis\u5b9e\u73b0\u5355\u8bbe\u5907\u767b\u5f55\u7684\u573a\u666f\u5206\u6790\n <\/dd>\n<\/dl>\n<dl>\n<dt>\n <\/dt>\n<dd>\n   Win10\u4e0b\u901a\u8fc7Ubuntu\u5b89\u88c5Redis\u7684\u8fc7\u7a0b\n <\/dd>\n<\/dl>\n","protected":false},"excerpt":{"rendered":"<p>Redis\u5b9e\u73b0\u9a8c\u8bc1\u7801\u53d1\u9001\u5e76\u9650\u5236\u6bcf\u65e5\u53d1\u9001\u6b21\u6570\u7684\u793a\u4f8b\u4ee3\u7801 0\u6d4f\u89c8 \u6536\u85cf 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 1\u3001\u529f\u80fd \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 \u8f93\u5165\u9a8c\u8bc1\u7801\uff0c\u70b9\u51fb\u9a8c\u8bc1\uff0c\u8fd4\u56de\u6210\u529f\u6216\u5931\u8d25 \u6bcf\u4e2a\u624b\u673a\u53f7\u6bcf\u5929\u53ea\u80fd\u8f933\u6b21 2\u3001\u5206\u6790 \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 \u968f\u673a\u751f\u62106\u4f4d\u6570\u5b57\u9a8c\u8bc1\u7801\uff1aRandomUtil\uff08hutool\uff09 \u9a8c\u8bc1\u78012\u5206\u949f\u6709\u6548\uff1a\u653e\u5165redis\u91cc\u5e76\u8bbe\u7f6e\u8fc7\u671f\u65f6\u95f42\u5206\u949f \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 3\u3001\u5b9e\u73b0 package cn.ken.blog.controller.common; import cn.hutool.core.date.DateUnit; import cn.hutool.core.date.DateUtil; import cn.hutool.core.util.RandomUtil; import cn.ken.blog.common.constant.Constants; import cn.ken.blog.common.domain.Result; import cn.ken.blog.common.enums.ErrorCodeEnum; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.util.ObjectUtils; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; import java.util.Date; import java.util.concurrent.TimeUnit; \/** &nbsp;* \u9a8c\u8bc1\u7801\u63a7\u5236\u5668 &nbsp;* @author Ken-Chy129 &nbsp;* @date 2022\/4\/17 20:28 &nbsp;*\/ [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-44124","post","type-post","status-publish","format-standard","hentry","category-os"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44124","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=44124"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/44124\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=44124"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=44124"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=44124"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}