diff --git a/src/main/java/org/redkale/caching/CacheManagerService.java b/src/main/java/org/redkale/caching/CacheManagerService.java index f896fc7ac..d69782fbe 100644 --- a/src/main/java/org/redkale/caching/CacheManagerService.java +++ b/src/main/java/org/redkale/caching/CacheManagerService.java @@ -226,7 +226,11 @@ public class CacheManagerService implements CacheManager, Service { if (val != null && !val.isExpired()) { return val.getValue(); } - return CacheValue.get(remoteSource.hget(hash, key, t)); + if (remoteSource != null) { + return CacheValue.get(remoteSource.hget(hash, key, t)); + } else { + return null; + } } /** @@ -245,8 +249,12 @@ public class CacheManagerService implements CacheManager, Service { if (val != null && !val.isExpired()) { return CompletableFuture.completedFuture(val.getValue()); } - CompletableFuture> future = remoteSource.hgetAsync(hash, key, t); - return future.thenApply(CacheValue::get); + if (remoteSource != null) { + CompletableFuture> future = remoteSource.hgetAsync(hash, key, t); + return future.thenApply(CacheValue::get); + } else { + return CompletableFuture.completedFuture(null); + } } /** @@ -263,7 +271,9 @@ public class CacheManagerService implements CacheManager, Service { public void bothSet(final String hash, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) { Type t = loadCacheType(type, value); localSource.hset(hash, key, t, CacheValue.create(value, localExpire)); - remoteSource.hset(hash, key, t, CacheValue.create(value, remoteExpire)); + if (remoteSource != null) { + remoteSource.hset(hash, key, t, CacheValue.create(value, remoteExpire)); + } } /** @@ -280,7 +290,11 @@ public class CacheManagerService implements CacheManager, Service { public CompletableFuture bothSetAsync(String hash, String key, Type type, T value, Duration localExpire, Duration remoteExpire) { Type t = loadCacheType(type, value); localSource.hset(hash, key, t, CacheValue.create(value, localExpire)); - return remoteSource.hsetAsync(hash, key, t, CacheValue.create(value, remoteExpire)); + if (remoteSource != null) { + return remoteSource.hsetAsync(hash, key, t, CacheValue.create(value, remoteExpire)); + } else { + return CompletableFuture.completedFuture(null); + } } /** @@ -292,8 +306,12 @@ public class CacheManagerService implements CacheManager, Service { * @return 删除数量 */ public long bothDel(String hash, String key) { - localSource.hdel(hash, key); - return remoteSource.hdel(hash, key); + long v = localSource.hdel(hash, key); + if (remoteSource != null) { + return remoteSource.hdel(hash, key); + } else { + return v; + } } /** @@ -305,8 +323,12 @@ public class CacheManagerService implements CacheManager, Service { * @return 删除数量 */ public CompletableFuture bothDelAsync(String hash, String key) { - localSource.hdel(hash, key); - return remoteSource.hdelAsync(hash, key); + long v = localSource.hdel(hash, key); + if (remoteSource != null) { + return remoteSource.hdelAsync(hash, key); + } else { + return CompletableFuture.completedFuture(v); + } } //-------------------------------------- 内部方法 --------------------------------------