{"id":969,"date":"2020-05-22T15:20:52","date_gmt":"2020-05-22T07:20:52","guid":{"rendered":"http:\/\/diuut.com\/?p=969"},"modified":"2020-06-01T17:48:52","modified_gmt":"2020-06-01T09:48:52","slug":"redis%e6%8c%87%e5%ae%9a%e5%ba%8f%e5%88%97%e5%8c%96%e5%af%b9%e8%b1%a1","status":"publish","type":"post","link":"https:\/\/diuut.com\/?p=969","title":{"rendered":"Redis\u6307\u5b9a\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5e8f\u5217\u5316"},"content":{"rendered":"\n<blockquote class=\"wp-block-quote\"><p>\u9700\u6c42\uff1a<\/p><cite>\u66ff\u6362redisTemplate\u4e2d\u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4e0d\u7528\u5f3a\u8f6c\u5f97\u5230\u6307\u5b9a\u5bf9\u8c61<\/cite><\/blockquote>\n\n\n\n<p>\u4fee\u6539RedisConfig\u4e2d\u7684redistemplate@bean<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n    @Bean(name = &quot;stuRedisTemplate&quot;)\n    public RedisTemplate&lt;String, Student&gt; stuRedisTemplate(RedisConnectionFactory redisConnectionFactory){\n        log.info(&quot;objectTemplate&quot;);\n        RedisTemplate&lt;String, Student&gt; redisTemplate = new RedisTemplate&lt;String, Student&gt;();\n        redisTemplate.setKeySerializer(new StringRedisSerializer());\n        redisTemplate.setValueSerializer(new Jackson2JsonRedisSerializer(Student.class));\n\/\/\u539f\u672c\u662fGenericJackson2JsonRedisSerializer()\n        redisTemplate.setHashKeySerializer(new StringRedisSerializer());\n        redisTemplate.setHashValueSerializer(new GenericJackson2JsonRedisSerializer());\n        redisTemplate.setConnectionFactory(redisConnectionFactory);\n        return redisTemplate;\n    }\n<\/pre><\/div>\n\n\n<p>\u7136\u540e\u5728\u8c03\u7528\u5904\u6307\u5b9a\u8c03\u7528\u65b9\u5f0f<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: java; title: ; notranslate\" title=\"\">\n@Resource(name = &quot;stuRedisTemplate&quot;)\n     RedisTemplate&lt;String, Student&gt; redisTemplate;\n\n     @Test\n     public void test5() {\n        Optional&lt;Student&gt; optionalStudent = studentDao.findById(4);\n        Student student = optionalStudent.get();\n        log.info(&quot;student\uff1a{}&quot;, student);\n        redisTemplate.opsForValue().set(&quot;test-students:3&quot;,student );\n\n        Student student1 =  redisTemplate.opsForValue().get(&quot;test-students:3&quot;);\n        \/\/\u6b64\u5904\u5c31\u4e0d\u7528\u5f3a\u8f6c\uff0c\u5e76\u4e14redis\u4e2d\u7684\u4e5f\u662f\u6807\u51c6\u7684json\u5b57\u7b26\u4e32\uff0c\u53ef\u8bfb\u6027\u4e5f\u597d\n        log.info(&quot;student1: {}&quot;,student1);\n    }\n<\/pre><\/div>","protected":false},"excerpt":{"rendered":"<p>\u9700\u6c42\uff1a \u66ff\u6362redisTemplate\u4e2d\u7684\u9ed8\u8ba4\u5e8f\u5217\u5316\u65b9\u5f0f\uff0c\u53ef\u4ee5\u4e0d\u7528\u5f3a\u8f6c\u5f97\u5230\u6307\u5b9a\u5bf9\u8c61 \u4fee\u6539RedisConfig<span class=\"more-button\"><a href=\"https:\/\/diuut.com\/?p=969\" class=\"more-link\">view all . . .<span class=\"screen-reader-text\">Redis\u6307\u5b9a\u81ea\u5b9a\u4e49\u5bf9\u8c61\u5e8f\u5217\u5316<\/span><\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":688,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[10,17],"_links":{"self":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/969"}],"collection":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=969"}],"version-history":[{"count":4,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions"}],"predecessor-version":[{"id":987,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/posts\/969\/revisions\/987"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=\/wp\/v2\/media\/688"}],"wp:attachment":[{"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/diuut.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}