{"id":201140,"date":"2025-05-10T08:56:38","date_gmt":"2025-05-10T00:56:38","guid":{"rendered":"https:\/\/server.hk\/cnblog\/201140\/"},"modified":"2025-05-10T08:56:38","modified_gmt":"2025-05-10T00:56:38","slug":"springboot-springcloud%e9%a1%b9%e7%9b%ae%e9%9b%86%e6%88%90redis%e8%bf%9b%e8%a1%8c%e5%ad%98%e5%8f%96%e7%9a%84%e8%bf%87%e7%a8%8b%e8%a7%a3%e6%9e%90","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/201140\/","title":{"rendered":"Springboot\/Springcloud\u9879\u76ee\u96c6\u6210redis\u8fdb\u884c\u5b58\u53d6\u7684\u8fc7\u7a0b\u89e3\u6790"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>Springboot\/Springcloud\u9879\u76ee\u96c6\u6210redis\u8fdb\u884c\u5b58\u53d6\u7684\u8fc7\u7a0b\u89e3\u6790<\/h1>\n<p><span style=\"cursor: pointer\"><i><\/i>\u6536\u85cf<\/span> <\/p>\n<p>\u600e\u4e48\u5165\u95e8\u6570\u636e\u5e93\u7f16\u7a0b\uff1f\u9700\u8981\u5b66\u4e60\u54ea\u4e9b\u77e5\u8bc6\u70b9\uff1f\u8fd9\u662f\u65b0\u624b\u4eec\u521a\u63a5\u89e6\u7f16\u7a0b\u65f6\u5e38\u89c1\u7684\u95ee\u9898\uff1b\u4e0b\u9762golang\u5b66\u4e60\u7f51\u5c31\u6765\u7ed9\u5927\u5bb6\u6574\u7406\u5206\u4eab\u4e00\u4e9b\u77e5\u8bc6\u70b9\uff0c\u5e0c\u671b\u80fd\u591f\u7ed9\u521d\u5b66\u8005\u4e00\u4e9b\u5e2e\u52a9\u3002\u672c\u7bc7\u6587\u7ae0\u5c31\u6765\u4ecb\u7ecd\u300aSpringboot\/Springcloud\u9879\u76ee\u96c6\u6210redis\u8fdb\u884c\u5b58\u53d6\u7684\u8fc7\u7a0b\u89e3\u6790\u300b\uff0c\u6d89\u53ca\u5230Redis\u3001\u5b58\u53d6\u3001SpringcloudSpringboot\uff0c\u6709\u9700\u8981\u7684\u53ef\u4ee5\u6536\u85cf\u4e00\u4e0b<\/p>\n<p>\u524d\u8a00\uff1aRedis\u652f\u6301\u4e94\u79cd\u6570\u636e\u7c7b\u578b\uff1astring\uff08\u5b57\u7b26\u4e32\uff09\uff0chash\uff08\u54c8\u5e0c\uff09\uff0clist\uff08\u5217\u8868\uff09\uff0cset\uff08\u96c6\u5408\uff09\uff0czset(sorted set\uff1a\u6709\u5e8f\u96c6\u5408)\u3002<\/p>\n<h2>\u4e00\u3001redis\u53bb\u5b98\u7f51https:\/\/redis.io\/download\u4e0b\u8f7d\u540e\u89e3\u538b<\/h2>\n<p>\u7136\u540e\u70b9\u51fb\u91cc\u9762\u7684redis-server.exe\uff08windows\u5e73\u53f0\uff09\u5373\u53ef\u6b63\u5e38\u542f\u52a8<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167237912463ae7af417a33.png\" class=\"aligncenter\"><\/p>\n<h2>\u4e8c\u3001\u5728\u9879\u76ee\u4e2d\u6dfb\u52a0redis\u4f9d\u8d56<\/h2>\n<pre>\n\n   \n    \n     org.springframework.boot\n    \n    \u3002\n    <p><\/p>\n\n    <p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167237912663ae7af6941a1.png\" class=\"aligncenter\"><\/p>\n\n    <h2>\u4e5d\u3001\u5982\u679c\u60f3\u8981\u5728\u5355\u5143\u6d4b\u8bd5\u4e2d\u8fdb\u884c\u5b58\u53d6<\/h2>\n\n    <p>\u6dfb\u52a0\u6d4b\u8bd5\u4f9d\u8d56\uff0cJunit\u5fc5\u987b4.12\u4ee5\u4e0a<\/p>\n\n\n    <pre>\n\n     \n      \n       org.springframework.boot\n      \n      \n       spring-boot-starter-test\n      \n     \n     \n      \n       junit\n      \n      \n       junit\n      \n      \n       4.12\n      \n      \n       test\n      \n     <\/pre>\n<p><span style=\"background-color: initial\">\u5728\u8981\u8fdb\u884c\u5b58\u53d6\u7684\u7c7b\u4e0a\u6dfb\u52a0\u6ce8\u89e3@RunWith\u3001@SpringBootTest\uff0c\u610f\u601d\u662f\u542f\u52a8\u5355\u5143\u6d4b\u8bd5\u65f6\u542f\u52a8\u5f53\u524d\u9879\u76ee\u7684\u542f\u52a8\u7c7b\uff0c\u56e0\u4e3a\u542f\u52a8\u7c7b\u91cc\u9762\u7684@SpringBootApplication\u91cc\u9762\u5305\u542b\u4e86\u5305\u626b\u63cf@ComponentScan\uff0c\u4e0d\u7136\u6ce8\u5165StringRedisTemplate\u6216RedisTemplate\u65f6\u6ce8\u5165\u5931\u8d25\u62a5\u7a7a\u6307\u9488\uff0c\u5f53\u7136\u4e5f\u53ef\u4ee5\u5728\u542f\u52a8\u7c7b\u91cc\u9762\u8fd4\u56denew StringRedisTemplate\u6216new RedisTemplate\u5e76\u4e14\u52a0\u6ce8\u89e3@Bean\u7684\u65b9\u5f0f\u5904\u7406\u6ce8\u5165\u5931\u8d25\u95ee\u9898\uff0c\u8fd9\u91cc\u76f4\u63a5\u901a\u8fc7\u52a0\u6ce8\u89e3\u7684\u65b9\u5f0f\u5904\u7406\u3002<\/span><\/p>\n<pre>\n@RunWith(value = SpringJUnit4ClassRunner.class)\n\/\/RedisApp\u4e3a\u542f\u52a8\u7c7b\u540d\u5b57\n@SpringBootTest(classes = {RedisApp.class})\npublic class RedisAppTest {\n    @Autowired\n    private StringRedisTemplate stringRedisTemplate;\n    @Autowired\n    private RedisTemplate redisTemplate;\n\n    @Test\n    public void setStringRedis(){\n        this.stringRedisTemplate.boundValueOps(\"name2\").set(\"\u718a\u5927\");\n        System.out.println(\"ok\");\n    }\n    @Test\n    public void getStringRedis(){\n        String name = this.stringRedisTemplate.boundValueOps(\"name2\").get();\n        System.out.println(\"ok:\"+name);\n    }\n}<\/pre>\n<p>\u5728redis desktop manager\u5de5\u5177\u4e2d\u53ef\u4ee5\u770b\u5230\u5b58\u50a8\u6210\u529f\u4e86\uff0c\u5728\u63a7\u5236\u53f0\u4e5f\u53ef\u4ee5\u8bfb\u53d6<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167237912763ae7af72128b.png\" class=\"aligncenter\"><\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20221230\/167237912763ae7af7aca4f.png\" class=\"aligncenter\"><\/p>\n<h2>\u4e5d\u3001\u5b9e\u9645\u4f7f\u7528\u601d\u8def<\/h2>\n<pre>\n@Autowired\n    private RedisTemplate redisTemplate;\n\u3000\u3000\u3000\u3000\/**\n        * \u9996\u6b21\u8bbf\u95ee\u65f6\uff0c\u5982\u679credis\u6ca1\u6709\u6570\u636e\uff0c\u5c31\u8bbf\u95ee\u6570\u636e\u5e93\uff0c\u7136\u540e\u628a\u8bbf\u95ee\u5230\u7684\u6570\u636e\u5b58\u5230redis\n        * \u540e\u7eed\u8bbf\u95ee\u65f6\uff0c\u76f4\u63a5\u67e5\u8be2redis\n        *\/\n        @Override\n        public List\n     \n       findByPersonId(Long id) {\/\/ \u5148\u67e5\u770b\u7f13\u5b58\u4e2d\u6709\u6ca1\u6709\n            List\n      \n        list = (List\n       \n        ) redisTemplate.boundValueOps(id).get(); \u3000\u3000\u3000\u3000\u3000\u3000\u3000if(list==null){ \u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000System.out.println(\"redis\u4e2d\u6ca1\u6709\uff0c\u5f00\u59cb\u4ece\u6570\u636e\u5e93\u4e2d\u83b7\u53d6\"); \u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000.........\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\u3000\/\/\u67e5\u8be2\u6570\u636e\u5e93\u5f97\u5230List\n        \n          list =xxxxxx; redisTemplate.boundValueOps(id).set(list);\/\/\u5c06\u4ece\u6570\u636e\u5e93\u67e5\u5230\u7684\u6570\u636e\u6dfb\u52a0\u5230redis\u4e2d\u4ee5\u5907\u4e0b\u6b21\u67e5\u627e }else{ System.out.println(\"redis\u4e2d\u5b58\u5728\uff0clist\u662f\u76f4\u63a5\u4ece\u7f13\u5b58\u4e2d\u83b7\u53d6\u7684\uff0c\u6ca1\u67e5\u6570\u636e\u5e93\"); } return list; }\n        \n       \n      \n     <\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Springboot\/Sprin&#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-201140","post","type-post","status-publish","format-standard","hentry","category-database"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201140","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=201140"}],"version-history":[{"count":0,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/201140\/revisions"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=201140"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=201140"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=201140"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}