{"id":201677,"date":"2025-05-10T09:12:32","date_gmt":"2025-05-10T01:12:32","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201677\/"},"modified":"2025-05-10T09:12:32","modified_gmt":"2025-05-10T01:12:32","slug":"%e5%a6%82%e4%bd%95%e5%9c%a8spring-boot%e4%b8%ad%e6%95%b4%e5%90%88redis%e7%bc%93%e5%ad%98%e5%b9%b6%e7%94%9f%e6%88%90%e9%aa%8c%e8%af%81%e7%a0%81","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201677\/","title":{"rendered":"\u5982\u4f55\u5728Spring Boot\u4e2d\u6574\u5408Redis\u7f13\u5b58\u5e76\u751f\u6210\u9a8c\u8bc1\u7801"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>\u5982\u4f55\u5728Spring Boot\u4e2d\u6574\u5408Redis\u7f13\u5b58\u5e76\u751f\u6210\u9a8c\u8bc1\u7801<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u77e5\u8bc6\u70b9\u638c\u63e1\u4e86\uff0c\u8fd8\u9700\u8981\u4e0d\u65ad\u7ec3\u4e60\u624d\u80fd\u719f\u7ec3\u8fd0\u7528\u3002\u4e0b\u9762golang\u5b66\u4e60\u7f51\u7ed9\u5927\u5bb6\u5e26\u6765\u4e00\u4e2a\u6570\u636e\u5e93\u5f00\u53d1\u5b9e\u6218\uff0c\u624b\u628a\u624b\u6559\u5927\u5bb6\u5b66\u4e60\u300a\u5982\u4f55\u5728Spring Boot\u4e2d\u6574\u5408Redis\u7f13\u5b58\u5e76\u751f\u6210\u9a8c\u8bc1\u7801\u300b\uff0c\u5728\u5b9e\u73b0\u529f\u80fd\u7684\u8fc7\u7a0b\u4e2d\u4e5f\u5e26\u5927\u5bb6\u91cd\u65b0\u6e29\u4e60\u76f8\u5173\u77e5\u8bc6\u70b9\uff0c\u6e29\u6545\u800c\u77e5\u65b0\uff0c\u56de\u5934\u770b\u770b\u8bf4\u4e0d\u5b9a\u53c8\u6709\u4e0d\u4e00\u6837\u7684\u611f\u609f\uff01<\/p>\n<h3>1\u3001\u7b80\u4ecb<\/h3>\n<p>Redis is an open source (BSD licensed), in-memory data structure store, used as a database, cache, and message broker.<\/p>\n<p>\u7ffb\u8bd1\uff1aRedis \u662f\u4e00\u4e2a\u5f00\u6e90\u7684\u5185\u5b58\u4e2d\u7684\u6570\u636e\u7ed3\u6784\u5b58\u50a8\u7cfb\u7edf\uff0c\u5b83\u53ef\u4ee5\u7528\u4f5c\uff1a\u6570\u636e\u5e93\u3001\u7f13\u5b58\u548c\u6d88\u606f\u4e2d\u95f4\u4ef6\u3002<\/p>\n<p>Redis \u662f\u7528 C \u8bed\u8a00\u5f00\u53d1\u7684\u4e00\u4e2a\u5f00\u6e90\u7684\u9ad8\u6027\u80fd\u952e\u503c\u5bf9\uff08key-value\uff09\u6570\u636e\u5e93\uff0c\u5b98\u65b9\u63d0\u4f9b\u7684\u6570\u636e\u662f\u53ef\u4ee5\u8fbe\u5230 **100000+**\u7684 QPS\u3002<\/p>\n<p>QPS\uff08Queries-per-second\uff09\uff0c\u6bcf\u79d2\u5185\u67e5\u8be2\u6b21\u6570\u3002\uff08\u767e\u5ea6\u767e\u79d1\uff09<\/p>\n<p>\u5b83\u5b58\u50a8\u7684 value \u7c7b\u578b\u6bd4\u8f83\u4e30\u5bcc\uff0c\u4e5f\u88ab\u79f0\u4e3a\u7ed3\u6784\u5316\u7684 NoSQL \u6570\u636e\u5e93\u3002<\/p>\n<p>NoSQL\uff08Not only SQL\uff09\uff0c\u4e0d\u4ec5\u4ec5\u662f SQL\uff0c\u6cdb\u6307\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\u3002<\/p>\n<p>NoSQL \u6570\u636e\u5e93\u5e76\u4e0d\u662f\u8981\u53d6\u4ee3\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff0c\u800c\u662f\u5173\u7cfb\u578b\u6570\u636e\u5e93\u7684\u8865\u5145\u3002<\/p>\n<p>\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08RDBMS\uff09<\/p>\n<ul>\n<li>\n<p>MySQL<\/p>\n<\/li>\n<li>\n<p>Oracle<\/p>\n<\/li>\n<li>\n<p>DB2<\/p>\n<\/li>\n<li>\n<p>SQL Server<\/p>\n<\/li>\n<\/ul>\n<p>\u975e\u5173\u7cfb\u578b\u6570\u636e\u5e93\uff08NoSQL\uff09<\/p>\n<ul>\n<li>\n<p>Redis<\/p>\n<\/li>\n<li>\n<p>Mongo db<\/p>\n<\/li>\n<li>\n<p>MemCached<\/p>\n<\/li>\n<\/ul>\n<p>Redis \u5e94\u7528\u573a\u666f<\/p>\n<ul>\n<li>\n<p>\u7f13\u5b58<\/p>\n<\/li>\n<li>\n<p>\u4efb\u52a1\u961f\u5217<\/p>\n<\/li>\n<li>\n<p>\u6d88\u606f\u961f\u5217<\/p>\n<\/li>\n<li>\n<p>\u5206\u5e03\u5f0f\u9501<\/p>\n<\/li>\n<\/ul>\n<h3>2\u3001\u4ecb\u7ecd<\/h3>\n<p>Redis \u7684 Java \u5ba2\u6237\u7aef\u5f88\u591a\uff0c\u5b98\u65b9\u63a8\u8350\u7684\u6709\u4e09\u79cd\uff1aJedis\u3001Lettuce\u3001Redisson\u3002<\/p>\n<p>Spring \u5bf9 Redis \u5ba2\u6237\u7aef\u8fdb\u884c\u4e86\u6574\u5408\uff0c\u63d0\u4f9b\u4e86 Spring Data Redis\u3002<\/p>\n<p>\u5728 Spring Boot \u9879\u76ee\u4e2d\u8fd8\u63d0\u4f9b\u4e86\u5bf9\u5e94\u7684 Starter\uff0c\u5373 spring-boot-starter-data-redis\u3002<\/p>\n<p>\u5728\u8fd9\u91cc\u76f4\u63a5\u4f7f\u7528\u7684\u662fSpring Data Redis\uff0c\u4e14\u4e0d\u5c55\u793aRedis\u7684\u4e0b\u8f7d\u548c\u5b89\u88c5\u8fc7\u7a0b\u5566\u3002<\/p>\n<h3>3\u3001\u524d\u671f\u914d\u7f6e<\/h3>\n<h4>3.1\u3001\u5750\u6807\u5bfc\u5165<\/h4>\n<p>\u5728\u521b\u5efa\u5b8c\u6210Spring Boot\u9879\u76ee\u4e4b\u540e\uff0c\u5728pom.xml\u4e2d\u52a0\u5165<code>spring-boot-start-data-redis<\/code>\u7684\u4f9d\u8d56\u5750\u6807<\/p>\n<pre>\n   \n\n   \n    \n&nbsp;&nbsp;&nbsp;&nbsp;\n    \n     org.springframework.boot\n    \n&nbsp;&nbsp;&nbsp;&nbsp;\n    \n     spring-boot-starter-data-redis\n    \n   <\/pre>\n<h4>3.2\u3001\u914d\u7f6e\u6587\u4ef6<\/h4>\n<p>\u8fd9\u91cc\u5ffd\u7565\u4e86\u5176\u4ed6\u7684\u914d\u7f6e\u5982MySQL\u8fd9\u4e9b\uff0c\u53ea\u7a81\u51faRedis\u7684\u914d\u7f6e\u4fe1\u606f<\/p>\n<blockquote>\n<p>spring:&nbsp;<br \/>&nbsp; # Redis\u914d\u7f6e<br \/>&nbsp; redis:<br \/>&nbsp; &nbsp; host: localhost<br \/>&nbsp; &nbsp; port: 6379<br \/>&nbsp; &nbsp; # \u6839\u636e\u81ea\u5df1\u8bbe\u7f6e\u7684\u5bc6\u7801\u51b3\u5b9a<br \/>&nbsp; &nbsp; password: 123456<br \/>&nbsp; &nbsp; # \u64cd\u4f5c0\u53f7\u6570\u636e\u5e93\uff0c\u9ed8\u8ba4\u670916\u4e2a\u6570\u636e\u5e93<br \/>&nbsp; &nbsp; database: 0<br \/>&nbsp; &nbsp; jedis:<br \/>&nbsp; &nbsp; &nbsp; pool:<br \/>&nbsp; &nbsp; &nbsp; &nbsp; max-active: 8 # \u6700\u5927\u8fde\u63a5\u6570<br \/>&nbsp; &nbsp; &nbsp; &nbsp; max-wait: 1ms # \u8fde\u63a5\u6c60\u6700\u5927\u963b\u585e\u7b49\u5f85\u65f6\u95f4<br \/>&nbsp; &nbsp; &nbsp; &nbsp; max-idle: 4 &nbsp; # \u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5927\u7a7a\u95f2\u8fde\u63a5<br \/>&nbsp; &nbsp; &nbsp; &nbsp; min-idle: 0 &nbsp; # \u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5<\/p>\n<\/blockquote>\n<h4>3.3\u3001\u914d\u7f6e\u7c7b<\/h4>\n<p>\u4f7f\u7528 springboot \u6574\u5408 redis \u7684\u4e13\u7528\u5ba2\u6237\u7aef\u63a5\u53e3\u64cd\u4f5c\u3002\u6b64\u5904\u4f7f\u7528\u7684\u662f SpringBoot \u6846\u67b6\u63d0\u4f9b\u7684 RedisTemplate\u3002<\/p>\n<p>RedisTemplate \u5728\u5904\u7406 key \u548c value \u65f6\uff0c\u4f1a\u5bf9\u5176\u8fdb\u884c\u5e8f\u5217\u5316\u64cd\u4f5c\uff0c\u8fd9\u5c31\u4f1a\u5bfc\u81f4\u4e00\u4e9b\u95ee\u9898\u3002<\/p>\n<p>\u6bd4\u5982\uff1a\u8f93\u5165\u7684 key \u503c\u662f <code>city<\/code>\uff0c\u4f46 redis \u83b7\u5f97\u5230\u7684 key \u503c\u5374\u662f <code>\\xac]xed\\x00\\x05t\\x00\\x04city<\/code>\u3002<\/p>\n<p>\u56e0\u6b64\u5c31\u9700\u8981\u4e00\u4e2a\u4e13\u95e8\u7684\u914d\u7f6e\u7c7b\uff0c\u6765\u4e13\u95e8\u5904\u7406 RedisTemplate \u9ed8\u8ba4\u7684\u5e8f\u5217\u5316\u5904\u7406\u6240\u5bfc\u81f4\u7684\u95ee\u9898\u3002<\/p>\n<p>\u503c\u5f97\u6ce8\u610f\u7684\u662f\uff0c\u8fd9\u91cc\u662f\u91cd\u65b0\u52a0\u8f7d\u4e00\u4e2a\u65b0\u7684\u5e8f\u5217\u5316\u5668\u6765\u53d6\u4ee3\u539f\u6765\u7684\u5e8f\u5217\u5316\u5668\uff0c\u8fd9\u5c31\u8bc1\u660e\u7740\u5176\u539f\u672c\u662f\u6709\u7740\u81ea\u5df1\u9ed8\u8ba4\u7684\u5e8f\u5217\u5316\u5668JdkSerializationRedisSerializer\u3002<\/p>\n<pre>\/**\n&nbsp;*&nbsp;@classname&nbsp;RedisConfig\n&nbsp;*&nbsp;@description&nbsp;Redis\u914d\u7f6e\u7c7b\uff0c\u66f4\u6362key\u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u5668\n&nbsp;*&nbsp;@author&nbsp;xBaozi\n&nbsp;*&nbsp;@date&nbsp;19:04&nbsp;2022\/7\/2\n&nbsp;**\/\n@Configuration\npublic&nbsp;class&nbsp;RedisConfig&nbsp;extends&nbsp;CachingConfigurerSupport&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;@Bean\n&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;RedisTemplate&nbsp;redisTemplate(RedisConnectionFactory&nbsp;connectionFactory)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;RedisTemplate&nbsp;redisTemplate&nbsp;=&nbsp;new&nbsp;RedisTemplate(); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.setKeySerializer(new&nbsp;StringRedisSerializer()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.setHashKeySerializer(new&nbsp;StringRedisSerializer()); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.setConnectionFactory(connectionFactory); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;redisTemplate; &nbsp;&nbsp;&nbsp;&nbsp;} }<\/pre>\n<h3>4\u3001Java\u64cd\u4f5cRedis<\/h3>\n<p>\u5728\u8fd9\u91cc\u7531\u4e8e\u4ee3\u7801\u6bd4\u8f83\u9690\u79c1\uff08\u4e3b\u8981\u662f\u61d2\u5f97\u6574\u7406\u767b\u5f55\u548c\u751f\u6210\u9a8c\u8bc1\u7801\u7684\u63a5\u53e3\u653e\u5728\u8fd9\u91cc\uff09\uff0c\u56e0\u6b64\u8fd9\u91cc\u5c31\u76f4\u63a5\u4f7f\u7528\u6d4b\u8bd5\u7c7b\u8fdb\u884c\u6f14\u793a\u3002<\/p>\n<p>\u53d1\u9001\u9a8c\u8bc1\u7801<\/p>\n<pre>@PostMapping(\"\/sendMsg\")\npublic&nbsp;Result\n   \n    &nbsp;sendMsg(@RequestBody&nbsp;User&nbsp;user,&nbsp;HttpSession&nbsp;session)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u83b7\u53d6\u9700\u8981\u53d1\u9001\u77ed\u4fe1\u7684\u624b\u673a\u53f7\n&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;userPhone&nbsp;=&nbsp;user.getPhone();\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(StringUtils.isNotEmpty(userPhone))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u968f\u673a\u751f\u62104\u4f4d\u9a8c\u8bc1\u7801\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;checkCode&nbsp;=&nbsp;ValidateCodeUtils.generateValidateCode4String(4);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5c06\u751f\u6210\u7684\u9a8c\u8bc1\u7801\u4fdd\u5b58\u5230Redis\u4e2d\u5e76\u8bbe\u7f6e\u6709\u6548\u671f\u4e94\u5206\u949f\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.opsForValue().set(userPhone,&nbsp;checkCode,&nbsp;5,&nbsp;TimeUnit.MINUTES);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Result.success(checkCode);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Result.error(\"\u77ed\u4fe1\u53d1\u9001\u9519\u8bef\");\n}\n   <\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230507\/168343115764571ef5a9189.png\" class=\"aligncenter\"><\/p>\n<p>\u8f93\u5165\u9a8c\u8bc1\u7801\u767b\u5f55<\/p>\n<pre>@PostMapping(\"\/login\")\npublic&nbsp;Result\n   \n    &nbsp;login(@RequestBody&nbsp;Map&nbsp;map,&nbsp;HttpSession&nbsp;session)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;log.info(\"map:&nbsp;{}\",&nbsp;map);\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u83b7\u53d6\u7528\u6237\u8f93\u5165\u4fe1\u606f\n&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;phone&nbsp;=&nbsp;(String)map.get(\"phone\");\n&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;code&nbsp;=&nbsp;(String)map.get(\"code\");\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u4eceRedis\u4e2d\u53d6\u51fa\u9a8c\u8bc1\u7801\n&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;checkCode&nbsp;=&nbsp;redisTemplate.opsForValue().get(phone);\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u6bd4\u5bf9\u9a8c\u8bc1\u7801\u662f\u5426\u4e00\u81f4\n&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(StringUtils.isNotEmpty(checkCode)&nbsp;&amp;&amp;&nbsp;checkCode.equals(code.toLowerCase()))&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5c06\u7528\u6237id\u5b58\u653e\u5230session\u4e2d\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.setAttribute(\"user\",&nbsp;user.getId());\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u767b\u5f55\u6210\u529f\uff0c\u5220\u9664Redis\u4e2d\u7684\u9a8c\u8bc1\u7801\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.delete(phone);\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u5c06\u7528\u6237\u4fe1\u606f\u8fd4\u56de\u5230\u524d\u7aef\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Result.success(user);\n&nbsp;&nbsp;&nbsp;&nbsp;}\n&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;Result.error(\"\u767b\u5f55\u5931\u8d25\uff0c\u8bf7\u68c0\u67e5\u624b\u673a\u53f7\u6216\u9a8c\u8bc1\u7801\u662f\u5426\u6b63\u786e\");\n}\n   <\/pre>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230507\/168343115764571ef5b30aa.png\" class=\"aligncenter\"><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5982\u4f55\u5728Spring Boot\u4e2d\u6574&#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-201677","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201677","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=201677"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201677\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201677"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201677"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201677"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}