{"id":201693,"date":"2025-05-10T09:03:09","date_gmt":"2025-05-10T01:03:09","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201693\/"},"modified":"2025-05-10T09:03:09","modified_gmt":"2025-05-10T01:03:09","slug":"springboot%e6%80%8e%e4%b9%88%e6%95%b4%e5%90%88redis%e4%bd%bf%e7%94%a8cacheable%e5%92%8credistemplate","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201693\/","title":{"rendered":"SpringBoot\u600e\u4e48\u6574\u5408Redis\u4f7f\u7528Cacheable\u548cRedisTemplate"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>SpringBoot\u600e\u4e48\u6574\u5408Redis\u4f7f\u7528Cacheable\u548cRedisTemplate<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u76ee\u524dgolang\u5b66\u4e60\u7f51\u4e0a\u5df2\u7ecf\u6709\u5f88\u591a\u5173\u4e8e<span style=\"color: #FF6600;font-family:\">\u6570\u636e\u5e93<\/span>\u7684\u6587\u7ae0\u4e86\uff0c\u81ea\u5df1\u5728\u521d\u6b21\u9605\u8bfb\u8fd9\u4e9b\u6587\u7ae0\u4e2d\uff0c\u4e5f\u89c1\u8bc6\u5230\u4e86\u5f88\u591a\u5b66\u4e60\u601d\u8def\uff1b\u90a3\u4e48\u672c\u6587<span style=\"color: #FF6600;font-family:\">\u300aSpringBoot\u600e\u4e48\u6574\u5408Redis\u4f7f\u7528Cacheable\u548cRedisTemplate\u300b<\/span>\uff0c\u4e5f\u5e0c\u671b\u80fd\u5e2e\u52a9\u5230\u5927\u5bb6\uff0c\u5982\u679c\u9605\u8bfb\u5b8c\u540e\u771f\u7684\u5bf9\u4f60\u5b66\u4e60<span style=\"color: #FF6600;font-family:\">\u6570\u636e\u5e93<\/span>\u6709\u5e2e\u52a9\uff0c\u6b22\u8fce\u52a8\u52a8\u624b\u6307\uff0c\u8bc4\u8bba\u7559\u8a00\u5e76\u5206\u4eab~<\/p>\n<p>\u5bf9\u4e4b\u524d\u7f51\u7ad9\u505a\u4e86\u4e00\u4e9b\u5f88\u7b80\u5355\u7684\u4f18\u5316\uff0c\u7ed9\u7528\u6237\u5217\u8868\u52a0\u4e86\u4e00\u4e2a\u5206\u9875\u529f\u80fd\u3002<\/p>\n<p>\u5206\u9875\u5c31\u66f4\u597d\u8003\u8651\u52a0\u8f7d\u901f\u5ea6\uff0c\u5982\u679c\u6362\u4e00\u9875\u5c31\u8981\u7b49\u51e0\u79d2\uff0c\u90a3\u4f53\u9a8c\u611f\u662f\u975e\u5e38\u5dee\u7684\u3002<\/p>\n<p>\u56e0\u6b64\u60f3\u5230\u4e86\u52a0\u4e00\u4e2aredis\u7f13\u5b58\u3002<\/p>\n<p>springboot\u6574\u5408redis\u6709\u4e24\u79cd\u65b9\u5f0f\uff1a<\/p>\n<ul>\n<li>\n<p>\u4e00\u3001\u4f7f\u7528\u6ce8\u89e3\uff0c@EnableCaching @Cacheable . . . \u7b49<\/p>\n<\/li>\n<li>\n<p>\u4e8c\u3001\u4f7f\u7528RedisTemplate<\/p>\n<\/li>\n<\/ul>\n<p>\u4e24\u8005\u90fd\u80fd\u64cd\u4f5c\u7f13\u5b58\uff0c\u4f7f\u7528RedisTemplate \u64cd\u4f5c\u80af\u5b9a\u662f\u6bd4\u4f7f\u7528\u6ce8\u89e3\u7075\u6d3b\u3001\u65b9\u4fbf\u3002\u4f46\u662f\u4ece\u7406\u8bba\u4e0a\u6765\u8bb2\u6ce8\u89e3\u65b9\u5f0f\u901f\u5ea6\u5e94\u8be5\u66f4\u5feb\uff0c\u56e0\u4e3a\u4f7f\u7528\u6ce8\u89e3\u5982\u679c\u5728\u7f13\u5b58\u4e2d\u6709\u5c31\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u53d6\uff0c\u4e0d\u7528\u8fdb\u5165\u65b9\u6cd5\u3002\u800cRedisTemplate \u5fc5\u987b\u8fdb\u5165\u65b9\u6cd5\uff0c\u800c\u4e14\u6267\u884c\u5199\u7684\u903b\u8f91\u5224\u65ad\u3002<\/p>\n<p>\u4e0b\u9762\u8bb0\u5f55\u4e00\u4e0b\u6211\u7ed9\u5206\u9875\u505a\u7f13\u5b58\u7684\u601d\u8def\uff0c\u80af\u5b9a\u6709\u5f88\u591a\u4e0d\u597d\u7684\u5730\u65b9\uff0c\u5e0c\u671b\u5927\u5bb6\u53ef\u4ee5\u7ed9\u6211\u6307\u51fa\u3002<\/p>\n<p>\u4e1a\u52a1\u573a\u666f\u662f\u540e\u53f0\u7ba1\u7406\u7cfb\u7edf\uff0c\u4e0d\u7528\u8fc7\u4e8e\u6ce8\u91cd\u5b9e\u65f6\u6570\u636e\u5237\u65b0\uff0c\u5c31\u8bbe\u7f6e\u4e00\u4e2a\u5c0f\u65f6\u8fc7\u671f\u3002<\/p>\n<p>\u6211\u7684\u601d\u8def\u662f\uff1a<\/p>\n<p>&nbsp;\u7b2c\u4e00\u6b21\u52a0\u8f7d\u9875\u9762\uff0c\u5c31\u4ece\u6570\u636e\u5e93\u628a\u524d\u9762\u56db\u9875\u7684\u6570\u636e\u4ece\u6570\u636e\u5e93\u67e5\u8be2\u51fa\u6765\uff0c\u8fd9\u6837\u7b2c\u4e00\u6b21\u7a0d\u5fae\u591a\u7b49\u4e00\u4e0b\uff0c\u540e\u9762\u6362\u9875\u51e0\u4e4e\u4e0d\u7528\u7b49\u5f85\uff0c\u8fd9\u6837\u4f53\u9a8c\u6bd4\u8f83\u597d\u3002\u7136\u540e\u6bcf\u6b21\u6362\u9875\u90fd\u6362\u67e5\u770b\u6709\u6ca1\u6709\u5728\u7f13\u5b58\u4e2d\uff0c\u6ca1\u7528\u5c31\u52a0\u5165\u7f13\u5b58\u3002<\/p>\n<pre>@RequestMapping(\"\/appUser\/{currentPage}\")\npublic&nbsp;R\n   \n    &nbsp;getTableData1(@PathVariable&nbsp;int&nbsp;currentPage)&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/\u7b2c\u4e00\u6b21\u8bf7\u6c42&nbsp;\u524d\u9762\u51e0\u9875\u7528\u5230\u7684\u6982\u7387\u66f4\u5927&nbsp;&nbsp;\u628a\u540e\u9762\u4e09\u9875\u5b58\u5165redis&nbsp;\u51cf\u5c11\u540e\u9762\u5206\u9875\u8bf7\u6c42\u7684\u65f6\u95f4&nbsp;&nbsp;\u4ee5\u540e\u6bcf\u6b21\u52a0\u8f7d\u9875\u9762\u90fd\u628a\u90a3\u9875\u653e\u5165redis\n&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;\u8bbe\u7f6e\u4e00\u4e2a\u5c0f\u65f6\u8fc7\u671f\n&nbsp;&nbsp;&nbsp;&nbsp;Page\n    \n     &nbsp;appUserPage&nbsp;=&nbsp;new&nbsp;Page\n     \n      (currentPage,&nbsp;12); &nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(currentPage&nbsp;==&nbsp;1&nbsp;&amp;&amp;&nbsp;!redisTemplate.hasKey(1))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;1;&nbsp;i&nbsp;&nbsp;redisPage&nbsp;=&nbsp;new&nbsp;Page\n      \n       (i,&nbsp;12); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.opsForValue().set(i,&nbsp;appUserServiceInterface.page(redisPage),&nbsp;1,&nbsp;TimeUnit.HOURS); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(!redisTemplate.hasKey(currentPage))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;redisTemplate.opsForValue().set(currentPage,&nbsp;appUserServiceInterface.page(appUserPage),&nbsp;1,&nbsp;TimeUnit.HOURS); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;R.success((Page\n       \n        )&nbsp;redisTemplate.opsForValue().get(currentPage)); &nbsp;&nbsp;&nbsp;&nbsp;}&nbsp;else&nbsp;if&nbsp;(redisTemplate.hasKey(currentPage))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;R.success((Page\n        \n         )&nbsp;redisTemplate.opsForValue().get(currentPage)); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;R.success(appUserServiceInterface.page(appUserPage)); }\n        \n       \n      \n     \n    \n   <\/pre>\n<p>\u6570\u636e\u7edf\u8ba1\u90a3\u5757\u6211\u53c8\u8bd5\u4e86\u8bd5\u6ce8\u89e3\u3002<\/p>\n<p>\u5148\u8981\u5728\u542f\u52a8\u52a0\u4e0a @EnableCaching\u6ce8\u89e3&nbsp;<\/p>\n<p>\u6ce8\u89e3\u4f7f\u7528\u5c31\u7b80\u5355\uff0c\u5728\u65b9\u6cd5\u4e0a\u52a0\u4e0a@Cacheable \u5c31\u884c\uff0c\u6267\u884c\u65b9\u6cd5\u524d\u4f1a\u67e5\u8be2redis\u7f13\u5b58\u662f\u5426\u6709\u5bf9\u5e94\u7684key\uff0c\u6709\u5c31\u76f4\u63a5\u53d6\u503c\uff0c\u6ca1\u6709\u5c31\u6267\u884c\u65b9\u6cd5\u3002<\/p>\n<p>value = &#8220;appUserData&#8221; \u662f\u7f13\u5b58\u533a\u7684\u540d\u5b57 \uff0c key\u662f\u952e\u7684\u540d\u5b57 \u3002<\/p>\n<p>\u4ee5\u4e0b\u7684\u952e\u503c\u5c31\u662f appUserData : : userArea<\/p>\n<pre>@RequestMapping&nbsp;(\"\/userArea\")\n@Cacheable(value&nbsp;=&nbsp;\"appUserData\",key&nbsp;=\"'userArea'\")\npublic&nbsp;R\n   \n    &nbsp;area()&nbsp;{\n&nbsp;&nbsp;&nbsp;&nbsp;List\n    \n     &nbsp;userList&nbsp;=&nbsp;appUserServiceInterface.list(); &nbsp;&nbsp;&nbsp;&nbsp;List\n     \n      &nbsp;areaList&nbsp;=&nbsp;new&nbsp;ArrayList(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(AppUser&nbsp;appUser&nbsp;:&nbsp;userList)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;areaList.add(appUser.getArea()); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u653e\u5165map\u8bb0\u5f55\u6bcf\u4e2a\u6708\u4efd\u51fa\u73b0\u7684\u6b21\u6570 &nbsp;&nbsp;&nbsp;&nbsp;Map\n      \n       &nbsp;areaTimes&nbsp;=&nbsp;new&nbsp;HashMap(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(String&nbsp;s&nbsp;:&nbsp;areaList)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(!areaTimes.containsKey(s))&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;areaTimes.put(s,&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}else&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;areaTimes.put(s,&nbsp;areaTimes.get(s)&nbsp;+&nbsp;1); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u6392\u5e8f &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u81ea\u5b9a\u4e49\u6bd4\u8f83\u5668 &nbsp;&nbsp;&nbsp;&nbsp;Comparator\n       \n        &gt;&nbsp;valCmp&nbsp;=&nbsp;new&nbsp;Comparator\n        \n         &gt;()&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@Override &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;public&nbsp;int&nbsp;compare(Map.Entry\n         \n          &nbsp;o1,&nbsp;Map.Entry\n          \n           &nbsp;o2)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;\/\/&nbsp;TODO&nbsp;Auto-generated&nbsp;method&nbsp;stub &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;o2.getValue()&nbsp;-&nbsp;o1.getValue();&nbsp;&nbsp;\/\/&nbsp;\u964d\u5e8f\u6392\u5e8f\uff0c\u5982\u679c\u60f3\u5347\u5e8f\u5c31\u53cd\u8fc7\u6765 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;}; &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u5c06map\u8f6c\u6210List\uff0cmap\u7684\u4e00\u7ec4key\uff0cvalue\u5bf9\u5e94list\u4e00\u4e2a\u5b58\u50a8\u7a7a\u95f4 &nbsp;&nbsp;&nbsp;&nbsp;List\n           \n            &gt;&nbsp;mapList&nbsp;=&nbsp;new&nbsp;ArrayList\n            \n             &gt;(areaTimes.entrySet());&nbsp;\/\/\u4f20\u5165maps\u5b9e\u4f53 &nbsp;&nbsp;&nbsp;&nbsp;Collections.sort(mapList,&nbsp;valCmp); &nbsp;&nbsp;&nbsp;&nbsp;\/\/\u53d6\u524d8 &nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;len&nbsp;=&nbsp;mapList.size(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(int&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;&nbsp;resMap&nbsp;=&nbsp;new&nbsp;HashMap(); &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(Map.Entry\n             \n              &nbsp;m&nbsp;:&nbsp;mapList)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resMap.put(m.getKey(),&nbsp;m.getValue().toString()); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;R.success(resMap); }\n             \n            \n           \n          \n         \n        \n       \n      \n     \n    \n   <\/pre>\n<p>\u5176\u4ed6\u6ce8\u89e3\uff1a<\/p>\n<ul>\n<li>\n<p>@CachePut<\/p>\n<\/li>\n<li>\n<p>@Caching<\/p>\n<\/li>\n<li>\n<p>@CacheEvict<\/p>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>SpringBoot\u600e\u4e48\u6574\u5408Re&#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-201693","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201693","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=201693"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201693\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201693"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201693"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201693"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}