From 9b6b1e657ca7c46ad90c502323273ba6f899b8f0 Mon Sep 17 00:00:00 2001 From: smallchill Date: Mon, 24 Dec 2018 13:53:45 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E5=A2=9E=E5=8A=A0redis=E9=85=8D?= =?UTF-8?q?=E7=BD=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- blade-core-boot/pom.xml | 5 ++ .../config/RedisTemplateConfiguration.java | 78 +++++++++++++++++++ .../core/boot/redis/RedisKeySerializer.java | 75 ++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 +- 4 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 blade-core-boot/src/main/java/org/springblade/core/boot/config/RedisTemplateConfiguration.java create mode 100644 blade-core-boot/src/main/java/org/springblade/core/boot/redis/RedisKeySerializer.java diff --git a/blade-core-boot/pom.xml b/blade-core-boot/pom.xml index 67b5149..d7f6114 100644 --- a/blade-core-boot/pom.xml +++ b/blade-core-boot/pom.xml @@ -83,6 +83,11 @@ swagger-bootstrap-ui ${swagger.bootstrapui.version} + + + org.springframework.boot + spring-boot-starter-data-redis + org.springblade diff --git a/blade-core-boot/src/main/java/org/springblade/core/boot/config/RedisTemplateConfiguration.java b/blade-core-boot/src/main/java/org/springblade/core/boot/config/RedisTemplateConfiguration.java new file mode 100644 index 0000000..a1ee36f --- /dev/null +++ b/blade-core-boot/src/main/java/org/springblade/core/boot/config/RedisTemplateConfiguration.java @@ -0,0 +1,78 @@ +/** + * Copyright (c) 2018-2028, Chill Zhuang 庄骞 (smallchill@163.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springblade.core.boot.config; + +import org.springblade.core.boot.redis.RedisKeySerializer; +import org.springframework.boot.autoconfigure.AutoConfigureBefore; +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; +import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration; +import org.springframework.cache.CacheManager; +import org.springframework.cache.annotation.EnableCaching; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheManager; +import org.springframework.data.redis.cache.RedisCacheWriter; +import org.springframework.data.redis.connection.RedisConnectionFactory; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer; +import org.springframework.data.redis.serializer.RedisSerializer; + +import java.time.Duration; + +/** + * RedisTemplate 配置 + */ +@EnableCaching +@Configuration +@AutoConfigureBefore(RedisAutoConfiguration.class) +public class RedisTemplateConfiguration { + + /** + * value 值 序列化 + * @return RedisSerializer + */ + @Bean + @ConditionalOnMissingBean(RedisSerializer.class) + public RedisSerializer redisSerializer() { + return new JdkSerializationRedisSerializer(); + } + + @Bean(name = "redisTemplate") + @ConditionalOnMissingBean(RedisTemplate.class) + public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer redisSerializer) { + RedisTemplate redisTemplate = new RedisTemplate<>(); + RedisKeySerializer redisKeySerializer = new RedisKeySerializer(); + // key 序列化 + redisTemplate.setKeySerializer(redisKeySerializer); + redisTemplate.setHashKeySerializer(redisKeySerializer); + // value 序列化 + redisTemplate.setValueSerializer(redisSerializer); + redisTemplate.setHashValueSerializer(redisSerializer); + redisTemplate.setConnectionFactory(redisConnectionFactory); + return redisTemplate; + } + + @Bean + public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) { + RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig() + .entryTtl(Duration.ofHours(1)); // 设置缓存有效期一小时 + return RedisCacheManager + .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory)) + .cacheDefaults(redisCacheConfiguration).build(); + } + +} diff --git a/blade-core-boot/src/main/java/org/springblade/core/boot/redis/RedisKeySerializer.java b/blade-core-boot/src/main/java/org/springblade/core/boot/redis/RedisKeySerializer.java new file mode 100644 index 0000000..1b183b4 --- /dev/null +++ b/blade-core-boot/src/main/java/org/springblade/core/boot/redis/RedisKeySerializer.java @@ -0,0 +1,75 @@ +/** + * Copyright (c) 2018-2028, DreamLu 卢春梦 (qq596392912@gmail.com). + *

+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *

+ * http://www.gnu.org/licenses/lgpl.html + *

+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.springblade.core.boot.redis; + +import org.springframework.cache.interceptor.SimpleKey; +import org.springframework.core.convert.ConversionService; +import org.springframework.core.convert.support.DefaultConversionService; +import org.springframework.data.redis.serializer.RedisSerializer; +import org.springframework.lang.Nullable; + +import java.nio.charset.Charset; +import java.nio.charset.StandardCharsets; +import java.util.Objects; + +/** + * 将redis key序列化为字符串 + * + *

+ * spring cache中的简单基本类型直接使用 StringRedisSerializer 会有问题 + *

+ * + * @author L.cm + */ +public class RedisKeySerializer implements RedisSerializer { + private final Charset charset; + private final ConversionService converter; + + public RedisKeySerializer() { + this(StandardCharsets.UTF_8); + } + + public RedisKeySerializer(Charset charset) { + Objects.requireNonNull(charset, "Charset must not be null"); + this.charset = charset; + this.converter = DefaultConversionService.getSharedInstance(); + } + + @Override + public Object deserialize(byte[] bytes) { + // redis keys 会用到反序列化 + if (bytes == null) { + return null; + } + return new String(bytes, charset); + } + + @Override + @Nullable + public byte[] serialize(Object object) { + Objects.requireNonNull(object, "redis key is null"); + String key; + if (object instanceof SimpleKey) { + key = ""; + } else if (object instanceof String) { + key = (String) object; + } else { + key = converter.convert(object, String.class); + } + return key.getBytes(this.charset); + } + +} diff --git a/blade-core-boot/src/main/resources/META-INF/spring.factories b/blade-core-boot/src/main/resources/META-INF/spring.factories index 6051062..40192f5 100644 --- a/blade-core-boot/src/main/resources/META-INF/spring.factories +++ b/blade-core-boot/src/main/resources/META-INF/spring.factories @@ -3,4 +3,5 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springblade.core.boot.logger.RequestLogAspect,\ org.springblade.core.boot.config.RetryConfiguration,\ org.springblade.core.boot.config.BladeWebMvcConfiguration,\ - org.springblade.core.boot.config.BladeBootAutoConfiguration + org.springblade.core.boot.config.BladeBootAutoConfiguration,\ + org.springblade.core.boot.config.RedisTemplateConfiguration