diff --git a/docs/cached.md b/docs/cached.md index 4278b689c..1c52d3bb5 100644 --- a/docs/cached.md +++ b/docs/cached.md @@ -9,6 +9,7 @@ ## 属性说明 |属性|默认值|说明| | --- | --- | --- | +|name|未定义|缓存的名称| |key|未定义|缓存的key,支持参数动态组合,比如"key_#{id}"| |manager|空|缓存管理器名称, 不能含有':'、'#'、'@'字符| |localExpire|-1|本地缓存过期时长, 0表示永不过期, -1表示不作本地缓存。
参数值支持方式:
 100: 设置数值
 ${env.cache.expires}: 读取系统配置项 | @@ -21,7 +22,7 @@ ## 基本用法   将结果进行本地缓存30秒且远程缓存60秒 ```java - @Cached(key = "name", localExpire = "30", remoteExpire = "60") + @Cached(name = "name", key = "name", localExpire = "30", remoteExpire = "60") public String getName() { return "haha"; } @@ -29,7 +30,7 @@   以参数code为key将结果进行本地缓存(时长由环境变量```env.cache.expire```配置,没配置采用默认值30秒) ```java - @Cached(key = "#{code}", localExpire = "${env.cache.expire:30}") + @Cached(name = "name", key = "#{code}", localExpire = "${env.cache.expire:30}") public CompletableFuture getNameAsync(String code) { return redis.getStringAsync(code); } @@ -42,10 +43,10 @@ //实时修改远程缓存的key值 public void updateName(String code, Map map) { - cachedManager.remoteSetString(code + "_" + map.get("id"), code + "-" + map, Duration.ofMillis(60)); + cachedManager.remoteSetString("name", code + "_" + map.get("id"), code + "-" + map, Duration.ofMillis(60)); } - @Cached(key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) + @Cached(name = "name", key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) public String getName(String code, Map map) { return code + "-" + map; } @@ -58,14 +59,14 @@ @Resource private CachedManager cachedManager; - @Cached(key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) + @Cached(name = "name", key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) public String getName(String code, Map map) { return code + "-" + map; } public void updateExpire() { Duration expire = Duration.ofMillis(600); - cachedManager.acceptCachedAction("#{code}_#{map.id}", action -> { + cachedManager.acceptCachedAction("name", action -> { //将缓存时长改成600毫秒,并开启本地缓存 action.setLocalExpire(expire); action.setRemoteExpire(expire); @@ -104,17 +105,17 @@ //第一个缓存器实时修改远程缓存的key值 public void updateName(String code, Map map) { - cachedManager.remoteSetString(code + "_" + map.get("id"), code + "-" + map, Duration.ofMillis(60)); + cachedManager.remoteSetString("name", code + "_" + map.get("id"), code + "-" + map, Duration.ofMillis(60)); } //使用第一个缓存器 - @Cached(key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) + @Cached(name = "name", key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) public String getName(String code, Map map) { return code + "-" + map; } //使用第二个缓存器 - @Cached(manager = "backup", key = "#{code}_#{map.id}_2", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) + @Cached(manager = "backup", name = "name", key = "#{code}_#{map.id}_2", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) public String getName2(String code, Map map) { return code + "-" + map; } diff --git a/src/main/java/org/redkale/cached/Cached.java b/src/main/java/org/redkale/cached/Cached.java index 909de7072..1edddf079 100644 --- a/src/main/java/org/redkale/cached/Cached.java +++ b/src/main/java/org/redkale/cached/Cached.java @@ -17,6 +17,8 @@ import org.redkale.service.LoadMode; * 2、方法返回类型必须可json序列化
* 3、方法必须是protected/public
* 4、方法不能是final/static
+ *
+ * 远程缓存里中存放的key值为: {CachedManager.schema}:{Cached.name}:{Cached.key} * * @since 2.8.0 */ @@ -25,11 +27,18 @@ import org.redkale.service.LoadMode; @Retention(RUNTIME) public @interface Cached { + /** + * 缓存的name
+ * + * @return name + */ + String name(); + /** * 缓存的key,支持参数动态组合,比如"key_#{id}"
* '@'开头的key值视为CacheKeyGenerator对象名称
* - * @see org.redkale.cached.spi.CachedKeyGenerator#name() + * @see org.redkale.cached.spi.CachedKeyGenerator#key() * * @return 键 */ diff --git a/src/main/java/org/redkale/cached/CachedManager.java b/src/main/java/org/redkale/cached/CachedManager.java index ebac3faa6..7521b5b9e 100644 --- a/src/main/java/org/redkale/cached/CachedManager.java +++ b/src/main/java/org/redkale/cached/CachedManager.java @@ -91,39 +91,42 @@ public interface CachedManager extends Resourcable { public List getCachedActions(); /** - * 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction}
+ * 处理指定缓存名称的{@link org.redkale.cached.spi.CachedAction}
* 可用于动态调整缓存时长 * - * @param templetKey 模板key + * @param name 缓存名称 * @param consumer 处理函数 */ - public void acceptCachedAction(String templetKey, Consumer consumer); + public void acceptCachedAction(String name, Consumer consumer); // -------------------------------------- 本地缓存 -------------------------------------- /** * 本地获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ - public T localGet(final String key, final Type type); + public T localGet(String name, String key, Type type); /** * 本地获取字符串缓存数据, 过期返回null * + * @param name 缓存名称 * @param key 缓存键 * @return 数据值 */ - default String localGetString(final String key) { - return localGet(key, String.class); + default String localGetString(String name, String key) { + return localGet(name, key, String.class); } /** * 本地获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -131,12 +134,14 @@ public interface CachedManager extends Resourcable { * @param supplier 数据函数 * @return 数据值 */ - public T localGetSet(String key, Type type, boolean nullable, Duration expire, ThrowSupplier supplier); + public T localGetSet( + String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier supplier); /** * 本地异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -145,83 +150,96 @@ public interface CachedManager extends Resourcable { * @return 数据值 */ public CompletableFuture localGetSetAsync( - String key, Type type, boolean nullable, Duration expire, ThrowSupplier> supplier); + String name, + String key, + Type type, + boolean nullable, + Duration expire, + ThrowSupplier> supplier); /** * 本地缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 */ - public void localSet(String key, Type type, T value, Duration expire); + public void localSet(String name, String key, Type type, T value, Duration expire); /** * 本地缓存字符串数据 * + * @param name 缓存名称 * @param key 缓存键 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 */ - default void localSetString(final String key, final String value, Duration expire) { - localSet(key, String.class, value, expire); + default void localSetString(String name, String key, String value, Duration expire) { + localSet(name, key, String.class, value, expire); } /** * 本地删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ - public long localDel(String key); + public long localDel(String name, String key); // -------------------------------------- 远程缓存 -------------------------------------- /** * 远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ - public T remoteGet(final String key, final Type type); + public T remoteGet(String name, String key, final Type type); /** * 远程获取字符串缓存数据, 过期返回null * + * @param name 缓存名称 * @param key 缓存键 * @return 数据值 */ - default String remoteGetString(final String key) { - return remoteGet(key, String.class); + default String remoteGetString(String name, String key) { + return remoteGet(name, key, String.class); } /** * 远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ - public CompletableFuture remoteGetAsync(final String key, final Type type); + public CompletableFuture remoteGetAsync(String name, String key, final Type type); /** * 远程异步获取字符串缓存数据, 过期返回null * + * @param name 缓存名称 * @param key 缓存键 * @return 数据值 */ - default CompletableFuture remoteGetStringAsync(final String key) { - return remoteGetAsync(key, String.class); + default CompletableFuture remoteGetStringAsync(String name, String key) { + return remoteGetAsync(name, key, String.class); } /** * 远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -229,12 +247,14 @@ public interface CachedManager extends Resourcable { * @param supplier 数据函数 * @return 数据值 */ - public T remoteGetSet(String key, Type type, boolean nullable, Duration expire, ThrowSupplier supplier); + public T remoteGetSet( + String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier supplier); /** * 远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -243,115 +263,131 @@ public interface CachedManager extends Resourcable { * @return 数据值 */ public CompletableFuture remoteGetSetAsync( - String key, Type type, boolean nullable, Duration expire, ThrowSupplier> supplier); + String name, + String key, + Type type, + boolean nullable, + Duration expire, + ThrowSupplier> supplier); /** * 远程缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 */ - public void remoteSet(final String key, final Type type, final T value, Duration expire); + public void remoteSet(String name, String key, Type type, T value, Duration expire); /** * 远程缓存字符串数据 * + * @param name 缓存名称 * @param key 缓存键 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 */ - default void remoteSetString(final String key, final String value, Duration expire) { - remoteSet(key, String.class, value, expire); + default void remoteSetString(String name, String key, String value, Duration expire) { + remoteSet(name, key, String.class, value, expire); } /** * 远程异步缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 * @return void */ - public CompletableFuture remoteSetAsync(String key, Type type, T value, Duration expire); + public CompletableFuture remoteSetAsync(String name, String key, Type type, T value, Duration expire); /** * 远程异步缓存字符串数据 * + * @param name 缓存名称 * @param key 缓存键 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 * @return void */ - default CompletableFuture remoteSetStringAsync(final String key, final String value, Duration expire) { - return remoteSetAsync(key, String.class, value, expire); + default CompletableFuture remoteSetStringAsync(String name, String key, String value, Duration expire) { + return remoteSetAsync(name, key, String.class, value, expire); } /** * 远程删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ - public long remoteDel(String key); + public long remoteDel(String name, String key); /** * 远程异步删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ - public CompletableFuture remoteDelAsync(String key); + public CompletableFuture remoteDelAsync(String name, String key); // -------------------------------------- both缓存 -------------------------------------- /** * 本地或远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ - public T bothGet(final String key, final Type type); + public T bothGet(String name, String key, Type type); /** * 本地或远程获取字符串缓存数据, 过期返回null * + * @param name 缓存名称 * @param key 缓存键 * @return 数据值 */ - default String bothGetString(final String key) { - return bothGet(key, String.class); + default String bothGetString(String name, String key) { + return bothGet(name, key, String.class); } /** * 本地或远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ - public CompletableFuture bothGetAsync(final String key, final Type type); + public CompletableFuture bothGetAsync(String name, String key, Type type); /** * 本地或远程异步获取字符串缓存数据, 过期返回null * + * @param name 缓存名称 * @param key 缓存键 * @return 数据值 */ - default CompletableFuture bothGetStringAsync(final String key) { - return bothGetAsync(key, String.class); + default CompletableFuture bothGetStringAsync(String name, String key) { + return bothGetAsync(name, key, String.class); } /** * 本地或远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -361,6 +397,7 @@ public interface CachedManager extends Resourcable { * @return 数据值 */ public T bothGetSet( + String name, String key, Type type, boolean nullable, @@ -372,6 +409,7 @@ public interface CachedManager extends Resourcable { * 本地或远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -381,6 +419,7 @@ public interface CachedManager extends Resourcable { * @return 数据值 */ public CompletableFuture bothGetSetAsync( + String name, String key, Type type, boolean nullable, @@ -392,30 +431,33 @@ public interface CachedManager extends Resourcable { * 本地和远程缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 * @param localExpire 本地过期时长,Duration.ZERO为永不过期,为null表示不本地缓存 * @param remoteExpire 远程过期时长,Duration.ZERO为永不过期,为null表示不远程缓存 */ - public void bothSet(String key, Type type, T value, Duration localExpire, Duration remoteExpire); + public void bothSet(String name, String key, Type type, T value, Duration localExpire, Duration remoteExpire); /** * 本地和远程缓存字符串数据 * + * @param name 缓存名称 * @param key 缓存键 * @param value 数据值 * @param localExpire 本地过期时长,Duration.ZERO为永不过期,为null表示不本地缓存 * @param remoteExpire 远程过期时长,Duration.ZERO为永不过期,为null表示不远程缓存 */ - default void bothSetString(final String key, final String value, Duration localExpire, Duration remoteExpire) { - bothSet(key, String.class, value, localExpire, remoteExpire); + default void bothSetString(String name, String key, String value, Duration localExpire, Duration remoteExpire) { + bothSet(name, key, String.class, value, localExpire, remoteExpire); } /** * 本地和远程异步缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 @@ -424,11 +466,12 @@ public interface CachedManager extends Resourcable { * @return void */ public CompletableFuture bothSetAsync( - String key, Type type, T value, Duration localExpire, Duration remoteExpire); + String name, String key, Type type, T value, Duration localExpire, Duration remoteExpire); /** * 本地和远程异步缓存字符串数据 * + * @param name 缓存名称 * @param key 缓存键 * @param value 数据值 * @param localExpire 本地过期时长,Duration.ZERO为永不过期,为null表示不本地缓存 @@ -436,23 +479,25 @@ public interface CachedManager extends Resourcable { * @return void */ default CompletableFuture bothSetStringAsync( - String key, String value, Duration localExpire, Duration remoteExpire) { - return bothSetAsync(key, String.class, value, localExpire, remoteExpire); + String name, String key, String value, Duration localExpire, Duration remoteExpire) { + return bothSetAsync(name, key, String.class, value, localExpire, remoteExpire); } /** * 本地和远程删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ - public long bothDel(String key); + public long bothDel(String name, String key); /** * 本地和远程异步删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ - public CompletableFuture bothDelAsync(String key); + public CompletableFuture bothDelAsync(String name, String key); } diff --git a/src/main/java/org/redkale/cached/spi/CachedAction.java b/src/main/java/org/redkale/cached/spi/CachedAction.java index 4b655b9ce..e603a6110 100644 --- a/src/main/java/org/redkale/cached/spi/CachedAction.java +++ b/src/main/java/org/redkale/cached/spi/CachedAction.java @@ -62,8 +62,11 @@ public class CachedAction { @Nullable private final String[] paramNames; + // 缓存名称 + final String name; + // 模板key - final String templetKey; + final String key; // 缓存key生成器 private CachedKeyGenerator keyGenerator; @@ -85,7 +88,8 @@ public class CachedAction { this.paramNames = paramNames; this.methodName = method.getName(); this.fieldName = Objects.requireNonNull(fieldName); - this.templetKey = cached.getTempletKey(); + this.name = cached.getName(); + this.key = cached.getKey(); Type returnType = method.getGenericReturnType(); this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType)); this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType; @@ -93,24 +97,25 @@ public class CachedAction { String init(ResourceFactory resourceFactory, Object service) { this.manager = resourceFactory.load(cached.getManager(), CachedManager.class); - final String key = environment.getPropertyValue(cached.getTempletKey()); - if (key.startsWith("@")) { // 动态加载缓存key生成器 - String generatorName = key.substring(1); + final String realKey = environment.getPropertyValue(cached.getKey()); + if (realKey.startsWith("@")) { // 动态加载缓存key生成器 + String generatorName = realKey.substring(1); this.keyGenerator = resourceFactory.findChild(generatorName, CachedKeyGenerator.class); } else { - MultiHashKey dynKey = MultiHashKey.create(paramNames, key); + MultiHashKey dynKey = MultiHashKey.create(paramNames, realKey); this.keyGenerator = CachedKeyGenerator.create(dynKey); } this.localExpire = createDuration(cached.getLocalExpire()); this.remoteExpire = createDuration(cached.getRemoteExpire()); ((CachedActionFunc) this.manager).addAction(this); - return key; + return realKey; } @ClassDepends public T get(ThrowSupplier supplier, Object... args) { if (async) { return (T) manager.bothGetSetAsync( + name, keyGenerator.generate(service, this, args), resultType, nullable, @@ -119,6 +124,7 @@ public class CachedAction { (ThrowSupplier) supplier); } else { return manager.bothGetSet( + name, keyGenerator.generate(service, this, args), resultType, nullable, @@ -161,8 +167,12 @@ public class CachedAction { return method; } - public String getTempletKey() { - return templetKey; + public String getName() { + return name; + } + + public String getKey() { + return key; } public Duration getLocalExpire() { @@ -189,7 +199,7 @@ public class CachedAction { + ",\"fieldName\":\"" + fieldName + "\"" + ",\"paramTypes\":" + JsonConvert.root().convertTo(method.getParameterTypes()) + ",\"paramNames\":" + JsonConvert.root().convertTo(paramNames) - + ",\"templetKey\":\"" + templetKey + "\"" + + ",\"templetKey\":\"" + key + "\"" + ",\"resultType\":\"" + resultType + "\"" + ",\"cache\":" + cached + "}"; diff --git a/src/main/java/org/redkale/cached/spi/CachedEntry.java b/src/main/java/org/redkale/cached/spi/CachedEntry.java index 30b1eb29f..24f2e82d7 100644 --- a/src/main/java/org/redkale/cached/spi/CachedEntry.java +++ b/src/main/java/org/redkale/cached/spi/CachedEntry.java @@ -20,7 +20,8 @@ public class CachedEntry { private String manager; - private String templetKey; + private String name; + private String key; private String localExpire; @@ -34,7 +35,8 @@ public class CachedEntry { public CachedEntry(DynForCached cached) { this.manager = cached.manager(); - this.templetKey = cached.key(); + this.name = cached.name(); + this.key = cached.key(); this.localExpire = cached.localExpire(); this.remoteExpire = cached.remoteExpire(); this.timeUnit = cached.timeUnit(); @@ -43,7 +45,8 @@ public class CachedEntry { public CachedEntry(Cached cached) { this.manager = cached.manager(); - this.templetKey = cached.key(); + this.name = cached.name(); + this.key = cached.key(); this.localExpire = cached.localExpire(); this.remoteExpire = cached.remoteExpire(); this.timeUnit = cached.timeUnit(); @@ -54,8 +57,12 @@ public class CachedEntry { return manager; } - public String getTempletKey() { - return templetKey; + public String getName() { + return name; + } + + public String getKey() { + return key; } public String getLocalExpire() { diff --git a/src/main/java/org/redkale/cached/spi/CachedEventMessage.java b/src/main/java/org/redkale/cached/spi/CachedEventMessage.java index 79d9dc108..1929401ac 100644 --- a/src/main/java/org/redkale/cached/spi/CachedEventMessage.java +++ b/src/main/java/org/redkale/cached/spi/CachedEventMessage.java @@ -21,6 +21,8 @@ public class CachedEventMessage implements Serializable { // CachedManager唯一标识 protected String node; + // name + protected String name; // key protected String key; @@ -29,8 +31,9 @@ public class CachedEventMessage implements Serializable { public CachedEventMessage() {} - public CachedEventMessage(String node, String key) { + public CachedEventMessage(String node, String name, String key) { this.node = node; + this.name = name; this.key = key; this.time = System.currentTimeMillis(); } @@ -43,6 +46,14 @@ public class CachedEventMessage implements Serializable { this.node = node; } + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + public String getKey() { return key; } diff --git a/src/main/java/org/redkale/cached/spi/CachedKeyGenerator.java b/src/main/java/org/redkale/cached/spi/CachedKeyGenerator.java index 51131104a..f0edf69ac 100644 --- a/src/main/java/org/redkale/cached/spi/CachedKeyGenerator.java +++ b/src/main/java/org/redkale/cached/spi/CachedKeyGenerator.java @@ -34,9 +34,9 @@ public interface CachedKeyGenerator { * * @see org.redkale.cached.Cached#key() * - * @return name + * @return key */ - public String name(); + public String key(); /** * 根据MultiHashKey生成一个CachedKeyGenerator @@ -52,7 +52,7 @@ public interface CachedKeyGenerator { } @Override - public String name() { + public String key() { return ""; } }; diff --git a/src/main/java/org/redkale/cached/spi/CachedKeyGeneratorLoader.java b/src/main/java/org/redkale/cached/spi/CachedKeyGeneratorLoader.java index 5588d0db9..45bf9438e 100644 --- a/src/main/java/org/redkale/cached/spi/CachedKeyGeneratorLoader.java +++ b/src/main/java/org/redkale/cached/spi/CachedKeyGeneratorLoader.java @@ -51,7 +51,7 @@ class CachedKeyGeneratorLoader implements ResourceTypeLoader { generator = generatorMap.computeIfAbsent(resourceName, n -> { for (CachedKeyGenerator instance : ServiceLoader.load( CachedKeyGenerator.class, engine.getApplication().getClassLoader())) { - if (Objects.equals(n, instance.name())) { + if (Objects.equals(n, instance.key())) { rf.inject(instance); if (instance instanceof Service) { ((Service) instance).init(null); diff --git a/src/main/java/org/redkale/cached/spi/CachedManagerService.java b/src/main/java/org/redkale/cached/spi/CachedManagerService.java index 85591d05d..25cdd1171 100644 --- a/src/main/java/org/redkale/cached/spi/CachedManagerService.java +++ b/src/main/java/org/redkale/cached/spi/CachedManagerService.java @@ -241,14 +241,12 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se /** * 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction} * - * @param templetKey 缓存key + * @param name 缓存名称 * @param consumer 处理函数 */ @Override - public void acceptCachedAction(String templetKey, Consumer consumer) { - actions.stream() - .filter(v -> Objects.equals(v.getTempletKey(), templetKey)) - .forEach(consumer); + public void acceptCachedAction(String name, Consumer consumer) { + actions.stream().filter(v -> Objects.equals(v.getName(), name)).forEach(consumer); } @Override @@ -261,20 +259,22 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 本地获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ @Override - public T localGet(final String key, final Type type) { + public T localGet(String name, String key, Type type) { checkEnable(); - return CachedValue.get(localSource.get(idFor(key), loadCacheType(type))); + return CachedValue.get(localSource.get(idFor(name, key), loadCacheType(type))); } /** * 本地获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -284,10 +284,11 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se */ @Override public T localGetSet( - final String key, final Type type, boolean nullable, Duration expire, ThrowSupplier supplier) { + String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier supplier) { return getSet( - (k, ex, ct) -> localSource.get(idFor(k), ct), + (n, k, ex, ct) -> localSource.get(idFor(n, k), ct), this::localSetCache, + name, key, type, nullable, @@ -299,6 +300,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 本地异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -308,10 +310,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se */ @Override public CompletableFuture localGetSetAsync( - String key, Type type, boolean nullable, Duration expire, ThrowSupplier> supplier) { + String name, + String key, + Type type, + boolean nullable, + Duration expire, + ThrowSupplier> supplier) { return getSetAsync( - (id, ex, ct) -> localSource.getAsync(id, ct), + (n, k, e, c) -> localSource.getAsync(idFor(n, k), c), this::localSetCacheAsync, + name, key, type, nullable, @@ -323,26 +331,28 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 本地缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 */ @Override - public void localSet(String key, Type type, T value, Duration expire) { - localSetCache(key, type, value, expire); + public void localSet(String name, String key, Type type, T value, Duration expire) { + localSetCache(name, key, type, value, expire); } /** * 本地删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ @Override - public long localDel(String key) { + public long localDel(String name, String key) { checkEnable(); - return localSource.del(idFor(key)); + return localSource.del(idFor(name, key)); } // -------------------------------------- 远程缓存 -------------------------------------- @@ -350,28 +360,30 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ @Override - public T remoteGet(final String key, final Type type) { + public T remoteGet(String name, String key, Type type) { checkEnable(); - return CachedValue.get(remoteSource.get(idFor(key), loadCacheType(type))); + return CachedValue.get(remoteSource.get(idFor(name, key), loadCacheType(type))); } /** * 远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ @Override - public CompletableFuture remoteGetAsync(final String key, final Type type) { + public CompletableFuture remoteGetAsync(String name, String key, Type type) { checkEnable(); - CompletableFuture> future = remoteSource.getAsync(idFor(key), loadCacheType(type)); + CompletableFuture> future = remoteSource.getAsync(idFor(name, key), loadCacheType(type)); return future.thenApply(CachedValue::get); } @@ -379,6 +391,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -388,10 +401,11 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se */ @Override public T remoteGetSet( - final String key, final Type type, boolean nullable, Duration expire, ThrowSupplier supplier) { + String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier supplier) { return getSet( - (k, ex, ct) -> remoteSource.get(idFor(k), ct), + (n, k, ex, ct) -> remoteSource.get(idFor(n, k), ct), this::remoteSetCache, + name, key, type, nullable, @@ -403,6 +417,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -412,10 +427,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se */ @Override public CompletableFuture remoteGetSetAsync( - String key, Type type, boolean nullable, Duration expire, ThrowSupplier> supplier) { + String name, + String key, + Type type, + boolean nullable, + Duration expire, + ThrowSupplier> supplier) { return getSetAsync( - (k, ex, ct) -> remoteSource.getAsync(idFor(k), ct), + (n, k, ex, ct) -> remoteSource.getAsync(idFor(n, k), ct), this::remoteSetCacheAsync, + name, key, type, nullable, @@ -427,52 +448,56 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 */ @Override - public void remoteSet(final String key, final Type type, final T value, Duration expire) { - remoteSetCache(key, type, value, expire); + public void remoteSet(String name, String key, Type type, T value, Duration expire) { + remoteSetCache(name, key, type, value, expire); } /** * 远程异步缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 * @param expire 过期时长,Duration.ZERO为永不过期 */ @Override - public CompletableFuture remoteSetAsync(String key, Type type, T value, Duration expire) { - return remoteSetCacheAsync(key, type, value, expire); + public CompletableFuture remoteSetAsync(String name, String key, Type type, T value, Duration expire) { + return remoteSetCacheAsync(name, key, type, value, expire); } /** * 远程删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ @Override - public long remoteDel(String key) { + public long remoteDel(String name, String key) { checkEnable(); - return remoteSource.del(idFor(key)); + return remoteSource.del(idFor(name, key)); } /** * 远程异步删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ @Override - public CompletableFuture remoteDelAsync(String key) { + public CompletableFuture remoteDelAsync(String name, String key) { checkEnable(); - return remoteSource.delAsync(idFor(key)); + return remoteSource.delAsync(idFor(name, key)); } // -------------------------------------- both缓存 -------------------------------------- @@ -480,32 +505,35 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ @Override - public T bothGet(final String key, final Type type) { - return CachedValue.get(bothGetCache(key, (Duration) null, type)); + public T bothGet(String name, String key, Type type) { + return CachedValue.get(bothGetCache(name, key, (Duration) null, type)); } /** * 远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @return 数据值 */ @Override - public CompletableFuture bothGetAsync(final String key, final Type type) { - return bothGetCacheAsync(key, (Duration) null, type).thenApply(CachedValue::get); + public CompletableFuture bothGetAsync(String name, String key, Type type) { + return bothGetCacheAsync(name, key, (Duration) null, type).thenApply(CachedValue::get); } /** * 远程获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -516,8 +544,9 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se */ @Override public T bothGetSet( - final String key, - final Type type, + String name, + String key, + Type type, boolean nullable, Duration localExpire, Duration remoteExpire, @@ -533,20 +562,21 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se } if (remoteExpire == null) { // 只有本地缓存 Objects.requireNonNull(localExpire); - return localGetSet(key, type, nullable, localExpire, supplier); + return localGetSet(name, key, type, nullable, localExpire, supplier); } if (localExpire == null) { // 只有远程缓存 Objects.requireNonNull(remoteExpire); - return remoteGetSet(key, type, nullable, remoteExpire, supplier); + return remoteGetSet(name, key, type, nullable, remoteExpire, supplier); } return getSet( this::bothGetCache, - (k, e, t, v) -> { - localSetCache(k, localExpire, t, v); + (n, k, e, t, v) -> { + localSetCache(n, k, localExpire, t, v); if (remoteSource != null) { - remoteSetCache(k, remoteExpire, t, v); + remoteSetCache(n, k, remoteExpire, t, v); } }, + name, key, type, nullable, @@ -558,6 +588,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -568,6 +599,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se */ @Override public CompletableFuture bothGetSetAsync( + String name, String key, Type type, boolean nullable, @@ -583,22 +615,23 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se } if (remoteExpire == null) { // 只有本地缓存 Objects.requireNonNull(localExpire); - return localGetSetAsync(key, type, nullable, localExpire, supplier); + return localGetSetAsync(name, key, type, nullable, localExpire, supplier); } if (localExpire == null) { // 只有远程缓存 Objects.requireNonNull(remoteExpire); - return remoteGetSetAsync(key, type, nullable, remoteExpire, supplier); + return remoteGetSetAsync(name, key, type, nullable, remoteExpire, supplier); } return getSetAsync( this::bothGetCacheAsync, - (k, e, t, v) -> { - localSetCache(k, localExpire, t, v); + (n, k, e, t, v) -> { + localSetCache(n, k, localExpire, t, v); if (remoteSource != null) { - return remoteSetCacheAsync(k, remoteExpire, t, v); + return remoteSetCacheAsync(n, k, remoteExpire, t, v); } else { return CompletableFuture.completedFuture(null); } }, + name, key, type, nullable, @@ -610,6 +643,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 @@ -617,17 +651,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * @param remoteExpire 远程过期时长,Duration.ZERO为永不过期,为null表示不远程缓存 */ @Override - public void bothSet( - final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) { + public void bothSet(String name, String key, Type type, T value, Duration localExpire, Duration remoteExpire) { checkEnable(); if (localExpire != null) { - localSetCache(key, type, value, localExpire); + localSetCache(name, key, type, value, localExpire); } if (remoteExpire != null && remoteSource != null) { - remoteSetCache(key, type, value, remoteExpire); + remoteSetCache(name, key, type, value, remoteExpire); } if (remoteSource != null && broadcastable) { - remoteSource.publish(getChannelTopic(), new CachedEventMessage(node, key)); + remoteSource.publish(getChannelTopic(), new CachedEventMessage(node, name, key)); } } @@ -635,6 +668,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程异步缓存数据 * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param value 数据值 @@ -644,18 +678,18 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se */ @Override public CompletableFuture bothSetAsync( - String key, Type type, T value, Duration localExpire, Duration remoteExpire) { + String name, String key, Type type, T value, Duration localExpire, Duration remoteExpire) { checkEnable(); if (localExpire != null) { - localSetCache(key, type, value, localExpire); + localSetCache(name, key, type, value, localExpire); } CompletableFuture future = CompletableFuture.completedFuture(null); if (remoteSource != null && remoteExpire != null) { - future = remoteSetCacheAsync(key, type, value, remoteExpire); + future = remoteSetCacheAsync(name, key, type, value, remoteExpire); } if (remoteSource != null && broadcastable) { future = future.thenCompose(r -> remoteSource - .publishAsync(getChannelTopic(), new CachedEventMessage(node, key)) + .publishAsync(getChannelTopic(), new CachedEventMessage(node, name, key)) .thenApply(n -> r)); } return future; @@ -664,18 +698,19 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se /** * 远程删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ @Override - public long bothDel(String key) { + public long bothDel(String name, String key) { checkEnable(); - String id = idFor(key); + String id = idFor(name, key); long v = localSource.del(id); if (remoteSource != null) { v = remoteSource.del(id); if (broadcastable) { - remoteSource.publish(getChannelTopic(), new CachedEventMessage(node, key)); + remoteSource.publish(getChannelTopic(), new CachedEventMessage(node, name, key)); } } return v; @@ -684,19 +719,20 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se /** * 远程异步删除缓存数据 * + * @param name 缓存名称 * @param key 缓存键 * @return 删除数量 */ @Override - public CompletableFuture bothDelAsync(String key) { + public CompletableFuture bothDelAsync(String name, String key) { checkEnable(); - String id = idFor(key); + String id = idFor(name, key); long v = localSource.del(id); // 内存操作,无需异步 if (remoteSource != null) { return remoteSource.delAsync(id).thenCompose(r -> { return broadcastable ? remoteSource - .publishAsync(getChannelTopic(), new CachedEventMessage(node, key)) + .publishAsync(getChannelTopic(), new CachedEventMessage(node, name, key)) .thenApply(n -> r) : CompletableFuture.completedFuture(v); }); @@ -712,6 +748,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * @param 泛型 * @param getter 获取数据函数 * @param setter 设置数据函数 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -722,6 +759,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se protected T getSet( GetterFunc> getter, SetterSyncFunc setter, + String name, String key, Type type, boolean nullable, @@ -732,8 +770,8 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se Objects.requireNonNull(expire); Objects.requireNonNull(supplier); final Type cacheType = loadCacheType(type); - final String id = idFor(key); - CachedValue cacheVal = getter.get(key, expire, cacheType); + final String id = idFor(name, key); + CachedValue cacheVal = getter.get(name, key, expire, cacheType); if (CachedValue.isValid(cacheVal)) { if (logable) { logger.log(logLevel, "Cached got id(" + id + ") value from eitherSource"); @@ -741,7 +779,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se return cacheVal.getVal(); } Function func = k -> { - CachedValue oldCacheVal = getter.get(key, expire, cacheType); + CachedValue oldCacheVal = getter.get(name, key, expire, cacheType); if (CachedValue.isValid(oldCacheVal)) { return oldCacheVal; } @@ -754,7 +792,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se throw new RedkaleException(t); } if (CachedValue.isValid(newCacheVal)) { - setter.set(key, expire, cacheType, newCacheVal); + setter.set(name, key, expire, cacheType, newCacheVal); } return newCacheVal; }; @@ -772,6 +810,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * @param 泛型 * @param getter 获取数据函数 * @param setter 设置数据函数 + * @param name 缓存名称 * @param key 缓存键 * @param type 数据类型 * @param nullable 是否缓存null值 @@ -782,6 +821,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se protected CompletableFuture getSetAsync( GetterFunc>> getter, SetterAsyncFunc setter, + String name, String key, Type type, boolean nullable, @@ -791,8 +831,8 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se boolean logable = logger.isLoggable(logLevel); Objects.requireNonNull(supplier); final Type cacheType = loadCacheType(type); - final String id = idFor(key); - CompletableFuture> sourceFuture = getter.get(key, expire, cacheType); + final String id = idFor(name, key); + CompletableFuture> sourceFuture = getter.get(name, key, expire, cacheType); return sourceFuture.thenCompose(val -> { if (CachedValue.isValid(val)) { if (logable) { @@ -810,7 +850,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se } CachedValue cacheVal = toCacheValue(nullable, v); if (CachedValue.isValid(cacheVal)) { - setter.set(key, expire, cacheType, cacheVal) + setter.set(name, key, expire, cacheType, cacheVal) .whenComplete((v2, e2) -> lock.success(CachedValue.get(cacheVal))); } else { lock.success(CachedValue.get(cacheVal)); @@ -824,16 +864,17 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se }); } - protected void localSetCache(String key, Type type, T value, Duration expire) { - localSetCache(key, expire, loadCacheType(type, value), CachedValue.create(value)); + protected void localSetCache(String name, String key, Type type, T value, Duration expire) { + localSetCache(name, key, expire, loadCacheType(type, value), CachedValue.create(value)); } - protected void localSetCache(String key, Duration expire, Type cacheType, CachedValue cacheVal) { + protected void localSetCache( + String name, String key, Duration expire, Type cacheType, CachedValue cacheVal) { checkEnable(); boolean logable = logger.isLoggable(logLevel); Objects.requireNonNull(expire); long millis = expire.toMillis(); - String id = idFor(key); + String id = idFor(name, key); if (logable) { logger.log(logLevel, "Cached set id(" + id + ") value to localSource expire " + millis + " ms"); } @@ -844,16 +885,17 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se } } - protected void remoteSetCache(String key, Type type, T value, Duration expire) { - remoteSetCache(key, expire, loadCacheType(type, value), CachedValue.create(value)); + protected void remoteSetCache(String name, String key, Type type, T value, Duration expire) { + remoteSetCache(name, key, expire, loadCacheType(type, value), CachedValue.create(value)); } - protected void remoteSetCache(String key, Duration expire, Type cacheType, CachedValue cacheVal) { + protected void remoteSetCache( + String name, String key, Duration expire, Type cacheType, CachedValue cacheVal) { checkEnable(); boolean logable = logger.isLoggable(logLevel); Objects.requireNonNull(expire); long millis = expire.toMillis(); - String id = idFor(key); + String id = idFor(name, key); if (logable) { logger.log(logLevel, "Cached set id(" + id + ") value to remoteSource expire " + millis + " ms"); } @@ -864,16 +906,17 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se } } - protected CompletableFuture localSetCacheAsync(String key, Type type, T value, Duration expire) { - return localSetCacheAsync(key, expire, loadCacheType(type, value), CachedValue.create(value)); + protected CompletableFuture localSetCacheAsync( + String name, String key, Type type, T value, Duration expire) { + return localSetCacheAsync(name, key, expire, loadCacheType(type, value), CachedValue.create(value)); } protected CompletableFuture localSetCacheAsync( - String key, Duration expire, Type cacheType, CachedValue cacheVal) { + String name, String key, Duration expire, Type cacheType, CachedValue cacheVal) { checkEnable(); boolean logable = logger.isLoggable(logLevel); Objects.requireNonNull(expire); - String id = idFor(key); + String id = idFor(name, key); long millis = expire.toMillis(); if (logable) { logger.log(logLevel, "Cached set id(" + id + ") value to localSource expire " + millis + " ms"); @@ -885,16 +928,17 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se } } - protected CompletableFuture remoteSetCacheAsync(String key, Type type, T value, Duration expire) { - return remoteSetCacheAsync(key, expire, loadCacheType(type, value), CachedValue.create(value)); + protected CompletableFuture remoteSetCacheAsync( + String name, String key, Type type, T value, Duration expire) { + return remoteSetCacheAsync(name, key, expire, loadCacheType(type, value), CachedValue.create(value)); } protected CompletableFuture remoteSetCacheAsync( - String key, Duration expire, Type cacheType, CachedValue cacheVal) { + String name, String key, Duration expire, Type cacheType, CachedValue cacheVal) { checkEnable(); boolean logable = logger.isLoggable(logLevel); Objects.requireNonNull(expire); - String id = idFor(key); + String id = idFor(name, key); long millis = expire.toMillis(); if (logable) { logger.log(logLevel, "Cached set id(" + id + ") value to remoteSource expire " + millis + " ms"); @@ -906,10 +950,11 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se } } - protected CachedValue bothGetCache(final String key, final Duration expire, final Type cacheType) { + protected CachedValue bothGetCache( + final String name, final String key, final Duration expire, final Type cacheType) { checkEnable(); boolean logable = logger.isLoggable(logLevel); - String id = idFor(key); + String id = idFor(name, key); CachedValue cacheVal = localSource.get(id, cacheType); if (CachedValue.isValid(cacheVal)) { if (logable) { @@ -924,7 +969,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se if (logable) { logger.log(logLevel, "Cached set id(" + id + ") value to localSource from remoteSource"); } - localSetCache(key, expire, cacheType, cacheVal); + localSetCache(name, key, expire, cacheType, cacheVal); } if (logable) { logger.log(logLevel, "Cached got id(" + id + ") value from remoteSource"); @@ -940,15 +985,17 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se * 远程异步获取缓存数据, 过期返回null * * @param 泛型 + * @param name 缓存名称 * @param key 缓存键 * @param expire 过期时长,Duration.ZERO为永不过期 * @param cacheType 数据类型 * @return 数据值 */ - protected CompletableFuture> bothGetCacheAsync(String key, Duration expire, Type cacheType) { + protected CompletableFuture> bothGetCacheAsync( + String name, String key, Duration expire, Type cacheType) { checkEnable(); boolean logable = logger.isLoggable(logLevel); - String id = idFor(key); + String id = idFor(name, key); CachedValue val = localSource.get(id, cacheType); // 内存操作,无需异步 if (CachedValue.isValid(val)) { if (logable) { @@ -964,7 +1011,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se if (logable) { logger.log(logLevel, "Cached set id(" + id + ") value to localSource from remoteSource"); } - localSetCache(id, expire, cacheType, v); + localSetCache(name, key, expire, cacheType, v); } if (logable) { logger.log(logLevel, "Cached got id(" + id + ") value from remoteSource"); @@ -986,11 +1033,12 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se /** * 创建一个锁key * + * @param name 缓存名称 * @param key 缓存键 * @return key */ - protected String idFor(String key) { - return schema + ':' + key; + protected String idFor(String name, String key) { + return schema + ':' + name + ':' + key; } /** @@ -1044,17 +1092,18 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se protected static interface GetterFunc { - public R get(String key, Duration expire, Type cacheType); + public R get(String name, String key, Duration expire, Type cacheType); } protected static interface SetterSyncFunc { - public void set(String key, Duration expire, Type cacheType, CachedValue cacheVal); + public void set(String name, String key, Duration expire, Type cacheType, CachedValue cacheVal); } protected static interface SetterAsyncFunc { - public CompletableFuture set(String key, Duration expire, Type cacheType, CachedValue cacheVal); + public CompletableFuture set( + String name, String key, Duration expire, Type cacheType, CachedValue cacheVal); } public class CacheRemoteListener implements CacheEventListener { @@ -1062,7 +1111,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se @Override public void onMessage(String topic, CachedEventMessage message) { if (!Objects.equals(getNode(), message.getNode())) { - localSource.del(idFor(message.getKey())); + localSource.del(idFor(message.getName(), message.getKey())); } } } diff --git a/src/main/java/org/redkale/cached/spi/DynForCached.java b/src/main/java/org/redkale/cached/spi/DynForCached.java index 728ed5bec..e15a2eded 100644 --- a/src/main/java/org/redkale/cached/spi/DynForCached.java +++ b/src/main/java/org/redkale/cached/spi/DynForCached.java @@ -26,6 +26,8 @@ public @interface DynForCached { String manager(); + String name(); + String key(); String localExpire(); diff --git a/src/test/java/org/redkale/test/cached/CachedInstance.java b/src/test/java/org/redkale/test/cached/CachedInstance.java index f19e06f1d..10233b686 100644 --- a/src/test/java/org/redkale/test/cached/CachedInstance.java +++ b/src/test/java/org/redkale/test/cached/CachedInstance.java @@ -27,56 +27,57 @@ public class CachedInstance implements Service { // 修改远程缓存的key值 public void updateName(String code, Map map) { - cachedManager.remoteSetString(code + "_" + map.get("id"), code + "-" + map, Duration.ofMillis(60)); + cachedManager.remoteSetString("name", code + "_" + map.get("id"), code + "-" + map, Duration.ofMillis(60)); } - @Cached(key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) + @Cached(name = "code", key = "#{code}_#{map.id}", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) public String getName(String code, Map map) { return code + "-" + map; } - @Cached(key = "name", localExpire = "30") + @Cached(name = "name", key = "name", localExpire = "30") public String getName() { return "haha"; } - public void updateName(String val) { - cachedManager.bothSet("name_2", String.class, val, Duration.ofSeconds(31), Duration.ofSeconds(60)); + public void updateName2(String val) { + cachedManager.bothSet("name2", "name_2", String.class, val, Duration.ofSeconds(31), Duration.ofSeconds(60)); } - @Cached(key = "name_2", localExpire = "31", remoteExpire = "60") + @Cached(name = "name2", key = "name_2", localExpire = "31", remoteExpire = "60") public String getName2() throws RedkaleException { return "haha"; } - @Cached(key = "dictcode", localExpire = "30", remoteExpire = "60") + @Cached(name = "dictcode", key = "dictcode", localExpire = "30", remoteExpire = "60") public CompletableFuture getDictcodeAsync() { System.out.println("执行了 getDictcodeAsync"); return CompletableFuture.completedFuture("code001"); } - @Cached(key = "name", localExpire = "30") + @Cached(name = "name", key = "name", localExpire = "30") public CompletableFuture getNameAsync() { return CompletableFuture.completedFuture("nameAsync"); } - @Cached(key = "name", localExpire = "30", remoteExpire = "60") + @Cached(name = "name", key = "name", localExpire = "30", remoteExpire = "60") public CompletableFuture getName2Async() throws IOException, InstantiationException { return CompletableFuture.completedFuture("name2Async"); } - @Cached(key = "info_#{id}_file#{files.one}", localExpire = "30", remoteExpire = "60") + @Cached(name = "info", key = "#{id}_#{files.one}", localExpire = "30", remoteExpire = "60") public File getInfo(ParamBean bean, int id, List idList, Map files) { return new File("aa.txt"); } - @Cached(key = "info_#{id}_file#{files.one}", localExpire = "30", remoteExpire = "60") + @Cached(name = "info", key = "#{id}_#{files.one}", localExpire = "30", remoteExpire = "60") public CompletableFuture getInfoAsync(ParamBean bean, int id, List idList, Map files) { return CompletableFuture.completedFuture(new File("aa.txt")); } @Cached( - key = "info_#{id}_file#{files.one}", + name = "info", + key = "#{id}_#{files.one}", localExpire = "30", remoteExpire = "60", timeUnit = TimeUnit.MILLISECONDS) diff --git a/src/test/java/org/redkale/test/cached/CachedInstanceTest.java b/src/test/java/org/redkale/test/cached/CachedInstanceTest.java index c79fb62fe..005fa2ab2 100644 --- a/src/test/java/org/redkale/test/cached/CachedInstanceTest.java +++ b/src/test/java/org/redkale/test/cached/CachedInstanceTest.java @@ -83,12 +83,12 @@ public class CachedInstanceTest { System.out.println("instance1.manager = " + instance.getCachedManager()); System.out.println("instance2.manager = " + instance2.getCachedManager()); manager.updateBroadcastable(false); - instance.updateName("gege"); + instance.updateName2("gege"); Assertions.assertEquals("gege", instance.getName2()); Assertions.assertEquals("haha", instance2.getName2()); manager.updateBroadcastable(true); System.out.println("准备设置 updateName"); - instance.updateName("gege"); + instance.updateName2("gege"); System.out.println("设置结束 updateName"); Utility.sleep(10); Assertions.assertEquals("gege", instance.getName2()); diff --git a/src/test/java/org/redkale/test/cached/CachedManagerTest.java b/src/test/java/org/redkale/test/cached/CachedManagerTest.java index 9d8d7d253..dd52082d8 100644 --- a/src/test/java/org/redkale/test/cached/CachedManagerTest.java +++ b/src/test/java/org/redkale/test/cached/CachedManagerTest.java @@ -37,22 +37,22 @@ public class CachedManagerTest { @Test public void run1() throws Exception { Duration expire = Duration.ofMillis(290); - manager.localSetString("cached-schema:name:haha", "myha", expire); - Assertions.assertEquals(manager.localGetString("cached-schema:name:haha"), "myha"); + manager.localSetString("name", "haha", "myha", expire); + Assertions.assertEquals(manager.localGetString("name", "haha"), "myha"); Utility.sleep(300); - Assertions.assertTrue(manager.localGetString("cached-schema:name:haha") == null); + Assertions.assertTrue(manager.localGetString("name", "haha") == null); CachingBean bean = new CachingBean(); bean.setName("tom"); bean.setRemark("这是名字备注"); String json = bean.toString(); - manager.localSet(bean.getName(), CachingBean.class, bean, expire); + manager.localSet("name", bean.getName(), CachingBean.class, bean, expire); Assertions.assertEquals( - manager.localGet(bean.getName(), CachingBean.class).toString(), json); + manager.localGet("name", bean.getName(), CachingBean.class).toString(), json); bean.setRemark(bean.getRemark() + "-新备注"); Assertions.assertEquals( - manager.localGet(bean.getName(), CachingBean.class).toString(), json); + manager.localGet("name", bean.getName(), CachingBean.class).toString(), json); } @Test @@ -67,7 +67,13 @@ public class CachedManagerTest { for (int i = 0; i < count; i++) { new Thread(() -> { manager.bothGetSet( - "name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); + "name", + "name", + String.class, + false, + localExpire, + remoteExpire, + () -> bean.getName()); cdl.countDown(); }) .start(); @@ -76,7 +82,7 @@ public class CachedManagerTest { } Assertions.assertEquals(1, ParallelBean.c1.get()); Utility.sleep(200); - manager.bothGetSet("name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); + manager.bothGetSet("name", "name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); Assertions.assertEquals(1, ParallelBean.c1.get()); Utility.sleep(300); { @@ -84,7 +90,13 @@ public class CachedManagerTest { for (int i = 0; i < count; i++) { new Thread(() -> { manager.bothGetSet( - "name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); + "name", + "name", + String.class, + false, + localExpire, + remoteExpire, + () -> bean.getName()); cdl.countDown(); }) .start(); diff --git a/src/test/java/org/redkale/test/cached/_DynLocalCacheInstance.java b/src/test/java/org/redkale/test/cached/_DynLocalCacheInstance.java index a37ee6257..676aba99f 100644 --- a/src/test/java/org/redkale/test/cached/_DynLocalCacheInstance.java +++ b/src/test/java/org/redkale/test/cached/_DynLocalCacheInstance.java @@ -46,6 +46,7 @@ public class _DynLocalCacheInstance extends CachedInstance { @DynForCached( dynField = "_redkale_getNameCachedAction1", manager = "", + name = "name", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS, @@ -63,7 +64,8 @@ public class _DynLocalCacheInstance extends CachedInstance { @DynForCached( dynField = "_redkale_getInfoCachedAction2", manager = "", - key = "info_#{id}_file#{files.one}", + name = "info", + key = "#{id}_#{files.one}", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", @@ -81,6 +83,7 @@ public class _DynLocalCacheInstance extends CachedInstance { @DynForCached( dynField = "_redkale_getNameAsyncCachedAction3", manager = "", + name = "name", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS, @@ -98,7 +101,8 @@ public class _DynLocalCacheInstance extends CachedInstance { @DynForCached( dynField = "_redkale_getInfo2AsyncCachedAction4", manager = "", - key = "info_#{id}_file#{files.one}", + name = "info", + key = "#{id}_#{files.one}", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", @@ -120,6 +124,7 @@ public class _DynLocalCacheInstance extends CachedInstance { @DynForCached( dynField = "_redkale_getName2AsyncCachedAction5", manager = "", + name = "name", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS, @@ -137,7 +142,8 @@ public class _DynLocalCacheInstance extends CachedInstance { @DynForCached( dynField = "_redkale_getInfoAsyncCachedAction6", manager = "", - key = "info_#{id}_file#{files.one}", + name = "info", + key = "#{id}_#{files.one}", nullable = false, timeUnit = TimeUnit.SECONDS, remoteExpire = "60", @@ -156,6 +162,7 @@ public class _DynLocalCacheInstance extends CachedInstance { @DynForCached( dynField = "_redkale_getName2CachedAction7", manager = "", + name = "name", key = "name", nullable = false, timeUnit = TimeUnit.SECONDS,