{"id":174846,"date":"2020-04-01T10:44:24","date_gmt":"2020-04-01T02:44:24","guid":{"rendered":"https:\/\/lrxjmw.cn\/?p=174846"},"modified":"2020-03-23T08:48:43","modified_gmt":"2020-03-23T00:48:43","slug":"springbootredis","status":"publish","type":"post","link":"https:\/\/lrxjmw.cn\/springbootredis.html","title":{"rendered":"\u4ecb\u7ecdSpringBoot \u6574\u5408 Redis \u7f13\u5b58"},"content":{"rendered":"\n\n\n
\u5bfc\u8bfb<\/td>\n\u65e0\u6cd5\u652f\u6491\u8fd9\u4e48\u5927\u7684\u6570\u636e\u8bbf\u95ee\u91cf,redis\u4f7f\u7528\u7684\u65f6\u5019\u53ef\u4ee5\u5355\u72ec\u5229\u7528\u5ba2\u6237\u7aef\u5f15\u5165jar\u5305\u64cd\u4f5c\u5373\u53ef,\u5b9e\u9645\u9879\u76ee\u4e2d\u90fd\u662f\u548c\u6846\u67b6\u8fdb\u884c\u6574\u5408\u7684\u6bd4\u8f83\u591a,\u6b64\u5904\u6f14\u793a\u5229\u7528springboot\u6574\u5408<\/strong><\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n
1.\u9996\u5148\u5bfc\u5165\u4f7f\u7528Maven\u5bfc\u5165jar\u5305<\/strong><\/span><\/div>\n
<dependency>\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0<groupId>org.springframework.boot<\/groupId>\u00a0\u00a0\u00a0\u00a0\r\n\u00a0\u00a0\u00a0\u00a0<artifactId>spring-boot-starter-data-redis<\/artifactId>\r\n<\/dependency>\r\n<dependency>\r\n\u00a0\u00a0\u00a0\u00a0<groupId>com.alibaba<\/groupId>\r\n\u00a0\u00a0\u00a0\u00a0<artifactId>fastjson<\/artifactId>\r\n\u00a0\u00a0\u00a0\u00a0<version>1.2.62<\/version>\r\n<\/dependency><\/pre>\n
2.\u5728application.properties\u914d\u7f6e\u4fe1\u606f<\/strong><\/span><\/div>\n
#\u00a0Redis\u6570\u636e\u5e93\u7d22\u5f15\uff08\u9ed8\u8ba4\u4e3a0\uff09\u00a0\u00a0\r\nspring.redis.database=0\u00a0\u00a0\r\n#\u00a0Redis\u670d\u52a1\u5668\u5730\u5740\u00a0\u00a0\r\nspring.redis.host=localhost\r\n#\u00a0Redis\u670d\u52a1\u5668\u8fde\u63a5\u7aef\u53e3\u00a0\u00a0\r\nspring.redis.port=6379\u00a0\u00a0\r\n#\u00a0Redis\u670d\u52a1\u5668\u8fde\u63a5\u5bc6\u7801\uff08\u9ed8\u8ba4\u4e3a\u7a7a\uff09\u00a0\u00a0\r\nspring.redis.password=123456\r\n#\u00a0\u8fde\u63a5\u6c60\u6700\u5927\u8fde\u63a5\u6570\uff08\u4f7f\u7528\u8d1f\u503c\u8868\u793a\u6ca1\u6709\u9650\u5236\uff09\u00a0\u00a0\r\nspring.redis.pool.max-active=200\u00a0\u00a0\r\n#\u00a0\u8fde\u63a5\u6c60\u6700\u5927\u963b\u585e\u7b49\u5f85\u65f6\u95f4\uff08\u4f7f\u7528\u8d1f\u503c\u8868\u793a\u6ca1\u6709\u9650\u5236\uff09\u00a0\u00a0\r\nspring.redis.pool.max-wait=-1\u00a0\u00a0\r\n#\u00a0\u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5927\u7a7a\u95f2\u8fde\u63a5\u00a0\u00a0\r\nspring.redis.pool.max-idle=10\u00a0\r\n#\u00a0\u8fde\u63a5\u6c60\u4e2d\u7684\u6700\u5c0f\u7a7a\u95f2\u8fde\u63a5\u00a0\u00a0\r\nspring.redis.pool.min-idle=0\u00a0\u00a0\r\n#\u00a0\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff08\u6beb\u79d2\uff09\u00a0\u00a0\r\nspring.redis.timeout=1000ms<\/pre>\n
3.\u7f16\u5199Redis\u5de5\u5177\u7c7b<\/strong><\/span><\/div>\n
@Configuration\r\n@ConditionalOnClass(RedisOperations.class)\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u7cfb\u7edf\u4e2d\u6709RedisOperations\u7c7b\u65f6\r\n@EnableConfigurationProperties(RedisProperties.class)\u00a0\u00a0\/\/\u542f\u52a8RedisProperties\u8fd9\u4e2a\u7c7b\r\n@EnableCaching\r\n\r\n\/\/\u00a0www.1b23.com\r\npublic\u00a0class\u00a0RedisConfig\u00a0extends\u00a0CachingConfigurerSupport\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0@Autowired\r\n\u00a0\u00a0\u00a0\u00a0RedisTemplate\u00a0redisTemplate;\r\n\u00a0\u00a0\u00a0\u00a0\/\/\u00a0\u914d\u7f6e\u7f13\u5b58\u7ba1\u7406\u5668\r\n\u00a0\u00a0\u00a0\u00a0@Bean\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0RedisCacheManager\u00a0cacheManager(RedisConnectionFactory\u00a0connectionFactory)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0LettuceConnectionFactory\u00a0jedisConnectionFactory\u00a0=\u00a0(LettuceConnectionFactory)\u00a0redisTemplate.getConnectionFactory();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0jedisConnectionFactory.setDatabase(2);\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u6307\u5b9adbindex\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0redisTemplate.setConnectionFactory(jedisConnectionFactory);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0jedisConnectionFactory.resetConnection();\r\n\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0RedisCacheConfiguration\u00a0config\u00a0=\u00a0RedisCacheConfiguration.defaultCacheConfig()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.entryTtl(Duration.ofSeconds(60*20))\u00a0\/\/\u00a020\u5206\u949f\u7f13\u5b58\u5931\u6548\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0\u8bbe\u7f6ekey\u7684\u5e8f\u5217\u5316\u65b9\u5f0f\r\n\/\/\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.entryTtl(Duration.ofSeconds(10))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer(new\u00a0StringRedisSerializer()))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0\u8bbe\u7f6evalue\u7684\u5e8f\u5217\u5316\u65b9\u5f0f\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(new\u00a0FastJsonRedisSerializer(Object.class)))\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\/\/\u00a0\u4e0d\u7f13\u5b58null\u503c\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.disableCachingNullValues();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0RedisCacheManager\u00a0redisCacheManager\u00a0=\u00a0RedisCacheManager.builder(connectionFactory)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.cacheDefaults(config)\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.transactionAware()\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0.build();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0redisCacheManager;\r\n\u00a0\u00a0\u00a0\u00a0}\r\n}<\/pre>\n
package\u00a0com.FireService.config;\r\nimport\u00a0java.nio.charset.Charset;\r\nimport\u00a0org.springframework.data.redis.serializer.RedisSerializer;\r\nimport\u00a0org.springframework.data.redis.serializer.SerializationException;\r\nimport\u00a0com.alibaba.fastjson.JSON;\r\nimport\u00a0com.alibaba.fastjson.parser.ParserConfig;\r\nimport\u00a0com.alibaba.fastjson.serializer.SerializerFeature;\r\n\r\npublic\u00a0class\u00a0FastJsonRedisSerializer<T>\u00a0implements\u00a0RedisSerializer<T>\u00a0{\r\n\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0static\u00a0final\u00a0Charset\u00a0DEFAULT_CHARSET\u00a0=\u00a0Charset.forName(\"UTF-8\");\r\n\r\n\u00a0\u00a0\u00a0\u00a0private\u00a0Class<T>\u00a0clazz;\r\n\u00a0\u00a0\u00a0\u00a0static\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0ParserConfig.getGlobalInstance().addAccept(\"com.FireService\");\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0FastJsonRedisSerializer(Class<T>\u00a0clazz)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0super();\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0this.clazz\u00a0=\u00a0clazz;\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0@Override\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0byte[]\u00a0serialize(T\u00a0t)\u00a0throws\u00a0SerializationException\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0(null\u00a0==\u00a0t)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0new\u00a0byte[0];\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0JSON.toJSONString(t,\u00a0SerializerFeature.WriteClassName).getBytes(DEFAULT_CHARSET);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n\u00a0\u00a0\u00a0\u00a0@Override\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0T\u00a0deserialize(byte[]\u00a0bytes)\u00a0throws\u00a0SerializationException\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0if\u00a0(null\u00a0==\u00a0bytes\u00a0||\u00a0bytes.length\u00a0<=\u00a00)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0null;\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0String\u00a0str\u00a0=\u00a0new\u00a0String(bytes,\u00a0DEFAULT_CHARSET);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0(T)\u00a0JSON.parseObject(str,\u00a0clazz);\r\n\u00a0\u00a0\u00a0\u00a0}\r\n\r\n}<\/pre>\n
4.SpringBoot\u6709\u5173\u7f13\u5b58\u7684\u51e0\u4e2a\u6ce8\u89e3<\/strong><\/span><\/div>\n

@Cacheable\uff1a\u67e5\u8be2
\n\u53ef\u9009\u5c5e\u6027\uff1a
\ncacheNames\/value\uff1a\u6307\u5b9a\u7f13\u5b58\u7ec4\u4ef6\u7684\u540d\u5b57\uff1b
\nkey\uff1a\u7f13\u5b58\u6570\u636e\u4f7f\u7528\u7684key\uff0c\u53ef\u4ee5\u7528\u6765\u6307\u5b9a\u3002\u9ed8\u8ba4\u5373\u4f7f\u7528\u65b9\u6cd5\u53c2\u6570\u7684\u503c
\nkeyGenerator\uff1akey\u7684\u751f\u6210\u5668\uff0c\u53ef\u4ee5\u81ea\u5df1\u6307\u5b9akey\u7684\u751f\u6210\u5668\u7684\u7ec4\u4ef6id<\/p>\n

\/\/\u81ea\u5b9a\u4e49\u914d\u7f6e\u7c7b\u914d\u7f6ekeyGenerator\r\n@Configuration\r\npublic\u00a0class\u00a0MyCacheConfig\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0@Bean(\"myKeyGenerator\")\r\n\u00a0\u00a0\u00a0\u00a0public\u00a0KeyGenerator\u00a0keyGenerator(){\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0new\u00a0KeyGenerator()\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0@Override\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0public\u00a0Object\u00a0generate(Object\u00a0target,\u00a0Method\u00a0method,\u00a0Object...\u00a0params)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0method.getName()+\"[\"+\u00a0Arrays.asList(params).toString()\u00a0+\"]\";\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0}\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0};\r\n\u00a0\u00a0\u00a0\u00a0}\r\n}<\/pre>\n

cacheManager\uff1a\u6307\u5b9a\u7f13\u5b58\u7ba1\u7406\u5668\uff1b\u6216\u8005cacheResolver\u83b7\u53d6\u6307\u5b9a\u89e3\u6790\u5668<\/p>\n

condition\uff1a\u6307\u5b9a\u7b26\u5408\u6761\u4ef6\u7684\u60c5\u51b5\u4e0b\u624d\u7f13\u5b58\uff1b\u5982condition=\"#id>0\"<\/p>\n

unless:\u5426\u5b9a\u7f13\u5b58\uff0c\u5f53unless\u6307\u5b9a\u7684\u6761\u4ef6\u4e3atrue\uff0c\u65b9\u6cd5\u7684\u8fd4\u56de\u503c\u4e0d\u4f1a\u88ab\u7f13\u5b58\uff0c\u53ef\u4ee5\u83b7\u53d6\u5230\u7ed3\u679c\u8fdb\u884c\u5224\u65ad;\u5982unless=\"#result==null\";<\/p>\n

sync\uff1a\u662f\u5426\u4f7f\u7528\u5f02\u6b65\u6a21\u5f0f<\/p>\n

\u4f8b\u5982\uff1a<\/p>\n

@Cacheable(value\u00a0=\u00a0\"RedisInfo\",\u00a0key\u00a0=\u00a0\"#root.methodName+'['+#account+']'\")\r\n@ResponseBody\r\n@RequestMapping(\"\/RedisTest\")\r\npublic\u00a0Result\u00a0findUserOrder(String\u00a0account)\u00a0throws\u00a0Exception{\r\n\u00a0\u00a0\u00a0\u00a0if(account!=null)\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0List<Map<String,\u00a0Object>>\u00a0list=orderFindGoods.findUserOrder(account);\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0Results.successWithData(list,\u00a0BaseEnums.SUCCESS.code(),\u00a0BaseEnums.SUCCESS.desc());\r\n\u00a0\u00a0\u00a0\u00a0}else\u00a0{\r\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0return\u00a0Results.failure();\r\n\u00a0\u00a0\u00a0\u00a0}\r\n}<\/pre>\n

\u8fd0\u884c\u9879\u76ee\u67e5\u770b\u7ed3\u679c<\/p>\n

1.\u7b2c\u4e00\u6b21\u8bbf\u95ee<\/p>\n

\u67e5\u770bDruid\u8fde\u63a5\u4fe1\u606f<\/p>\n","protected":false},"excerpt":{"rendered":"

<dependency>\u00a0\u00a0\u00a0\u00a0 \u00a0\u00a0\u00a0\u00a0<groupId>org.springfra […]<\/p>\n","protected":false},"author":643,"featured_media":174857,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[55],"tags":[],"class_list":["post-174846","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-thread"],"acf":[],"_links":{"self":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174846","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/users\/643"}],"replies":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/comments?post=174846"}],"version-history":[{"count":3,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174846\/revisions"}],"predecessor-version":[{"id":174856,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/posts\/174846\/revisions\/174856"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media\/174857"}],"wp:attachment":[{"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/media?parent=174846"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/categories?post=174846"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/lrxjmw.cn\/wp-json\/wp\/v2\/tags?post=174846"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}