{"id":715,"date":"2025-02-19T21:21:48","date_gmt":"2025-02-19T13:21:48","guid":{"rendered":"http:\/\/www.cmd137blog.top\/?p=715"},"modified":"2025-02-19T21:21:50","modified_gmt":"2025-02-19T13:21:50","slug":"spring-cache","status":"publish","type":"post","link":"http:\/\/www.cmd137blog.top\/?p=715","title":{"rendered":"Spring Cache"},"content":{"rendered":"\n<p>Spring Cache \u662f\u57fa\u4e8e AOP\uff08\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09\u5b9e\u73b0\u7684\u7f13\u5b58\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u5957\u6ce8\u89e3\u9a71\u52a8\u7684\u7f13\u5b58\u7ba1\u7406\u673a\u5236\u3002\u5b83\u4e0d\u76f4\u63a5\u5b9e\u73b0\u7f13\u5b58\uff0c\u800c\u662f\u63d0\u4f9b\u4e86\u4e00\u4e2a\u62bd\u8c61\u5c42\uff0c\u5e95\u5c42\u53ef\u4ee5\u5207\u6362\u4e0d\u540c\u5b9e\u73b0\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">1 \u5bfc\u5165\u4f9d\u8d56\uff1a<\/h1>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;dependency>\n    &lt;groupId>org.springframework.boot&lt;\/groupId>\n    &lt;artifactId>spring-boot-starter-cache&lt;\/artifactId>\n    &lt;version>2.7.3&lt;\/version>\n&lt;\/dependency><\/code><\/pre>\n\n\n\n<p>\u5bfc\u5165redis\uff0cspring Cache\u4f1a\u81ea\u52a8\u4f7f\u7528redis\u3002<\/p>\n\n\n\n<h1 class=\"wp-block-heading\">2\u3001\u6838\u5fc3\u6ce8\u89e3<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u6ce8\u89e3<\/th><th>\u8bf4\u660e<\/th><\/tr><\/thead><tbody><tr><td>@EnableCaching<\/td><td>\u5f00\u542f\u7f13\u5b58\u6ce8\u89e3\u529f\u80fd\uff0c\u901a\u5e38\u52a0\u5728\u542f\u52a8\u7c7b\u4e0a<\/td><\/tr><tr><td>@Cacheable<\/td><td>\u5728\u65b9\u6cd5\u6267\u884c\u524d\u5148\u67e5\u8be2\u7f13\u5b58\u4e2d\u662f\u5426\u6709\u6570\u636e\uff0c\u5982\u679c\u6709\u6570\u636e\uff0c\u5219\u76f4\u63a5\u8fd4\u56de\u7f13\u5b58\u6570\u636e\uff1b\u5982\u679c\u6ca1\u6709\u7f13\u5b58\u6570\u636e\uff0c\u8c03\u7528\u65b9\u6cd5\u5e76\u5c06\u65b9\u6cd5\u8fd4\u56de\u503c\u653e\u5230\u7f13\u5b58\u4e2d<\/td><\/tr><tr><td>@CachePut<\/td><td>\u5c06\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u653e\u5230\u7f13\u5b58\u4e2d<\/td><\/tr><tr><td>@CacheEvict<\/td><td>\u5c06\u4e00\u6761\u6216\u591a\u6761\u6570\u636e\u4ece\u7f13\u5b58\u4e2d\u5220\u9664<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<h3 class=\"wp-block-heading\">@Cacheable<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a\u6807\u6ce8\u5728\u9700\u8981\u7f13\u5b58\u7ed3\u679c\u7684\u65b9\u6cd5\u4e0a\uff0c\u793a\u4f8b\u5982\u4e0b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.cache.annotation.Cacheable;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class YourService {\n    @Cacheable(value = \"yourCacheName\", key = \"#param\")\n    public String yourMethod(String param) {\n        \/\/ \u65b9\u6cd5\u903b\u8f91\uff0c\u4f8b\u5982\u67e5\u8be2\u6570\u636e\u5e93\u7b49\u64cd\u4f5c\n        return \"result\";\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>yourMethod<\/code>\u65b9\u6cd5\u7684\u7ed3\u679c\u4f1a\u6839\u636e\u6307\u5b9a\u7684\u7f13\u5b58\u540d\u79f0<code>yourCacheName<\/code>\u548c\u7f13\u5b58\u952e<code>#param<\/code>\uff08\u8fd9\u91cc\u8868\u793a\u65b9\u6cd5\u7684<code>param<\/code>\u53c2\u6570\uff09\u8fdb\u884c\u7f13\u5b58\u3002\u5373 <strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Key=<code>value<\/code>\/<code>cacheNames<\/code>+ :: + key<\/mark><\/strong>,\u4e0b\u540c\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6807\u7b7e\u5c5e\u6027<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>\/<code>cacheNames<\/code>\uff1a\u6307\u5b9a\u7f13\u5b58\u7684\u540d\u79f0\uff0c\u53ef\u4ee5\u662f\u4e00\u4e2a\u6216\u591a\u4e2a\uff0c\u5982<code>{\"cache1\", \"cache2\"}<\/code>\u3002<\/li>\n\n\n\n<li><code>key<\/code>\uff1a\u6307\u5b9a\u7f13\u5b58\u7684\u952e\uff0c\u652f\u6301 Spring \u8868\u8fbe\u5f0f\u8bed\u8a00\uff08SpEL\uff09\uff0c\u53ef\u4ee5\u901a\u8fc7\u65b9\u6cd5\u53c2\u6570\u7b49\u751f\u6210\u952e\u3002<\/li>\n\n\n\n<li><code>condition<\/code>\uff1a\u6307\u5b9a\u7f13\u5b58\u7684\u6761\u4ef6\uff0c\u53ea\u6709\u6ee1\u8db3\u6761\u4ef6\u65f6\u624d\u4f1a\u7f13\u5b58\uff0c\u4f8b\u5982<code>condition = \"#param.length() > 0\"<\/code>\u3002<\/li>\n\n\n\n<li><code>unless<\/code>\uff1a\u6307\u5b9a\u4e0d\u7f13\u5b58\u7684\u6761\u4ef6\uff0c\u4f8b\u5982<code>unless = \"#result == null\"<\/code>\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">@CachePut<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a\u6807\u6ce8\u5728\u65b9\u6cd5\u4e0a\uff0c\u4fdd\u8bc1\u65b9\u6cd5\u6267\u884c\u540e\u5c06\u8fd4\u56de\u503c\u5b58\u5165\u7f13\u5b58\uff0c\u793a\u4f8b\uff1a<\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.cache.annotation.CachePut;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class YourService {\n    @CachePut(value = \"yourCacheName\", key = \"#entity.id\")\n    public YourEntity updateYourEntity(YourEntity entity) {\n        \/\/ \u66f4\u65b0\u6570\u636e\u5e93\u7b49\u64cd\u4f5c\n        return updatedEntity;\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u8fd9\u91cc<code>updateYourEntity<\/code>\u65b9\u6cd5\u6267\u884c\u540e\uff0c\u8fd4\u56de\u7684<code>YourEntity<\/code>\u5bf9\u8c61\u4f1a\u6839\u636e<code>yourCacheName<\/code>\u548c<code>#entity.id<\/code>\u4f5c\u4e3a\u952e\u5b58\u5165\u7f13\u5b58\u3002<strong><mark style=\"background-color:rgba(0, 0, 0, 0)\" class=\"has-inline-color has-luminous-vivid-orange-color\">Key=<code>value<\/code>\/<code>cacheNames<\/code>+ :: + key<\/mark><\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6807\u7b7e\u5c5e\u6027<\/strong>\uff1a\u4e0e @Cacheable \u7c7b\u4f3c\uff0c\u6709<code>value<\/code>\/<code>cacheNames<\/code>\u3001<code>key<\/code>\u3001<code>condition<\/code>\u7b49\u5c5e\u6027\uff0c\u542b\u4e49\u548c\u7528\u6cd5\u4e00\u81f4\u3002<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">@CacheEvict<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u4f7f\u7528\u65b9\u6cd5<\/strong>\uff1a\u6807\u6ce8\u5728\u65b9\u6cd5\u4e0a\uff0c\u7528\u4e8e\u4ece\u7f13\u5b58\u4e2d\u79fb\u9664\u6570\u636e\uff0c\u793a\u4f8b\uff1a<\/li>\n\n\n\n<li><\/li>\n<\/ul>\n\n\n\n<pre class=\"wp-block-code\"><code>import org.springframework.cache.annotation.CacheEvict;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class YourService {\n    @CacheEvict(value = \"yourCacheName\", key = \"#id\")\n    public void deleteYourEntity(String id) {\n        \/\/ \u5220\u9664\u6570\u636e\u5e93\u4e2d\u5bf9\u5e94\u6570\u636e\u7684\u64cd\u4f5c\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>\u5f53<code>deleteYourEntity<\/code>\u65b9\u6cd5\u88ab\u8c03\u7528\u65f6\uff0c\u4f1a\u6839\u636e<code>yourCacheName<\/code>\u548c<code>#id<\/code>\u4ece\u7f13\u5b58\u4e2d\u79fb\u9664\u5bf9\u5e94\u7684\u6570\u636e\u3002<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>\u6807\u7b7e\u5c5e\u6027<\/strong>\uff1a\n<ul class=\"wp-block-list\">\n<li><code>value<\/code>\/<code>cacheNames<\/code>\uff1a\u6307\u5b9a\u8981\u6e05\u9664\u7684\u7f13\u5b58\u540d\u79f0\u3002<\/li>\n\n\n\n<li><code>key<\/code>\uff1a\u6307\u5b9a\u8981\u6e05\u9664\u7684\u7f13\u5b58\u952e\u3002<\/li>\n\n\n\n<li><code>allEntries<\/code>\uff1a\u5e03\u5c14\u7c7b\u578b\uff0c\u82e5\u4e3a<code>true<\/code>\uff0c\u5219\u6e05\u9664\u6307\u5b9a\u7f13\u5b58\u4e2d\u7684\u6240\u6709\u6570\u636e\u3002<\/li>\n\n\n\n<li><code>beforeInvocation<\/code>\uff1a\u5e03\u5c14\u7c7b\u578b\uff0c\u82e5\u4e3a<code>true<\/code>\uff0c\u5728\u65b9\u6cd5\u6267 \u884c\u524d\u5c31\u6e05\u9664\u7f13\u5b58\uff1b\u82e5\u4e3a<code>false<\/code>\uff08\u9ed8\u8ba4\uff09\uff0c\u5728\u65b9\u6cd5\u6210\u529f\u6267\u884c\u540e\u6e05\u9664\u7f13\u5b58\u3002<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Spring Cache \u662f\u57fa\u4e8e AOP\uff08\u9762\u5411\u5207\u9762\u7f16\u7a0b\uff09\u5b9e\u73b0\u7684\u7f13\u5b58\u6846\u67b6\uff0c\u5b83\u63d0\u4f9b\u4e86\u4e00\u5957\u6ce8\u89e3\u9a71\u52a8\u7684\u7f13\u5b58\u7ba1\u7406\u673a\u5236\u3002\u5b83 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[37],"tags":[70,25,71,64,69],"class_list":["post-715","post","type-post","status-publish","format-standard","hentry","category-javaweblist","tag-cache","tag-java","tag-redis","tag-spring","tag-69"],"_links":{"self":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/715","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=715"}],"version-history":[{"count":1,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/715\/revisions"}],"predecessor-version":[{"id":716,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=\/wp\/v2\/posts\/715\/revisions\/716"}],"wp:attachment":[{"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=715"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=715"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.cmd137blog.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=715"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}