From 9df80032a7c65138f9f0d767a12184b10dac9b00 Mon Sep 17 00:00:00 2001 From: redkale Date: Wed, 13 Dec 2023 08:52:38 +0800 Subject: [PATCH] =?UTF-8?q?CacheManager=E6=94=AF=E6=8C=81nullable?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/org/redkale/cache/CacheManager.java | 22 +++++--- .../cache/support/CacheManagerService.java | 53 +++++++++++-------- 2 files changed, 46 insertions(+), 29 deletions(-) diff --git a/src/main/java/org/redkale/cache/CacheManager.java b/src/main/java/org/redkale/cache/CacheManager.java index 18c3b9ffa..0baf12436 100644 --- a/src/main/java/org/redkale/cache/CacheManager.java +++ b/src/main/java/org/redkale/cache/CacheManager.java @@ -52,12 +52,13 @@ public interface CacheManager { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public T localGet(final String hash, final String key, final Type type, Duration expire, Supplier supplier); + public T localGet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier supplier); /** * 本地异步获取缓存数据, 过期返回null @@ -66,12 +67,13 @@ public interface CacheManager { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public CompletableFuture localGetAsync(String hash, String key, Type type, Duration expire, Supplier> supplier); + public CompletableFuture localGetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier> supplier); /** * 本地缓存数据 @@ -163,12 +165,14 @@ public interface CacheManager { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public T remoteGet(final String hash, final String key, final Type type, Duration expire, Supplier supplier); + public T remoteGet(final String hash, final String key, final Type type, boolean nullable, + Duration expire, Supplier supplier); /** * 远程异步获取缓存数据, 过期返回null @@ -177,12 +181,14 @@ public interface CacheManager { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public CompletableFuture remoteGetAsync(String hash, String key, Type type, Duration expire, Supplier> supplier); + public CompletableFuture remoteGetAsync(String hash, String key, Type type, boolean nullable, + Duration expire, Supplier> supplier); /** * 远程缓存数据 @@ -308,13 +314,15 @@ public interface CacheManager { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param localExpire 本地过期时长,为null表示永不过期 * @param remoteExpire 远程过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public T bothGet(String hash, String key, Type type, Duration localExpire, Duration remoteExpire, Supplier supplier); + public T bothGet(String hash, String key, Type type, boolean nullable, + Duration localExpire, Duration remoteExpire, Supplier supplier); /** * 本地或远程异步获取缓存数据, 过期返回null @@ -323,13 +331,15 @@ public interface CacheManager { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param localExpire 本地过期时长,为null表示永不过期 * @param remoteExpire 远程过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public CompletableFuture bothGetAsync(String hash, String key, Type type, Duration localExpire, Duration remoteExpire, Supplier> supplier); + public CompletableFuture bothGetAsync(String hash, String key, Type type, boolean nullable, + Duration localExpire, Duration remoteExpire, Supplier> supplier); /** * 本地和远程缓存数据 diff --git a/src/main/java/org/redkale/cache/support/CacheManagerService.java b/src/main/java/org/redkale/cache/support/CacheManagerService.java index caa11f6a0..fd7e9d2f4 100644 --- a/src/main/java/org/redkale/cache/support/CacheManagerService.java +++ b/src/main/java/org/redkale/cache/support/CacheManagerService.java @@ -46,9 +46,6 @@ public class CacheManagerService implements CacheManager, Service { //是否开启缓存 protected boolean enabled = true; - //是否缓存null值 - protected boolean nullable = false; - //配置 protected AnyValue config; @@ -157,13 +154,14 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public T localGet(final String hash, final String key, final Type type, Duration expire, Supplier supplier) { - return get(localSource::hget, localSource::hset, hash, key, type, expire, supplier); + public T localGet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier supplier) { + return get(localSource::hget, localSource::hset, hash, key, type, nullable, expire, supplier); } /** @@ -173,14 +171,15 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ @Override - public CompletableFuture localGetAsync(String hash, String key, Type type, Duration expire, Supplier> supplier) { - return getAsync(localSource::hgetAsync, localSource::hsetAsync, hash, key, type, expire, supplier); + public CompletableFuture localGetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier> supplier) { + return getAsync(localSource::hgetAsync, localSource::hsetAsync, hash, key, type, nullable, expire, supplier); } /** @@ -255,13 +254,14 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public T remoteGet(final String hash, final String key, final Type type, Duration expire, Supplier supplier) { - return get(remoteSource::hget, remoteSource::hset, hash, key, type, expire, supplier); + public T remoteGet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier supplier) { + return get(remoteSource::hget, remoteSource::hset, hash, key, type, nullable, expire, supplier); } /** @@ -271,13 +271,14 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ - public CompletableFuture remoteGetAsync(String hash, String key, Type type, Duration expire, Supplier> supplier) { - return getAsync(remoteSource::hgetAsync, remoteSource::hsetAsync, hash, key, type, expire, supplier); + public CompletableFuture remoteGetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier> supplier) { + return getAsync(remoteSource::hgetAsync, remoteSource::hsetAsync, hash, key, type, nullable, expire, supplier); } /** @@ -374,6 +375,8 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * + * @param nullable 是否缓存null值 * @param localExpire 本地过期时长,为null表示永不过期 * @param remoteExpire 远程过期时长,为null表示永不过期 * @param supplier 数据函数 @@ -381,13 +384,13 @@ public class CacheManagerService implements CacheManager, Service { * @return 数据值 */ @Override - public T bothGet(final String hash, final String key, final Type type, Duration localExpire, Duration remoteExpire, Supplier supplier) { + public T bothGet(final String hash, final String key, final Type type, boolean nullable, Duration localExpire, Duration remoteExpire, Supplier supplier) { return get(this::bothGetCache, (h, k, t, v) -> { localSource.hset(key, key, type, v); if (remoteSource != null) { remoteSource.hset(hash, key, t, CacheValue.create(v.getValue(), remoteExpire)); } - }, hash, key, type, localExpire, supplier); + }, hash, key, type, nullable, localExpire, supplier); } /** @@ -397,6 +400,7 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param localExpire 本地过期时长,为null表示永不过期 * @param remoteExpire 远程过期时长,为null表示永不过期 * @param supplier 数据函数 @@ -404,7 +408,7 @@ public class CacheManagerService implements CacheManager, Service { * @return 数据值 */ @Override - public CompletableFuture bothGetAsync(String hash, String key, Type type, Duration localExpire, Duration remoteExpire, Supplier> supplier) { + public CompletableFuture bothGetAsync(String hash, String key, Type type, boolean nullable, Duration localExpire, Duration remoteExpire, Supplier> supplier) { return getAsync(this::bothGetCacheAsync, (h, k, t, v) -> { localSource.hset(key, key, type, v); if (remoteSource != null) { @@ -412,7 +416,7 @@ public class CacheManagerService implements CacheManager, Service { } else { return CompletableFuture.completedFuture(null); } - }, hash, key, type, localExpire, supplier); + }, hash, key, type, nullable, localExpire, supplier); } /** @@ -504,13 +508,14 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ protected T get(GetterFunc> getter, SetterSyncFunc setter, - String hash, String key, Type type, Duration expire, Supplier supplier) { + String hash, String key, Type type, boolean nullable, Duration expire, Supplier supplier) { Objects.requireNonNull(supplier); final Type t = loadCacheType(type); CacheValue val = getter.apply(hash, key, t); @@ -522,7 +527,7 @@ public class CacheManagerService implements CacheManager, Service { if (CacheValue.isValid(oldVal)) { return oldVal; } - CacheValue newVal = toCacheSupplier(expire, supplier).get(); + CacheValue newVal = toCacheSupplier(nullable, expire, supplier).get(); if (CacheValue.isValid(newVal)) { setter.apply(hash, key, t, newVal); } @@ -546,13 +551,14 @@ public class CacheManagerService implements CacheManager, Service { * @param hash 缓存hash * @param key 缓存键 * @param type 数据类型 + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 * @param supplier 数据函数 * * @return 数据值 */ protected CompletableFuture getAsync(GetterFunc>> getter, SetterAsyncFunc setter, - String hash, String key, Type type, Duration expire, Supplier> supplier) { + String hash, String key, Type type, boolean nullable, Duration expire, Supplier> supplier) { Objects.requireNonNull(supplier); final Type t = loadCacheType(type); CompletableFuture> sourceFuture = getter.apply(hash, key, t); @@ -569,7 +575,7 @@ public class CacheManagerService implements CacheManager, Service { if (e != null) { entry.fail(e); } - CacheValue rs = toCacheValue(expire, v); + CacheValue rs = toCacheValue(nullable, expire, v); if (CacheValue.isValid(val)) { setter.apply(hash, key, t, val) .whenComplete((v2, e2) -> entry.success(CacheValue.get(rs))); @@ -636,12 +642,13 @@ public class CacheManagerService implements CacheManager, Service { /** * 将原始数据函数转换成获取CacheValue数据函数 * + * @param nullable 是否缓存null值 * @param expire 过期时长,为null表示永不过期 - * @param supplier 数据函数 + * @param value 缓存值 * * @return CacheValue函数 */ - protected CacheValue toCacheValue(Duration expire, T value) { + protected CacheValue toCacheValue(boolean nullable, Duration expire, T value) { if (value == null) { return nullable ? CacheValue.create(value, expire) : null; } @@ -656,8 +663,8 @@ public class CacheManagerService implements CacheManager, Service { * * @return CacheValue函数 */ - protected Supplier> toCacheSupplier(Duration expire, Supplier supplier) { - return () -> toCacheValue(expire, supplier.get()); + protected Supplier> toCacheSupplier(boolean nullable, Duration expire, Supplier supplier) { + return () -> toCacheValue(nullable, expire, supplier.get()); } /**