From c8d75255f561a2bf000075bb1d2eb91297f64c40 Mon Sep 17 00:00:00 2001 From: xxm Date: Tue, 6 Jul 2021 10:14:38 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=E5=A2=9E=E5=BC=BAcache,=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=80=BC=E4=B8=BA=E7=A9=BA=E4=B8=8D=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../starter/cache/CachingConfiguration.java | 2 +- .../starter/cache/tenant/BootxRedisCache.java | 35 +++++++++++++++++++ .../cache/tenant/TenantRedisCacheManager.java | 19 ++++++++-- 3 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/BootxRedisCache.java diff --git a/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/CachingConfiguration.java b/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/CachingConfiguration.java index a86437d..4c37891 100644 --- a/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/CachingConfiguration.java +++ b/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/CachingConfiguration.java @@ -87,7 +87,7 @@ public class CachingConfiguration extends CachingConfigurerSupport { .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(jackson2JsonRedisSerializer)) // 不缓存null .disableCachingNullValues() - // //覆盖默认的构造key,否则会多出一个冒号 + // 覆盖默认的构造key,否则会多出一个冒号 .computePrefixWith(name -> name + ":") // 过期时间 .entryTtl(duration); diff --git a/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/BootxRedisCache.java b/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/BootxRedisCache.java new file mode 100644 index 0000000..31e73d9 --- /dev/null +++ b/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/BootxRedisCache.java @@ -0,0 +1,35 @@ +package cn.bootx.starter.cache.tenant; + +import org.springframework.data.redis.cache.RedisCache; +import org.springframework.data.redis.cache.RedisCacheConfiguration; +import org.springframework.data.redis.cache.RedisCacheWriter; + +import javax.annotation.Nullable; +import java.util.Objects; + +/** +* 自定义RedisCache, 缓存值为空不报错 +* @author xxm +* @date 2021/7/6 +*/ +public class BootxRedisCache extends RedisCache { + /** + * Create new {@link RedisCache}. + * + * @param name must not be {@literal null}. + * @param cacheWriter must not be {@literal null}. + * @param cacheConfig must not be {@literal null}. + */ + protected BootxRedisCache(String name, RedisCacheWriter cacheWriter, RedisCacheConfiguration cacheConfig) { + super(name, cacheWriter, cacheConfig); + } + + @SuppressWarnings("NullableProblems") + @Override + public void put(Object key, @Nullable Object value) { + // 允许为空或者非空值 + if (isAllowNullValues() || Objects.nonNull(value)) { + super.put(key, value); + } + } +} diff --git a/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/TenantRedisCacheManager.java b/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/TenantRedisCacheManager.java index 29aa8f6..0c8244c 100644 --- a/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/TenantRedisCacheManager.java +++ b/bootx-starters/starter-cache/src/main/java/cn/bootx/starter/cache/tenant/TenantRedisCacheManager.java @@ -15,7 +15,7 @@ import java.util.Map; import java.util.Optional; /** -* 租户Redis缓存管理 +* 自定义Redis缓存管理 * @author xxm * @date 2021/6/11 */ @@ -24,25 +24,31 @@ public class TenantRedisCacheManager extends RedisCacheManager { private HeaderHolder headerHolder; @Setter private Map keysTtl; + private final RedisCacheWriter cacheWriter; public TenantRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration) { super(cacheWriter, defaultCacheConfiguration); + this.cacheWriter = cacheWriter; } public TenantRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, String... initialCacheNames) { super(cacheWriter, defaultCacheConfiguration, initialCacheNames); + this.cacheWriter = cacheWriter; } public TenantRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, boolean allowInFlightCacheCreation, String... initialCacheNames) { super(cacheWriter, defaultCacheConfiguration, allowInFlightCacheCreation, initialCacheNames); + this.cacheWriter = cacheWriter; } public TenantRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map initialCacheConfigurations) { super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations); + this.cacheWriter = cacheWriter; } public TenantRedisCacheManager(RedisCacheWriter cacheWriter, RedisCacheConfiguration defaultCacheConfiguration, Map initialCacheConfigurations, boolean allowInFlightCacheCreation) { super(cacheWriter, defaultCacheConfiguration, initialCacheConfigurations, allowInFlightCacheCreation); + this.cacheWriter = cacheWriter; } /** @@ -67,9 +73,16 @@ public class TenantRedisCacheManager extends RedisCacheManager { // 是自定义的key if (keyOptional.isPresent()){ String key = keyOptional.get(); - return super.createRedisCache(name, cacheConfig.entryTtl(Duration.ofSeconds(keysTtl.get(key)))); + return this.createBootxRedisCache(name, cacheConfig.entryTtl(Duration.ofSeconds(keysTtl.get(key)))); } - return super.createRedisCache(name, cacheConfig); + return this.createBootxRedisCache(name, cacheConfig); + } + + /** + * 替换为自定义的RedisCache + */ + public BootxRedisCache createBootxRedisCache(String name, RedisCacheConfiguration cacheConfig){ + return new BootxRedisCache(name, this.cacheWriter, cacheConfig); } } -- Gitee