{"id":153905,"date":"2024-11-02T11:20:21","date_gmt":"2024-11-02T03:20:21","guid":{"rendered":"https:\/\/server.hk\/cnblog\/153905\/"},"modified":"2024-11-02T11:20:22","modified_gmt":"2024-11-02T03:20:22","slug":"%e7%95%b6redis%e9%81%87%e5%88%b0springboot%ef%bc%8c%e7%b7%a9%e5%ad%98%e6%8a%80%e8%a1%93%e5%b0%b1%e9%82%a3%e9%ba%bc%e5%9b%9e%e4%ba%8b","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/153905\/","title":{"rendered":"\u7576Redis\u9047\u5230Springboot\uff0c\u7de9\u5b58\u6280\u8853\u5c31\u90a3\u9ebc\u56de\u4e8b"},"content":{"rendered":"<h1 id=\"%e7%95%b6redis%e9%81%87%e5%88%b0spring-boot%ef%bc%8c%e7%b7%a9%e5%ad%98%e6%8a%80%e8%a1%93%e5%b0%b1%e9%82%a3%e9%ba%bc%e5%9b%9e%e4%ba%8b-GOiLrpYjbG\">\u7576Redis\u9047\u5230Spring Boot\uff0c\u7de9\u5b58\u6280\u8853\u5c31\u90a3\u9ebc\u56de\u4e8b<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u61c9\u7528\u7a0b\u5f0f\u958b\u767c\u4e2d\uff0c\u6027\u80fd\u548c\u6548\u7387\u662f\u958b\u767c\u8005\u6700\u95dc\u6ce8\u7684\u5169\u500b\u65b9\u9762\u3002\u96a8\u8457\u7528\u6236\u9700\u6c42\u7684\u589e\u52a0\uff0c\u5982\u4f55\u5feb\u901f\u97ff\u61c9\u8acb\u6c42\u4e26\u63d0\u4f9b\u7a69\u5b9a\u7684\u670d\u52d9\u6210\u70ba\u4e86\u958b\u767c\u8005\u9762\u81e8\u7684\u4e00\u5927\u6311\u6230\u3002\u9019\u6642\uff0c\u7de9\u5b58\u6280\u8853\u7684\u61c9\u7528\u4fbf\u986f\u5f97\u5c24\u70ba\u91cd\u8981\u3002Redis\u4f5c\u70ba\u4e00\u7a2e\u9ad8\u6548\u7684\u7de9\u5b58\u89e3\u6c7a\u65b9\u6848\uff0c\u8207Spring Boot\u7684\u7d50\u5408\uff0c\u80fd\u5920\u986f\u8457\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u97ff\u61c9\u901f\u5ea6\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%afredis%ef%bc%9f-GOiLrpYjbG\">\u4ec0\u9ebc\u662fRedis\uff1f<\/h2>\n<p>Redis\u662f\u4e00\u7a2e\u958b\u6e90\u7684\u9ad8\u6027\u80fd\u9375\u503c\u6578\u64da\u5eab\uff0c\u652f\u6301\u591a\u7a2e\u6578\u64da\u7d50\u69cb\uff0c\u5982\u5b57\u7b26\u4e32\u3001\u54c8\u5e0c\u3001\u5217\u8868\u3001\u96c6\u5408\u7b49\u3002\u5b83\u7684\u4e3b\u8981\u7279\u9ede\u5305\u62ec\uff1a<\/p>\n<ul>\n<li>\u9ad8\u6548\u7684\u6578\u64da\u5b58\u53d6\u901f\u5ea6\uff1aRedis\u5c07\u6578\u64da\u5b58\u5132\u5728\u5167\u5b58\u4e2d\uff0c\u8b80\u53d6\u901f\u5ea6\u6975\u5feb\u3002<\/li>\n<li>\u6301\u4e45\u5316\uff1a\u652f\u6301\u5c07\u6578\u64da\u6301\u4e45\u5316\u5230\u78c1\u789f\uff0c\u907f\u514d\u6578\u64da\u4e1f\u5931\u3002<\/li>\n<li>\u8c50\u5bcc\u7684\u6578\u64da\u7d50\u69cb\uff1a\u652f\u6301\u591a\u7a2e\u6578\u64da\u985e\u578b\uff0c\u9748\u6d3b\u6027\u9ad8\u3002<\/li>\n<li>\u5206\u4f48\u5f0f\uff1a\u652f\u6301\u4e3b\u5f9e\u8907\u88fd\u548c\u5206\u7247\uff0c\u9069\u5408\u5927\u898f\u6a21\u61c9\u7528\u3002<\/li>\n<\/ul>\n<h2 id=\"spring-boot%e8%88%87%e7%b7%a9%e5%ad%98%e6%8a%80%e8%a1%93-GOiLrpYjbG\">Spring Boot\u8207\u7de9\u5b58\u6280\u8853<\/h2>\n<p>Spring Boot\u662f\u4e00\u500b\u57fa\u65bcSpring\u6846\u67b6\u7684\u958b\u767c\u6846\u67b6\uff0c\u65e8\u5728\u7c21\u5316Spring\u61c9\u7528\u7684\u914d\u7f6e\u548c\u90e8\u7f72\u3002\u5b83\u63d0\u4f9b\u4e86\u8a31\u591a\u958b\u7bb1\u5373\u7528\u7684\u529f\u80fd\uff0c\u5176\u4e2d\u4e4b\u4e00\u4fbf\u662f\u7de9\u5b58\u652f\u6301\u3002Spring Boot\u7684\u7de9\u5b58\u62bd\u8c61\u5c64\u4f7f\u5f97\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u96c6\u6210\u5404\u7a2e\u7de9\u5b58\u6280\u8853\uff0c\u5305\u62ecRedis\u3002<\/p>\n<h3 id=\"%e5%a6%82%e4%bd%95%e5%9c%a8spring-boot%e4%b8%ad%e4%bd%bf%e7%94%a8redis%e4%bd%9c%e7%82%ba%e7%b7%a9%e5%ad%98-GOiLrpYjbG\">\u5982\u4f55\u5728Spring Boot\u4e2d\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58<\/h3>\n<p>\u8981\u5728Spring Boot\u4e2d\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\uff0c\u9996\u5148\u9700\u8981\u6dfb\u52a0\u76f8\u61c9\u7684\u4f9d\u8cf4\u3002\u5728Maven\u7684pom.xml\u4e2d\u6dfb\u52a0\u4ee5\u4e0b\u4f9d\u8cf4\uff1a<\/p>\n<pre><code>\n&lt;dependency&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-data-redis&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n    &lt;groupId&gt;redis.clients&lt;\/groupId&gt;\n    &lt;artifactId&gt;jedis&lt;\/artifactId&gt;\n&lt;\/dependency&gt;\n<\/code><\/pre>\n<p>\u63a5\u4e0b\u4f86\uff0c\u5728application.properties\u4e2d\u914d\u7f6eRedis\u7684\u9023\u63a5\u4fe1\u606f\uff1a<\/p>\n<pre><code>\nspring.redis.host=localhost\nspring.redis.port=6379\n<\/code><\/pre>\n<p>\u7136\u5f8c\uff0c\u5728Spring Boot\u61c9\u7528\u4e2d\u555f\u7528\u7de9\u5b58\u529f\u80fd\uff0c\u53ea\u9700\u5728\u4e3b\u985e\u4e0a\u6dfb\u52a0@EnableCaching\u8a3b\u89e3\uff1a<\/p>\n<pre><code>\nimport org.springframework.boot.SpringApplication;\nimport org.springframework.boot.autoconfigure.SpringBootApplication;\nimport org.springframework.cache.annotation.EnableCaching;\n\n@SpringBootApplication\n@EnableCaching\npublic class Application {\n    public static void main(String[] args) {\n        SpringApplication.run(Application.class, args);\n    }\n}\n<\/code><\/pre>\n<h3 id=\"%e4%bd%bf%e7%94%a8%e7%b7%a9%e5%ad%98%e7%9a%84%e7%a4%ba%e4%be%8b-GOiLrpYjbG\">\u4f7f\u7528\u7de9\u5b58\u7684\u793a\u4f8b<\/h3>\n<p>\u4ee5\u4e0b\u662f\u4e00\u500b\u7c21\u55ae\u7684\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528Redis\u7de9\u5b58\u4e00\u500b\u670d\u52d9\uff1a<\/p>\n<pre><code>\nimport org.springframework.cache.annotation.Cacheable;\nimport org.springframework.stereotype.Service;\n\n@Service\npublic class UserService {\n    \n    @Cacheable(\"users\")\n    public User getUserById(Long id) {\n        \/\/ \u6a21\u64ec\u5f9e\u6578\u64da\u5eab\u67e5\u8a62\u7528\u6236\n        return userRepository.findById(id).orElse(null);\n    }\n}\n<\/code><\/pre>\n<p>\u5728\u9019\u500b\u4f8b\u5b50\u4e2d\uff0c\u7576\u8abf\u7528getUserById\u65b9\u6cd5\u6642\uff0cSpring\u6703\u9996\u5148\u6aa2\u67e5\u7de9\u5b58\u4e2d\u662f\u5426\u5b58\u5728\u5c0d\u61c9\u7684\u7528\u6236\u6578\u64da\u3002\u5982\u679c\u5b58\u5728\uff0c\u5247\u76f4\u63a5\u8fd4\u56de\u7de9\u5b58\u4e2d\u7684\u6578\u64da\uff1b\u5982\u679c\u4e0d\u5b58\u5728\uff0c\u5247\u5f9e\u6578\u64da\u5eab\u4e2d\u67e5\u8a62\u4e26\u5c07\u7d50\u679c\u5b58\u5165\u7de9\u5b58\u3002<\/p>\n<h2 id=\"redis%e7%b7%a9%e5%ad%98%e7%9a%84%e5%84%aa%e5%8b%a2-GOiLrpYjbG\">Redis\u7de9\u5b58\u7684\u512a\u52e2<\/h2>\n<p>\u4f7f\u7528Redis\u4f5c\u70ba\u7de9\u5b58\u7684\u512a\u52e2\u4e3b\u8981\u9ad4\u73fe\u5728\u4ee5\u4e0b\u5e7e\u500b\u65b9\u9762\uff1a<\/p>\n<ul>\n<li>\u63d0\u9ad8\u6027\u80fd\uff1a\u901a\u904e\u6e1b\u5c11\u5c0d\u6578\u64da\u5eab\u7684\u8a2a\u554f\u6b21\u6578\uff0c\u986f\u8457\u63d0\u9ad8\u61c9\u7528\u7684\u97ff\u61c9\u901f\u5ea6\u3002<\/li>\n<li>\u6e1b\u5c11\u5ef6\u9072\uff1a\u7528\u6236\u8acb\u6c42\u7684\u97ff\u61c9\u6642\u9593\u5927\u5e45\u964d\u4f4e\uff0c\u63d0\u5347\u7528\u6236\u9ad4\u9a57\u3002<\/li>\n<li>\u64f4\u5c55\u6027\uff1aRedis\u7684\u5206\u4f48\u5f0f\u7279\u6027\u4f7f\u5f97\u61c9\u7528\u80fd\u5920\u8f15\u9b06\u64f4\u5c55\uff0c\u61c9\u5c0d\u9ad8\u4e26\u767c\u7684\u8acb\u6c42\u3002<\/li>\n<\/ul>\n<h2 id=\"%e7%b8%bd%e7%b5%90-GOiLrpYjbG\">\u7e3d\u7d50<\/h2>\n<p>\u7576Redis\u9047\u5230Spring Boot\uff0c\u7de9\u5b58\u6280\u8853\u4fbf\u80fd\u5920\u5145\u5206\u767c\u63ee\u5176\u512a\u52e2\uff0c\u63d0\u5347\u61c9\u7528\u7684\u6027\u80fd\u548c\u7528\u6236\u9ad4\u9a57\u3002\u901a\u904e\u7c21\u55ae\u7684\u914d\u7f6e\u548c\u4f7f\u7528\uff0c\u958b\u767c\u8005\u53ef\u4ee5\u8f15\u9b06\u5730\u5c07Redis\u96c6\u6210\u5230Spring Boot\u61c9\u7528\u4e2d\uff0c\u5be6\u73fe\u9ad8\u6548\u7684\u6578\u64da\u7de9\u5b58\u3002\u5c0d\u65bc\u9700\u8981\u9ad8\u6027\u80fd\u548c\u9ad8\u53ef\u7528\u6027\u7684\u61c9\u7528\u4f86\u8aaa\uff0c\u9078\u64c7\u5408\u9069\u7684\u7de9\u5b58\u6280\u8853\u662f\u81f3\u95dc\u91cd\u8981\u7684\u3002<\/p>\n<p>\u5982\u679c\u60a8\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2fVPS<\/a> \u89e3\u6c7a\u65b9\u6848\uff0cServer.HK\u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u60a8\u7684\u9700\u6c42\u3002\u7121\u8ad6\u662f\u642d\u5efaRedis\u9084\u662f\u5176\u4ed6\u61c9\u7528\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4f3a\u670d\u5668<\/a> \u90fd\u80fd\u70ba\u60a8\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u63a2\u7d22Redis\u8207Spring Boot\u7684\u7d50\u5408\uff0c\u4e86\u89e3\u5982\u4f55\u7c21\u5316\u7de9\u5b58\u6280\u8853\uff0c\u63d0\u9ad8\u61c9\u7528\u6027\u80fd\u8207\u6548\u7387\u3002<\/p>\n","protected":false},"author":0,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[101],"tags":[],"class_list":["post-153905","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153905","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"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=153905"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153905\/revisions"}],"predecessor-version":[{"id":153906,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/153905\/revisions\/153906"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=153905"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=153905"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=153905"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}