{"id":201253,"date":"2025-05-10T09:04:53","date_gmt":"2025-05-10T01:04:53","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201253\/"},"modified":"2025-05-10T09:04:53","modified_gmt":"2025-05-10T01:04:53","slug":"springboot-%e5%bc%80%e5%90%afredis%e7%bc%93%e5%ad%98%e5%8f%8a%e4%bd%bf%e7%94%a8%e6%96%b9%e6%b3%95","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201253\/","title":{"rendered":"SpringBoot \u5f00\u542fRedis\u7f13\u5b58\u53ca\u4f7f\u7528\u65b9\u6cd5"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>SpringBoot \u5f00\u542fRedis\u7f13\u5b58\u53ca\u4f7f\u7528\u65b9\u6cd5<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u672c\u7bc7\u6587\u7ae0\u5411\u5927\u5bb6\u4ecb\u7ecd\u300aSpringBoot \u5f00\u542fRedis\u7f13\u5b58\u53ca\u4f7f\u7528\u65b9\u6cd5\u300b\uff0c\u4e3b\u8981\u5305\u62ec\u7f13\u5b58\u3001SpringBootRedis\uff0c\u5177\u6709\u4e00\u5b9a\u7684\u53c2\u8003\u4ef7\u503c\uff0c\u9700\u8981\u7684\u670b\u53cb\u53ef\u4ee5\u53c2\u8003\u4e00\u4e0b\u3002<\/p>\n<blockquote>\n<p>\u4e4b\u524d\u4e0d\u662f\u8bf4\u8fc7Redis\u53ef\u4ee5\u5f53\u4f5c\u7f13\u5b58\u7528\u561b<br \/> \u73b0\u5728\u6211\u4eec\u5c31\u914d\u7f6e\u4e00\u4e0bSpringBoot\u4f7f\u7528Redis\u7684\u7f13\u5b58<\/p>\n<\/blockquote>\n<h2>Redis\u7f13\u5b58<\/h2>\n<p><strong>\u4e3a\u4ec0\u4e48\u7528Redis\u4f5c\u7f13\u5b58<\/strong><br \/> \u7528redis\u505a\u7f13\u5b58\uff0c\u662f\u56e0\u4e3aredis\u6709\u7740\u5f88\u4f18\u79c0\u7684\u8bfb\u5199\u80fd\u529b\uff0c\u5728\u96c6\u7fa4\u4e0b\u53ef\u4ee5\u4fdd\u8bc1\u6570\u636e\u7684\u9ad8\u53ef\u7528<\/p>\n<h2>\u4e3b\u8981\u6b65\u9aa4<\/h2>\n<p>&nbsp;1\u3001pom.xml\u6587\u4ef6\u6dfb\u52a0\u4f9d\u8d56<\/p>\n<p>2\u3001yml\u6587\u4ef6\u914d\u7f6eredis\u96c6\u7fa4<\/p>\n<p>3\u3001\u7f16\u5199RedisConfig\u914d\u7f6e\u5e8f\u5217\u5316\u53ca\u7f13\u5b58\u914d\u7f6e\uff0c\u6dfb\u52a0\u7f13\u5b58\u6ce8\u89e3<\/p>\n<p>4\u3001\u7f16\u5199\u4e1a\u52a1Controller\uff0c\u6dfb\u52a0\u7f13\u5b58\u6ce8\u89e3<\/p>\n<p>5\u3001\u7f16\u5199\u542f\u52a8\u7c7b<\/p>\n<h2>\u5177\u4f53\u5b9e\u8df5<\/h2>\n<h3>\u6574\u4f53\u76ee\u5f55\u7ed3\u6784<\/h3>\n<p>pom.xml\u6dfb\u52a0\u4f9d\u8d56<\/p>\n<pre>\n\n\n   \n    \n     4.0.0\n    \n    \n     org.example\n    \n    \n     SpringBoot_Redis\n    \n    \n     1.0-SNAPSHOT\n    \n    \n     \n      1.8\n     \n    \n    \n     \n      org.springframework.boot\n     \n     \n      spring-boot-starter-parent\n     \n     \n      2.1.8.RELEASE\n     \n    \n    \n     \n      \n       org.springframework.boot\n      \n      \n       spring-boot-starter-web\n      \n     \n     \n     \n      \n       org.springframework.boot\n      \n      \n       spring-boot-starter-data-redis\n      \n     \n     \n     \n      \n       org.springframework.boot\n      \n      \n       spring-boot-starter-test\n      \n     \n    \n   <\/pre>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575663b0decccbcfe.png\" class=\"aligncenter\"><\/p>\n<h3>yml\u6587\u4ef6\u91cc\u914d\u7f6eRedis\u96c6\u7fa4<\/h3>\n<p>\u7ed3\u6784\u662fip+port<\/p>\n<pre>\nspring:\n  redis:\n    cluster:\n      nodes:\n        - 169.254.0.100:8001\n        - 169.254.0.100:8002\n        - 169.254.0.100:8003\n        - 169.254.0.100:8004\n        - 169.254.0.100:8005\n        - 169.254.0.100:8006<\/pre>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575763b0decd12102.png\" class=\"aligncenter\"><\/p>\n<p>\u7f16\u5199RedisConfig\u914d\u7f6e\u5e8f\u5217\u5316\u53ca\u7f13\u5b58\u914d\u7f6e\uff0c\u6dfb\u52a0\u7f13\u5b58\u6ce8\u89e3<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575763b0decd4b5b1.png\" class=\"aligncenter\"><\/p>\n<h3>\u8bbe\u7f6e\u5e8f\u5217\u5316\u7684Bean<br \/><\/h3>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575763b0decd80246.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8bbe\u7f6e\u7f13\u5b58\u7684Bean<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575763b0decdb7888.jpg\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u91cc\u6709\u5fc5\u8981\u89e3\u91ca\u4e00\u4e0b<\/p>\n<ul>\n<li>cacheNames.add() \u8fd9\u91cc\u6211\u7406\u89e3\u7684\u662f\u548ccontroller\u8fdb\u884c\u7ed1\u5b9a\uff0c\u6bd5\u7adf\u5f88\u591acontroller\u7684\u65f6\u5019\uff0c\u8fd9\u91cc\u53ef\u4ee5\u786e\u5b9a\u5230\u5e95\u90a3\u4e2acontroller\u5f00\u542f\u7f13\u5b58\uff0c\u4ee5\u53ca\u6bcf\u4e2acontroller\u5bf9\u7f13\u5b58\u7684\u8981\u6c42\u53ef\u80fd\u4e5f\u4e0d\u4e00\u6837<\/li>\n<li>configMap\u8fd9\u91cc\u5c31\u662f\u5c06\u6211\u4eec\u5bf9\u7f13\u5b58\u7684\u4e00\u4e9b\u914d\u7f6e\u548c\u547d\u540d\u7a7a\u95f4\u8fdb\u884c\u5173\u8054<\/li>\n<li>\u8bbe\u7f6e\u7f13\u5b58\u65f6\u95f4\u548c\u7981\u6b62\u7f13\u5b58\u7a7a\u6570\u636e\u5e94\u8be5\u8fd8\u597d\u7406\u89e3<\/li>\n<\/ul>\n<h3>\u7f16\u5199\u4e1a\u52a1Controller<\/h3>\n<pre>\n@RestController\n@RequestMapping(\"user\")\npublic class RedisCacheController {\n    @Cacheable(value = \"user\",key = \"#root.methodName+#root.args[0]\")\n    @GetMapping(\"findWord\/{id}\")\n    public String findWord(@PathVariable String id) {\n        System.out.println(\"Cacheing\");\n        HashMap\n   \n     words = new HashMap();\n        words.put(\"1\", \"java\");\n        words.put(\"2\", \"redis\");\n        words.put(\"3\", \"cache\");\n        return words.get(id);\n    }\n}\n   <\/pre>\n<p>@Cacheable\u4e00\u5b9a\u8981\u52a0\u5728\u65b9\u6cd5\u4e4b\u4e0a<br \/> value\u5c31\u662f\u4e4b\u524d\u5728RedisConfig\u4e2d\u5b9a\u4e49\u7684\u547d\u540d\u7a7a\u95f4\uff0c\u4e5f\u662f\u7f13\u5b58\u4fdd\u5b58\u7684\u7a7a\u95f4<br \/> key\u5c31\u662f\u7f13\u5b58\u4fdd\u5b58\u7684key,\u8fd9\u91cc\u4ee5\u65b9\u6cd5\u540d\u4e3akey,\u4f46\u662f\u4e3a\u907f\u514d\u65b9\u6cd5\u540d\u91cd\u590d\u5bfc\u81f4\u7684key\u91cd\u590d\uff0c\u6240\u4ee5\u52a0\u5165id,\u6765\u907f\u514d\u91cd\u590d<\/p>\n<h3>\u5173\u4e8e\u7f13\u5b58\u7684\u5176\u4ed6\u6ce8\u89e3<\/h3>\n<ul>\n<li>@CachePut<\/li>\n<\/ul>\n<p>\u5728\u652f\u6301Spring Cache\u7684\u73af\u5883\u4e2d\uff0c\u5bf9\u4e8e\u4f7f\u7528@Cacheale\u6807\u6ce8\u7684\u65b9\u6cd5\uff0cSpring\u5728\u6bcf\u6b21\u6267\u884c\u524d\u90fd\u4f1a\u68c0\u67e5Cache\u4e2d\u662f\u5426\u5b58\u5728\u76f8\u540c\u7684key\u7684\u7f13\u5b58\u5143\u7d20\uff0c\u5982\u679c\u5b58\u5728\u5c31\u4e0d\u518d\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u800c\u662f\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7ed3\u679c\u76f4\u63a5\u8fdb\u884c\u8fd4\u56de\uff0c\u82e5\u4e0d\u5b58\u5728\u624d\u4f1a\u6267\u884c\u65b9\u6cd5\u5e76\u5c06\u8fd4\u56de\u7ed3\u679c\u5b58\u5165\u6307\u5b9a\u7684\u7f13\u5b58\u4e2d<\/p>\n<p>@Cacheput\u4e5f\u53ef\u4ee5\u751f\u547d\u4e00\u4e2a\u65b9\u6cd5\u652f\u6301\u7f13\u5b58\u529f\u80fd\uff0c\u4e0e@Cacheable\u4e0d\u540c\u7684\u662f\u4f7f\u7528@CachePut\u6807\u6ce8\u7684\u65b9\u6cd5\u5728\u6267\u884c\u5e76\u4e0d\u4f1a\u53bb\u68c0\u67e5\u7f13\u5b58\u4e2d\u662f\u5426\u5b58\u5728\u4e4b\u524d\u6267\u884c\u8fc7\u7684\u7ed3\u679c\uff0c\u800c\u662f\u6bcf\u6b21\u90fd\u4f1a\u6267\u884c\u8be5\u65b9\u6cd5\uff0c\u5e76\u5c06\u6267\u884c\u7ed3\u679c\u4ee5\u952e\u503c\u5bf9\u7684\u5f62\u5f0f\u5b58\u5165\u7f13\u5b58\u4e2d\u3002<\/p>\n<ul>\n<li>@CacheEvict<\/li>\n<\/ul>\n<p>CacheEvict\u662f\u7528\u6765\u6807\u6ce8\u5728\u9700\u8981\u6e05\u9664\u7f13\u5b58\u5143\u7d20\u7684\u65b9\u6cd5\u6216\u7c7b\u4e0a\u7684\u3002<br \/> \u5f53\u6807\u8bb0\u5728\u4e00\u4e2a\u7c7b\u4e0a\u65f6\u8868\u793a\u5176\u4e2d\u6240\u6709\u7684\u65b9\u6cd5\u7684\u6267\u884c\u90fd\u4f1a\u89e6\u53d1\u7f13\u5b58\u7684\u6e05\u9664\u64cd\u4f5c<br \/> @CacheEvict\u53ef\u4ee5\u6307\u5b9a\u7684\u5c5e\u6027\u6709<strong>value\u3001key\u3001condition\u3001allEntries\u548cbeforeInvocation\u3002<\/strong><br \/> \u5176\u4e2dvalue\u3001key\u548ccondition\u7684\u8bed\u4e49\u4e0e@Cacheable\u5bf9\u5e94\u7684\u5c5e\u6027\u7c7b\u4f3c\u3002\u5373value\u8868\u793a\u6e05\u9664\u64cd\u4f5c\u662f\u53d1\u751f\u5728\u54ea\u4e9b\u7f13\u5b58\uff08\u547d\u540d\u7a7a\u95f4\uff09\u4e0a\u7684<br \/> key\u8868\u793a\u8981\u6e05\u9664\u7684\u662f\u54ea\u4e2akey,\u5982\u672a\u6307\u5b9a\u5219\u4f1a\u8c01\u7528\u9ed8\u8ba4\u7b56\u7565\u751f\u6210\u7684key,condition\u8868\u793a\u6e05\u9664\u64cd\u4f5c\u53d1\u751f\u7684\u6761\u4ef6<br \/><strong>allEntries\u5c5e\u6027<\/strong><br \/> allEntries\u662fboolean\u7c7b\u578b\u7684\uff0c\u8868\u793a\u662f\u5426\u8981\u6e05\u9664\u7f13\u5b58\u4e2d\u7684\u6240\u6709\u5143\u7d20\uff0c\u9ed8\u8ba4\u4e3afalse,\u5f53\u6307\u5b9a\u4e3atrue\u65f6\uff0c\u4f1a\u5ffd\u7565\u6307\u5b9a\u7684key<br \/><strong>beforeInvocation\u5c5e\u6027<\/strong><br \/> \u6e05\u9664\u64cd\u4f5c\u9ed8\u8ba4\u65f6\u5728\u5bf9\u5e94\u65b9\u6cd5\u6210\u529f\u6267\u884c\u540e\u89e6\u53d1\u7684\uff0c\u5373\u65b9\u6cd5\u5982\u679c\u56e0\u4e3a\u629b\u51fa\u5f02\u5e38\u800c\u672a\u80fd\u6210\u529f\u8fd4\u56de\u4e5f\u4e0d\u4f1a\u89e6\u53d1\u6e05\u9664\u64cd\u4f5c<br \/> \u4f7f\u7528beforeInvocation\u53ef\u4ee5\u6539\u53d8\u89e6\u53d1\u6e05\u9664\u64cd\u4f5c\u7684\u65f6\u95f4\uff0c\u5f53\u6211\u4eec\u6307\u5b9a\u5c5e\u6027\u503c\u4e3atrue\u65f6\uff0cSpring\u4f1a\u5728\u8c03\u7528\u8be5\u65b9\u6cd5\u4e4b\u524d\u6e05\u9664\u7f13\u5b58\u4e2d\u7684\u6307\u5b9a\u5143\u7d20 \u7f16\u5199\u542f\u52a8\u7c7b<\/p>\n<p>\u5c31\u662f\u4f20\u7edf\u7684\u542f\u52a8\u7c7b<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575863b0dece05100.png\" class=\"aligncenter\"><\/p>\n<h2>\u68c0\u9a8c\u7ed3\u679c<\/h2>\n<p>\u4f7f\u7528postman\u53d1\u9001\u8bf7\u6c42\u8fdb\u884c\u68c0\u6d4b<br \/> \u7b2c\u4e00\u6b21\u7684\u65f6\u5019\u53ef\u4ee5\u770b\u5230\u63a7\u5236\u53f0\u6253\u5370<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575863b0dece33bc0.png\" class=\"aligncenter\"><\/p>\n<p>\u8fd9\u8bf4\u660e\u65b9\u6cd5\u6267\u884c\u4e86<br \/> \u4f46\u662f\u7b2c\u4e8c\u6b21\u53d1\u9001\u76f8\u540c\u8bf7\u6c42\u7684\u65f6\u5019\uff0c\u53ef\u4ee5\u770b\u5230\u62ff\u5230\u4e86\u6570\u636e\uff0c\u4f46\u662f\u65b9\u6cd5\u6ca1\u6709\u6267\u884c\uff0c\u8bf4\u660e\u7f13\u5b58\u6709\u7528\u4e86<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575863b0dece5c870.png\" class=\"aligncenter\"><br \/><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20230101\/167253575863b0dece9018d.png\" class=\"aligncenter\"><\/p>\n<p>\u597d\u4e86\uff0c\u5230\u6b64\u7ed3\u675f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot \u5f00\u542fRed&#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-201253","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201253","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=201253"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201253\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201253"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201253"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201253"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}