From 2ee787f08b3927cb190689a9871bc63bc2fbf68c Mon Sep 17 00:00:00 2001 From: redkale Date: Mon, 11 Dec 2023 16:19:33 +0800 Subject: [PATCH] =?UTF-8?q?CacheManagerService=E4=BC=98=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../redkale/caching/CacheManagerService.java | 217 ++++-------------- .../java/org/redkale/caching/CacheValue.java | 4 + 2 files changed, 47 insertions(+), 174 deletions(-) diff --git a/src/main/java/org/redkale/caching/CacheManagerService.java b/src/main/java/org/redkale/caching/CacheManagerService.java index 873db5e21..611537044 100644 --- a/src/main/java/org/redkale/caching/CacheManagerService.java +++ b/src/main/java/org/redkale/caching/CacheManagerService.java @@ -74,7 +74,9 @@ public class CacheManagerService implements CacheManager, Service { */ @Override public T localGet(final String map, final String key, final Type type) { - return get(localSource, map, key, type); + Type t = loadCacheType(type); + CacheValue val = localSource.hget(map, key, t); + return CacheValue.get(val); } /** @@ -89,7 +91,9 @@ public class CacheManagerService implements CacheManager, Service { */ @Override public void localSet(String map, String key, Type type, T value, Duration expire) { - set(localSource, map, key, type, value, expire); + Type t = loadCacheType(type, value); + CacheValue val = CacheValue.create(value, expire); + localSource.hset(map, key, t, val); } /** @@ -102,7 +106,7 @@ public class CacheManagerService implements CacheManager, Service { */ @Override public long localDel(String map, String key) { - return del(localSource, map, key); + return localSource.hdel(map, key); } //-------------------------------------- 远程缓存 -------------------------------------- @@ -118,7 +122,9 @@ public class CacheManagerService implements CacheManager, Service { */ @Override public T remoteGet(final String map, final String key, final Type type) { - return get(remoteSource, map, key, type); + Type t = loadCacheType(type); + CacheValue val = remoteSource.hget(map, key, t); + return CacheValue.get(val); } /** @@ -133,7 +139,9 @@ public class CacheManagerService implements CacheManager, Service { */ @Override public CompletableFuture remoteGetAsync(final String map, final String key, final Type type) { - return getAsync(remoteSource, map, key, type); + Type t = loadCacheType(type); + CompletableFuture> future = remoteSource.hgetAsync(map, key, t); + return future.thenApply(CacheValue::get); } /** @@ -147,7 +155,9 @@ public class CacheManagerService implements CacheManager, Service { * @param expire 过期时长,为null表示永不过期 */ public void remoteSet(final String map, final String key, final Type type, final T value, Duration expire) { - set(remoteSource, map, key, type, value, expire); + Type t = loadCacheType(type, value); + CacheValue val = CacheValue.create(value, expire); + remoteSource.hset(map, key, t, val); } /** @@ -161,7 +171,9 @@ public class CacheManagerService implements CacheManager, Service { * @param expire 过期时长,为null表示永不过期 */ public CompletableFuture remoteSetAsync(String map, String key, Type type, T value, Duration expire) { - return setAsync(remoteSource, map, key, type, value, expire); + Type t = loadCacheType(type, value); + CacheValue val = CacheValue.create(value, expire); + return remoteSource.hsetAsync(map, key, t, val); } /** @@ -173,7 +185,7 @@ public class CacheManagerService implements CacheManager, Service { * @return 删除数量 */ public long remoteDel(String map, String key) { - return del(remoteSource, map, key); + return remoteSource.hdel(map, key); } /** @@ -185,7 +197,7 @@ public class CacheManagerService implements CacheManager, Service { * @return 删除数量 */ public CompletableFuture remoteDelAsync(String map, String key) { - return delAsync(remoteSource, map, key); + return remoteSource.hdelAsync(map, key); } //-------------------------------------- both缓存 -------------------------------------- @@ -200,8 +212,12 @@ public class CacheManagerService implements CacheManager, Service { * @return 数据值 */ public T bothGet(final String map, final String key, final Type type) { - T val = get(localSource, map, key, type); - return val == null ? get(remoteSource, map, key, type) : val; + Type t = loadCacheType(type); + CacheValue val = localSource.hget(map, key, t); + if (val != null && !val.isExpired()) { + return val.getValue(); + } + return CacheValue.get(remoteSource.hget(map, key, t)); } /** @@ -215,35 +231,13 @@ public class CacheManagerService implements CacheManager, Service { * @return 数据值 */ public CompletableFuture bothGetAsync(final String map, final String key, final Type type) { - T val = get(localSource, map, key, type); - if (val != null) { - return CompletableFuture.completedFuture(val); + Type t = loadCacheType(type); + CacheValue val = localSource.hget(map, key, t); + if (val != null && !val.isExpired()) { + return CompletableFuture.completedFuture(val.getValue()); } - return getAsync(remoteSource, map, key, type); - } - - /** - * 远程获取字符串缓存数据, 过期返回null - * - * @param map 缓存hash - * @param key 缓存键 - * - * @return 数据值 - */ - public final String bothGetString(final String map, final String key) { - return bothGet(map, key, String.class); - } - - /** - * 远程异步获取字符串缓存数据, 过期返回null - * - * @param map 缓存hash - * @param key 缓存键 - * - * @return 数据值 - */ - public final CompletableFuture bothGetStringAsync(final String map, final String key) { - return bothGetAsync(map, key, String.class); + CompletableFuture> future = remoteSource.hgetAsync(map, key, t); + return future.thenApply(CacheValue::get); } /** @@ -258,8 +252,9 @@ public class CacheManagerService implements CacheManager, Service { * @param remoteExpire 远程过期时长,为null表示永不过期 */ public void bothSet(final String map, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) { - set(localSource, map, key, type, value, localExpire); - set(remoteSource, map, key, type, value, remoteExpire); + Type t = loadCacheType(type, value); + localSource.hset(map, key, t, CacheValue.create(value, localExpire)); + remoteSource.hset(map, key, t, CacheValue.create(value, remoteExpire)); } /** @@ -274,34 +269,9 @@ public class CacheManagerService implements CacheManager, Service { * @param remoteExpire 远程过期时长,为null表示永不过期 */ public CompletableFuture bothSetAsync(String map, String key, Type type, T value, Duration localExpire, Duration remoteExpire) { - set(localSource, map, key, type, value, localExpire); - return setAsync(remoteSource, map, key, type, value, remoteExpire); - } - - /** - * 远程缓存字符串数据 - * - * @param map 缓存hash - * @param key 缓存键 - * @param value 数据值 - * @param localExpire 本地过期时长,为null表示永不过期 - * @param remoteExpire 远程过期时长,为null表示永不过期 - */ - public void bothSetString(final String map, final String key, final String value, Duration localExpire, Duration remoteExpire) { - bothSet(map, key, String.class, value, localExpire, remoteExpire); - } - - /** - * 远程异步缓存字符串数据 - * - * @param map 缓存hash - * @param key 缓存键 - * @param value 数据值 - * @param localExpire 本地过期时长,为null表示永不过期 - * @param remoteExpire 远程过期时长,为null表示永不过期 - */ - public CompletableFuture bothSetStringAsync(String map, String key, String value, Duration localExpire, Duration remoteExpire) { - return bothSetAsync(map, key, String.class, value, localExpire, remoteExpire); + Type t = loadCacheType(type, value); + localSource.hset(map, key, t, CacheValue.create(value, localExpire)); + return remoteSource.hsetAsync(map, key, t, CacheValue.create(value, remoteExpire)); } /** @@ -313,8 +283,8 @@ public class CacheManagerService implements CacheManager, Service { * @return 删除数量 */ public long bothDel(String map, String key) { - del(localSource, map, key); - return del(remoteSource, map, key); + localSource.hdel(map, key); + return remoteSource.hdel(map, key); } /** @@ -326,112 +296,11 @@ public class CacheManagerService implements CacheManager, Service { * @return 删除数量 */ public CompletableFuture bothDelAsync(String map, String key) { - del(localSource, map, key); - return delAsync(remoteSource, map, key); + localSource.hdel(map, key); + return remoteSource.hdelAsync(map, key); } //-------------------------------------- 内部方法 -------------------------------------- - /** - * 获取缓存数据, 过期返回null - * - * @param 泛型 - * @param source 缓存源 - * @param map 缓存hash - * @param key 缓存键 - * @param type 数据类型 - * - * @return 数据值 - */ - protected T get(final CacheSource source, final String map, final String key, final Type type) { - CacheValue val = source.hget(map, key, loadCacheType(type)); - return val != null && !val.isExpired() ? val.getValue() : null; - } - - /** - * 获取缓存数据, 过期返回null - * - * @param 泛型 - * @param source 缓存源 - * @param map 缓存hash - * @param key 缓存键 - * @param type 数据类型 - * - * @return 数据值 - */ - protected CompletableFuture getAsync(final CacheSource source, final String map, final String key, final Type type) { - if (source == null) { - return CompletableFuture.failedFuture(new NullPointerException(CacheManager.class.getSimpleName() + ".source is null")); - } - return source.hgetAsync(map, key, loadCacheType(type)).thenApply(v -> { - CacheValue val = (CacheValue) v; - return val != null && !val.isExpired() ? (T) val.getValue() : null; - }); - } - - /** - * 缓存数据 - * - * @param 泛型 - * @param source 缓存源 - * @param map 缓存hash - * @param key 缓存键 - * @param type 数据类型 - * @param value 数据值 - * @param expire 过期时长,为null表示永不过期 - */ - protected void set(CacheSource source, String map, String key, Type type, T value, Duration expire) { - Type t = loadCacheType(type, value); - source.hset(map, key, t, CacheValue.create(value, expire)); - } - - /** - * 缓存数据 - * - * @param 泛型 - * @param source 缓存源 - * @param map 缓存hash - * @param key 缓存键 - * @param type 数据类型 - * @param value 数据值 - * @param expire 过期时长,为null表示永不过期 - */ - protected CompletableFuture setAsync(CacheSource source, String map, String key, Type type, T value, Duration expire) { - if (source == null) { - return CompletableFuture.failedFuture(new NullPointerException(CacheManager.class.getSimpleName() + ".source is null")); - } - Type t = loadCacheType(type, value); - return source.hsetAsync(map, key, t, CacheValue.create(value, expire)); - } - - /** - * 删除缓存数据 - * - * @param source 缓存源 - * @param map 缓存hash - * @param key 缓存键 - * - * @return 删除数量 - */ - protected long del(final CacheSource source, String map, String key) { - return source.hdel(map, key); - } - - /** - * 删除缓存数据 - * - * @param source 缓存源 - * @param map 缓存hash - * @param key 缓存键 - * - * @return 删除数量 - */ - protected CompletableFuture delAsync(final CacheSource source, String map, String key) { - if (source == null) { - return CompletableFuture.failedFuture(new NullPointerException(CacheManager.class.getSimpleName() + ".source is null")); - } - return source.hdelAsync(map, key); - } - /** * 创建数据类型创建对应CacheValue泛型 * diff --git a/src/main/java/org/redkale/caching/CacheValue.java b/src/main/java/org/redkale/caching/CacheValue.java index c726d62af..89118fd13 100644 --- a/src/main/java/org/redkale/caching/CacheValue.java +++ b/src/main/java/org/redkale/caching/CacheValue.java @@ -41,6 +41,10 @@ public class CacheValue { return new CacheValue(value, expire); } + public static T get(CacheValue val) { + return val != null && !val.isExpired() ? (T) val.getValue() : null; + } + @ConvertDisabled public boolean isExpired() { return time > 0 && System.currentTimeMillis() > time;