增加Cached.name属性

This commit is contained in:
redkale
2024-09-11 11:17:39 +08:00
parent 61586beb45
commit 2b18310050
14 changed files with 349 additions and 195 deletions

View File

@@ -9,6 +9,7 @@
## 属性说明 ## 属性说明
|属性|默认值|说明| |属性|默认值|说明|
| --- | --- | --- | | --- | --- | --- |
|name|未定义|缓存的名称|
|key|未定义|缓存的key支持参数动态组合比如"key_#{id}"| |key|未定义|缓存的key支持参数动态组合比如"key_#{id}"|
|manager|空|缓存管理器名称, 不能含有':'、'#'、'@'字符| |manager|空|缓存管理器名称, 不能含有':'、'#'、'@'字符|
|localExpire|-1|本地缓存过期时长, 0表示永不过期 -1表示不作本地缓存。 <br> 参数值支持方式:<br> &emsp;100: 设置数值 <br> &emsp;${env.cache.expires}: 读取系统配置项 | |localExpire|-1|本地缓存过期时长, 0表示永不过期 -1表示不作本地缓存。 <br> 参数值支持方式:<br> &emsp;100: 设置数值 <br> &emsp;${env.cache.expires}: 读取系统配置项 |
@@ -21,7 +22,7 @@
## 基本用法 ## 基本用法
&emsp;&emsp;将结果进行本地缓存30秒且远程缓存60秒 &emsp;&emsp;将结果进行本地缓存30秒且远程缓存60秒
```java ```java
@Cached(key = "name", localExpire = "30", remoteExpire = "60") @Cached(name = "name", key = "name", localExpire = "30", remoteExpire = "60")
public String getName() { public String getName() {
return "haha"; return "haha";
} }
@@ -29,7 +30,7 @@
&emsp;&emsp;以参数code为key将结果进行本地缓存(时长由环境变量```env.cache.expire```配置没配置采用默认值30秒) &emsp;&emsp;以参数code为key将结果进行本地缓存(时长由环境变量```env.cache.expire```配置没配置采用默认值30秒)
```java ```java
@Cached(key = "#{code}", localExpire = "${env.cache.expire:30}") @Cached(name = "name", key = "#{code}", localExpire = "${env.cache.expire:30}")
public CompletableFuture<String> getNameAsync(String code) { public CompletableFuture<String> getNameAsync(String code) {
return redis.getStringAsync(code); return redis.getStringAsync(code);
} }
@@ -42,10 +43,10 @@
//实时修改远程缓存的key值 //实时修改远程缓存的key值
public void updateName(String code, Map<String, Long> map) { public void updateName(String code, Map<String, Long> 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<String, Long> map) { public String getName(String code, Map<String, Long> map) {
return code + "-" + map; return code + "-" + map;
} }
@@ -58,14 +59,14 @@
@Resource @Resource
private CachedManager cachedManager; 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<String, Long> map) { public String getName(String code, Map<String, Long> map) {
return code + "-" + map; return code + "-" + map;
} }
public void updateExpire() { public void updateExpire() {
Duration expire = Duration.ofMillis(600); Duration expire = Duration.ofMillis(600);
cachedManager.acceptCachedAction("#{code}_#{map.id}", action -> { cachedManager.acceptCachedAction("name", action -> {
//将缓存时长改成600毫秒并开启本地缓存 //将缓存时长改成600毫秒并开启本地缓存
action.setLocalExpire(expire); action.setLocalExpire(expire);
action.setRemoteExpire(expire); action.setRemoteExpire(expire);
@@ -104,17 +105,17 @@
//第一个缓存器实时修改远程缓存的key值 //第一个缓存器实时修改远程缓存的key值
public void updateName(String code, Map<String, Long> map) { public void updateName(String code, Map<String, Long> 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<String, Long> map) { public String getName(String code, Map<String, Long> map) {
return code + "-" + 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<String, Long> map) { public String getName2(String code, Map<String, Long> map) {
return code + "-" + map; return code + "-" + map;
} }

View File

@@ -17,6 +17,8 @@ import org.redkale.service.LoadMode;
* 2、方法返回类型必须可json序列化 <br> * 2、方法返回类型必须可json序列化 <br>
* 3、方法必须是protected/public <br> * 3、方法必须是protected/public <br>
* 4、方法不能是final/static <br> * 4、方法不能是final/static <br>
*<br>
* 远程缓存里中存放的key值为: {CachedManager.schema}:{Cached.name}:{Cached.key}
* *
* @since 2.8.0 * @since 2.8.0
*/ */
@@ -25,11 +27,18 @@ import org.redkale.service.LoadMode;
@Retention(RUNTIME) @Retention(RUNTIME)
public @interface Cached { public @interface Cached {
/**
* 缓存的name <br>
*
* @return name
*/
String name();
/** /**
* 缓存的key支持参数动态组合比如"key_#{id}" <br> * 缓存的key支持参数动态组合比如"key_#{id}" <br>
* <b>'@'开头的key值视为CacheKeyGenerator对象名称</b> <br> * <b>'@'开头的key值视为CacheKeyGenerator对象名称</b> <br>
* *
* @see org.redkale.cached.spi.CachedKeyGenerator#name() * @see org.redkale.cached.spi.CachedKeyGenerator#key()
* *
* @return 键 * @return 键
*/ */

View File

@@ -91,39 +91,42 @@ public interface CachedManager extends Resourcable {
public List<CachedAction> getCachedActions(); public List<CachedAction> getCachedActions();
/** /**
* 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction}<br> * 处理指定缓存名称的{@link org.redkale.cached.spi.CachedAction}<br>
* 可用于动态调整缓存时长 * 可用于动态调整缓存时长
* *
* @param templetKey 模板key * @param name 缓存名称
* @param consumer 处理函数 * @param consumer 处理函数
*/ */
public void acceptCachedAction(String templetKey, Consumer<CachedAction> consumer); public void acceptCachedAction(String name, Consumer<CachedAction> consumer);
// -------------------------------------- 本地缓存 -------------------------------------- // -------------------------------------- 本地缓存 --------------------------------------
/** /**
* 本地获取缓存数据, 过期返回null * 本地获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
public <T> T localGet(final String key, final Type type); public <T> T localGet(String name, String key, Type type);
/** /**
* 本地获取字符串缓存数据, 过期返回null * 本地获取字符串缓存数据, 过期返回null
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 数据值 * @return 数据值
*/ */
default String localGetString(final String key) { default String localGetString(String name, String key) {
return localGet(key, String.class); return localGet(name, key, String.class);
} }
/** /**
* 本地获取缓存数据, 过期返回null * 本地获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -131,12 +134,14 @@ public interface CachedManager extends Resourcable {
* @param supplier 数据函数 * @param supplier 数据函数
* @return 数据值 * @return 数据值
*/ */
public <T> T localGetSet(String key, Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier); public <T> T localGetSet(
String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier);
/** /**
* 本地异步获取缓存数据, 过期返回null * 本地异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -145,83 +150,96 @@ public interface CachedManager extends Resourcable {
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> localGetSetAsync( public <T> CompletableFuture<T> localGetSetAsync(
String key, Type type, boolean nullable, Duration expire, ThrowSupplier<CompletableFuture<T>> supplier); String name,
String key,
Type type,
boolean nullable,
Duration expire,
ThrowSupplier<CompletableFuture<T>> supplier);
/** /**
* 本地缓存数据 * 本地缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
*/ */
public <T> void localSet(String key, Type type, T value, Duration expire); public <T> void localSet(String name, String key, Type type, T value, Duration expire);
/** /**
* 本地缓存字符串数据 * 本地缓存字符串数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
*/ */
default void localSetString(final String key, final String value, Duration expire) { default void localSetString(String name, String key, String value, Duration expire) {
localSet(key, String.class, value, expire); localSet(name, key, String.class, value, expire);
} }
/** /**
* 本地删除缓存数据 * 本地删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
public long localDel(String key); public long localDel(String name, String key);
// -------------------------------------- 远程缓存 -------------------------------------- // -------------------------------------- 远程缓存 --------------------------------------
/** /**
* 远程获取缓存数据, 过期返回null * 远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
public <T> T remoteGet(final String key, final Type type); public <T> T remoteGet(String name, String key, final Type type);
/** /**
* 远程获取字符串缓存数据, 过期返回null * 远程获取字符串缓存数据, 过期返回null
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 数据值 * @return 数据值
*/ */
default String remoteGetString(final String key) { default String remoteGetString(String name, String key) {
return remoteGet(key, String.class); return remoteGet(name, key, String.class);
} }
/** /**
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> remoteGetAsync(final String key, final Type type); public <T> CompletableFuture<T> remoteGetAsync(String name, String key, final Type type);
/** /**
* 远程异步获取字符串缓存数据, 过期返回null * 远程异步获取字符串缓存数据, 过期返回null
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 数据值 * @return 数据值
*/ */
default CompletableFuture<String> remoteGetStringAsync(final String key) { default CompletableFuture<String> remoteGetStringAsync(String name, String key) {
return remoteGetAsync(key, String.class); return remoteGetAsync(name, key, String.class);
} }
/** /**
* 远程获取缓存数据, 过期返回null * 远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -229,12 +247,14 @@ public interface CachedManager extends Resourcable {
* @param supplier 数据函数 * @param supplier 数据函数
* @return 数据值 * @return 数据值
*/ */
public <T> T remoteGetSet(String key, Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier); public <T> T remoteGetSet(
String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier);
/** /**
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -243,115 +263,131 @@ public interface CachedManager extends Resourcable {
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> remoteGetSetAsync( public <T> CompletableFuture<T> remoteGetSetAsync(
String key, Type type, boolean nullable, Duration expire, ThrowSupplier<CompletableFuture<T>> supplier); String name,
String key,
Type type,
boolean nullable,
Duration expire,
ThrowSupplier<CompletableFuture<T>> supplier);
/** /**
* 远程缓存数据 * 远程缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
*/ */
public <T> void remoteSet(final String key, final Type type, final T value, Duration expire); public <T> void remoteSet(String name, String key, Type type, T value, Duration expire);
/** /**
* 远程缓存字符串数据 * 远程缓存字符串数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
*/ */
default void remoteSetString(final String key, final String value, Duration expire) { default void remoteSetString(String name, String key, String value, Duration expire) {
remoteSet(key, String.class, value, expire); remoteSet(name, key, String.class, value, expire);
} }
/** /**
* 远程异步缓存数据 * 远程异步缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
* @return void * @return void
*/ */
public <T> CompletableFuture<Void> remoteSetAsync(String key, Type type, T value, Duration expire); public <T> CompletableFuture<Void> remoteSetAsync(String name, String key, Type type, T value, Duration expire);
/** /**
* 远程异步缓存字符串数据 * 远程异步缓存字符串数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
* @return void * @return void
*/ */
default CompletableFuture<Void> remoteSetStringAsync(final String key, final String value, Duration expire) { default CompletableFuture<Void> remoteSetStringAsync(String name, String key, String value, Duration expire) {
return remoteSetAsync(key, String.class, value, expire); return remoteSetAsync(name, key, String.class, value, expire);
} }
/** /**
* 远程删除缓存数据 * 远程删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
public long remoteDel(String key); public long remoteDel(String name, String key);
/** /**
* 远程异步删除缓存数据 * 远程异步删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
public CompletableFuture<Long> remoteDelAsync(String key); public CompletableFuture<Long> remoteDelAsync(String name, String key);
// -------------------------------------- both缓存 -------------------------------------- // -------------------------------------- both缓存 --------------------------------------
/** /**
* 本地或远程获取缓存数据, 过期返回null * 本地或远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
public <T> T bothGet(final String key, final Type type); public <T> T bothGet(String name, String key, Type type);
/** /**
* 本地或远程获取字符串缓存数据, 过期返回null * 本地或远程获取字符串缓存数据, 过期返回null
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 数据值 * @return 数据值
*/ */
default String bothGetString(final String key) { default String bothGetString(String name, String key) {
return bothGet(key, String.class); return bothGet(name, key, String.class);
} }
/** /**
* 本地或远程异步获取缓存数据, 过期返回null * 本地或远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> bothGetAsync(final String key, final Type type); public <T> CompletableFuture<T> bothGetAsync(String name, String key, Type type);
/** /**
* 本地或远程异步获取字符串缓存数据, 过期返回null * 本地或远程异步获取字符串缓存数据, 过期返回null
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 数据值 * @return 数据值
*/ */
default CompletableFuture<String> bothGetStringAsync(final String key) { default CompletableFuture<String> bothGetStringAsync(String name, String key) {
return bothGetAsync(key, String.class); return bothGetAsync(name, key, String.class);
} }
/** /**
* 本地或远程获取缓存数据, 过期返回null * 本地或远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -361,6 +397,7 @@ public interface CachedManager extends Resourcable {
* @return 数据值 * @return 数据值
*/ */
public <T> T bothGetSet( public <T> T bothGetSet(
String name,
String key, String key,
Type type, Type type,
boolean nullable, boolean nullable,
@@ -372,6 +409,7 @@ public interface CachedManager extends Resourcable {
* 本地或远程异步获取缓存数据, 过期返回null * 本地或远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -381,6 +419,7 @@ public interface CachedManager extends Resourcable {
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> bothGetSetAsync( public <T> CompletableFuture<T> bothGetSetAsync(
String name,
String key, String key,
Type type, Type type,
boolean nullable, boolean nullable,
@@ -392,30 +431,33 @@ public interface CachedManager extends Resourcable {
* 本地和远程缓存数据 * 本地和远程缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
* @param localExpire 本地过期时长Duration.ZERO为永不过期为null表示不本地缓存 * @param localExpire 本地过期时长Duration.ZERO为永不过期为null表示不本地缓存
* @param remoteExpire 远程过期时长Duration.ZERO为永不过期为null表示不远程缓存 * @param remoteExpire 远程过期时长Duration.ZERO为永不过期为null表示不远程缓存
*/ */
public <T> void bothSet(String key, Type type, T value, Duration localExpire, Duration remoteExpire); public <T> void bothSet(String name, String key, Type type, T value, Duration localExpire, Duration remoteExpire);
/** /**
* 本地和远程缓存字符串数据 * 本地和远程缓存字符串数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param value 数据值 * @param value 数据值
* @param localExpire 本地过期时长Duration.ZERO为永不过期为null表示不本地缓存 * @param localExpire 本地过期时长Duration.ZERO为永不过期为null表示不本地缓存
* @param remoteExpire 远程过期时长Duration.ZERO为永不过期为null表示不远程缓存 * @param remoteExpire 远程过期时长Duration.ZERO为永不过期为null表示不远程缓存
*/ */
default void bothSetString(final String key, final String value, Duration localExpire, Duration remoteExpire) { default void bothSetString(String name, String key, String value, Duration localExpire, Duration remoteExpire) {
bothSet(key, String.class, value, localExpire, remoteExpire); bothSet(name, key, String.class, value, localExpire, remoteExpire);
} }
/** /**
* 本地和远程异步缓存数据 * 本地和远程异步缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
@@ -424,11 +466,12 @@ public interface CachedManager extends Resourcable {
* @return void * @return void
*/ */
public <T> CompletableFuture<Void> bothSetAsync( public <T> CompletableFuture<Void> 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 key 缓存键
* @param value 数据值 * @param value 数据值
* @param localExpire 本地过期时长Duration.ZERO为永不过期为null表示不本地缓存 * @param localExpire 本地过期时长Duration.ZERO为永不过期为null表示不本地缓存
@@ -436,23 +479,25 @@ public interface CachedManager extends Resourcable {
* @return void * @return void
*/ */
default CompletableFuture<Void> bothSetStringAsync( default CompletableFuture<Void> bothSetStringAsync(
String key, String value, Duration localExpire, Duration remoteExpire) { String name, String key, String value, Duration localExpire, Duration remoteExpire) {
return bothSetAsync(key, String.class, value, localExpire, remoteExpire); return bothSetAsync(name, key, String.class, value, localExpire, remoteExpire);
} }
/** /**
* 本地和远程删除缓存数据 * 本地和远程删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
public long bothDel(String key); public long bothDel(String name, String key);
/** /**
* 本地和远程异步删除缓存数据 * 本地和远程异步删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
public CompletableFuture<Long> bothDelAsync(String key); public CompletableFuture<Long> bothDelAsync(String name, String key);
} }

View File

@@ -62,8 +62,11 @@ public class CachedAction {
@Nullable @Nullable
private final String[] paramNames; private final String[] paramNames;
// 缓存名称
final String name;
// 模板key // 模板key
final String templetKey; final String key;
// 缓存key生成器 // 缓存key生成器
private CachedKeyGenerator keyGenerator; private CachedKeyGenerator keyGenerator;
@@ -85,7 +88,8 @@ public class CachedAction {
this.paramNames = paramNames; this.paramNames = paramNames;
this.methodName = method.getName(); this.methodName = method.getName();
this.fieldName = Objects.requireNonNull(fieldName); this.fieldName = Objects.requireNonNull(fieldName);
this.templetKey = cached.getTempletKey(); this.name = cached.getName();
this.key = cached.getKey();
Type returnType = method.getGenericReturnType(); Type returnType = method.getGenericReturnType();
this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType)); this.async = CompletableFuture.class.isAssignableFrom(TypeToken.typeToClass(returnType));
this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType; this.resultType = this.async ? ((ParameterizedType) returnType).getActualTypeArguments()[0] : returnType;
@@ -93,24 +97,25 @@ public class CachedAction {
String init(ResourceFactory resourceFactory, Object service) { String init(ResourceFactory resourceFactory, Object service) {
this.manager = resourceFactory.load(cached.getManager(), CachedManager.class); this.manager = resourceFactory.load(cached.getManager(), CachedManager.class);
final String key = environment.getPropertyValue(cached.getTempletKey()); final String realKey = environment.getPropertyValue(cached.getKey());
if (key.startsWith("@")) { // 动态加载缓存key生成器 if (realKey.startsWith("@")) { // 动态加载缓存key生成器
String generatorName = key.substring(1); String generatorName = realKey.substring(1);
this.keyGenerator = resourceFactory.findChild(generatorName, CachedKeyGenerator.class); this.keyGenerator = resourceFactory.findChild(generatorName, CachedKeyGenerator.class);
} else { } else {
MultiHashKey dynKey = MultiHashKey.create(paramNames, key); MultiHashKey dynKey = MultiHashKey.create(paramNames, realKey);
this.keyGenerator = CachedKeyGenerator.create(dynKey); this.keyGenerator = CachedKeyGenerator.create(dynKey);
} }
this.localExpire = createDuration(cached.getLocalExpire()); this.localExpire = createDuration(cached.getLocalExpire());
this.remoteExpire = createDuration(cached.getRemoteExpire()); this.remoteExpire = createDuration(cached.getRemoteExpire());
((CachedActionFunc) this.manager).addAction(this); ((CachedActionFunc) this.manager).addAction(this);
return key; return realKey;
} }
@ClassDepends @ClassDepends
public <T> T get(ThrowSupplier<T> supplier, Object... args) { public <T> T get(ThrowSupplier<T> supplier, Object... args) {
if (async) { if (async) {
return (T) manager.bothGetSetAsync( return (T) manager.bothGetSetAsync(
name,
keyGenerator.generate(service, this, args), keyGenerator.generate(service, this, args),
resultType, resultType,
nullable, nullable,
@@ -119,6 +124,7 @@ public class CachedAction {
(ThrowSupplier) supplier); (ThrowSupplier) supplier);
} else { } else {
return manager.bothGetSet( return manager.bothGetSet(
name,
keyGenerator.generate(service, this, args), keyGenerator.generate(service, this, args),
resultType, resultType,
nullable, nullable,
@@ -161,8 +167,12 @@ public class CachedAction {
return method; return method;
} }
public String getTempletKey() { public String getName() {
return templetKey; return name;
}
public String getKey() {
return key;
} }
public Duration getLocalExpire() { public Duration getLocalExpire() {
@@ -189,7 +199,7 @@ public class CachedAction {
+ ",\"fieldName\":\"" + fieldName + "\"" + ",\"fieldName\":\"" + fieldName + "\""
+ ",\"paramTypes\":" + JsonConvert.root().convertTo(method.getParameterTypes()) + ",\"paramTypes\":" + JsonConvert.root().convertTo(method.getParameterTypes())
+ ",\"paramNames\":" + JsonConvert.root().convertTo(paramNames) + ",\"paramNames\":" + JsonConvert.root().convertTo(paramNames)
+ ",\"templetKey\":\"" + templetKey + "\"" + ",\"templetKey\":\"" + key + "\""
+ ",\"resultType\":\"" + resultType + "\"" + ",\"resultType\":\"" + resultType + "\""
+ ",\"cache\":" + cached + ",\"cache\":" + cached
+ "}"; + "}";

View File

@@ -20,7 +20,8 @@ public class CachedEntry {
private String manager; private String manager;
private String templetKey; private String name;
private String key;
private String localExpire; private String localExpire;
@@ -34,7 +35,8 @@ public class CachedEntry {
public CachedEntry(DynForCached cached) { public CachedEntry(DynForCached cached) {
this.manager = cached.manager(); this.manager = cached.manager();
this.templetKey = cached.key(); this.name = cached.name();
this.key = cached.key();
this.localExpire = cached.localExpire(); this.localExpire = cached.localExpire();
this.remoteExpire = cached.remoteExpire(); this.remoteExpire = cached.remoteExpire();
this.timeUnit = cached.timeUnit(); this.timeUnit = cached.timeUnit();
@@ -43,7 +45,8 @@ public class CachedEntry {
public CachedEntry(Cached cached) { public CachedEntry(Cached cached) {
this.manager = cached.manager(); this.manager = cached.manager();
this.templetKey = cached.key(); this.name = cached.name();
this.key = cached.key();
this.localExpire = cached.localExpire(); this.localExpire = cached.localExpire();
this.remoteExpire = cached.remoteExpire(); this.remoteExpire = cached.remoteExpire();
this.timeUnit = cached.timeUnit(); this.timeUnit = cached.timeUnit();
@@ -54,8 +57,12 @@ public class CachedEntry {
return manager; return manager;
} }
public String getTempletKey() { public String getName() {
return templetKey; return name;
}
public String getKey() {
return key;
} }
public String getLocalExpire() { public String getLocalExpire() {

View File

@@ -21,6 +21,8 @@ public class CachedEventMessage implements Serializable {
// CachedManager唯一标识 // CachedManager唯一标识
protected String node; protected String node;
// name
protected String name;
// key // key
protected String key; protected String key;
@@ -29,8 +31,9 @@ public class CachedEventMessage implements Serializable {
public CachedEventMessage() {} public CachedEventMessage() {}
public CachedEventMessage(String node, String key) { public CachedEventMessage(String node, String name, String key) {
this.node = node; this.node = node;
this.name = name;
this.key = key; this.key = key;
this.time = System.currentTimeMillis(); this.time = System.currentTimeMillis();
} }
@@ -43,6 +46,14 @@ public class CachedEventMessage implements Serializable {
this.node = node; this.node = node;
} }
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() { public String getKey() {
return key; return key;
} }

View File

@@ -34,9 +34,9 @@ public interface CachedKeyGenerator {
* *
* @see org.redkale.cached.Cached#key() * @see org.redkale.cached.Cached#key()
* *
* @return name * @return key
*/ */
public String name(); public String key();
/** /**
* 根据MultiHashKey生成一个CachedKeyGenerator * 根据MultiHashKey生成一个CachedKeyGenerator
@@ -52,7 +52,7 @@ public interface CachedKeyGenerator {
} }
@Override @Override
public String name() { public String key() {
return ""; return "";
} }
}; };

View File

@@ -51,7 +51,7 @@ class CachedKeyGeneratorLoader implements ResourceTypeLoader {
generator = generatorMap.computeIfAbsent(resourceName, n -> { generator = generatorMap.computeIfAbsent(resourceName, n -> {
for (CachedKeyGenerator instance : ServiceLoader.load( for (CachedKeyGenerator instance : ServiceLoader.load(
CachedKeyGenerator.class, engine.getApplication().getClassLoader())) { CachedKeyGenerator.class, engine.getApplication().getClassLoader())) {
if (Objects.equals(n, instance.name())) { if (Objects.equals(n, instance.key())) {
rf.inject(instance); rf.inject(instance);
if (instance instanceof Service) { if (instance instanceof Service) {
((Service) instance).init(null); ((Service) instance).init(null);

View File

@@ -241,14 +241,12 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
/** /**
* 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction} * 处理指定缓存key的{@link org.redkale.cached.spi.CachedAction}
* *
* @param templetKey 缓存key * @param name 缓存名称
* @param consumer 处理函数 * @param consumer 处理函数
*/ */
@Override @Override
public void acceptCachedAction(String templetKey, Consumer<CachedAction> consumer) { public void acceptCachedAction(String name, Consumer<CachedAction> consumer) {
actions.stream() actions.stream().filter(v -> Objects.equals(v.getName(), name)).forEach(consumer);
.filter(v -> Objects.equals(v.getTempletKey(), templetKey))
.forEach(consumer);
} }
@Override @Override
@@ -261,20 +259,22 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 本地获取缓存数据, 过期返回null * 本地获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> T localGet(final String key, final Type type) { public <T> T localGet(String name, String key, Type type) {
checkEnable(); checkEnable();
return CachedValue.get(localSource.get(idFor(key), loadCacheType(type))); return CachedValue.get(localSource.get(idFor(name, key), loadCacheType(type)));
} }
/** /**
* 本地获取缓存数据, 过期返回null * 本地获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -284,10 +284,11 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/ */
@Override @Override
public <T> T localGetSet( public <T> T localGetSet(
final String key, final Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier) { String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier) {
return getSet( return getSet(
(k, ex, ct) -> localSource.get(idFor(k), ct), (n, k, ex, ct) -> localSource.get(idFor(n, k), ct),
this::localSetCache, this::localSetCache,
name,
key, key,
type, type,
nullable, nullable,
@@ -299,6 +300,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 本地异步获取缓存数据, 过期返回null * 本地异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -308,10 +310,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/ */
@Override @Override
public <T> CompletableFuture<T> localGetSetAsync( public <T> CompletableFuture<T> localGetSetAsync(
String key, Type type, boolean nullable, Duration expire, ThrowSupplier<CompletableFuture<T>> supplier) { String name,
String key,
Type type,
boolean nullable,
Duration expire,
ThrowSupplier<CompletableFuture<T>> supplier) {
return getSetAsync( return getSetAsync(
(id, ex, ct) -> localSource.getAsync(id, ct), (n, k, e, c) -> localSource.getAsync(idFor(n, k), c),
this::localSetCacheAsync, this::localSetCacheAsync,
name,
key, key,
type, type,
nullable, nullable,
@@ -323,26 +331,28 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 本地缓存数据 * 本地缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
*/ */
@Override @Override
public <T> void localSet(String key, Type type, T value, Duration expire) { public <T> void localSet(String name, String key, Type type, T value, Duration expire) {
localSetCache(key, type, value, expire); localSetCache(name, key, type, value, expire);
} }
/** /**
* 本地删除缓存数据 * 本地删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
@Override @Override
public long localDel(String key) { public long localDel(String name, String key) {
checkEnable(); checkEnable();
return localSource.del(idFor(key)); return localSource.del(idFor(name, key));
} }
// -------------------------------------- 远程缓存 -------------------------------------- // -------------------------------------- 远程缓存 --------------------------------------
@@ -350,28 +360,30 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程获取缓存数据, 过期返回null * 远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> T remoteGet(final String key, final Type type) { public <T> T remoteGet(String name, String key, Type type) {
checkEnable(); checkEnable();
return CachedValue.get(remoteSource.get(idFor(key), loadCacheType(type))); return CachedValue.get(remoteSource.get(idFor(name, key), loadCacheType(type)));
} }
/** /**
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> CompletableFuture<T> remoteGetAsync(final String key, final Type type) { public <T> CompletableFuture<T> remoteGetAsync(String name, String key, Type type) {
checkEnable(); checkEnable();
CompletableFuture<CachedValue<T>> future = remoteSource.getAsync(idFor(key), loadCacheType(type)); CompletableFuture<CachedValue<T>> future = remoteSource.getAsync(idFor(name, key), loadCacheType(type));
return future.thenApply(CachedValue::get); return future.thenApply(CachedValue::get);
} }
@@ -379,6 +391,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程获取缓存数据, 过期返回null * 远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -388,10 +401,11 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/ */
@Override @Override
public <T> T remoteGetSet( public <T> T remoteGetSet(
final String key, final Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier) { String name, String key, Type type, boolean nullable, Duration expire, ThrowSupplier<T> supplier) {
return getSet( return getSet(
(k, ex, ct) -> remoteSource.get(idFor(k), ct), (n, k, ex, ct) -> remoteSource.get(idFor(n, k), ct),
this::remoteSetCache, this::remoteSetCache,
name,
key, key,
type, type,
nullable, nullable,
@@ -403,6 +417,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -412,10 +427,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/ */
@Override @Override
public <T> CompletableFuture<T> remoteGetSetAsync( public <T> CompletableFuture<T> remoteGetSetAsync(
String key, Type type, boolean nullable, Duration expire, ThrowSupplier<CompletableFuture<T>> supplier) { String name,
String key,
Type type,
boolean nullable,
Duration expire,
ThrowSupplier<CompletableFuture<T>> supplier) {
return getSetAsync( return getSetAsync(
(k, ex, ct) -> remoteSource.getAsync(idFor(k), ct), (n, k, ex, ct) -> remoteSource.getAsync(idFor(n, k), ct),
this::remoteSetCacheAsync, this::remoteSetCacheAsync,
name,
key, key,
type, type,
nullable, nullable,
@@ -427,52 +448,56 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程缓存数据 * 远程缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
*/ */
@Override @Override
public <T> void remoteSet(final String key, final Type type, final T value, Duration expire) { public <T> void remoteSet(String name, String key, Type type, T value, Duration expire) {
remoteSetCache(key, type, value, expire); remoteSetCache(name, key, type, value, expire);
} }
/** /**
* 远程异步缓存数据 * 远程异步缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
*/ */
@Override @Override
public <T> CompletableFuture<Void> remoteSetAsync(String key, Type type, T value, Duration expire) { public <T> CompletableFuture<Void> remoteSetAsync(String name, String key, Type type, T value, Duration expire) {
return remoteSetCacheAsync(key, type, value, expire); return remoteSetCacheAsync(name, key, type, value, expire);
} }
/** /**
* 远程删除缓存数据 * 远程删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
@Override @Override
public long remoteDel(String key) { public long remoteDel(String name, String key) {
checkEnable(); checkEnable();
return remoteSource.del(idFor(key)); return remoteSource.del(idFor(name, key));
} }
/** /**
* 远程异步删除缓存数据 * 远程异步删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
@Override @Override
public CompletableFuture<Long> remoteDelAsync(String key) { public CompletableFuture<Long> remoteDelAsync(String name, String key) {
checkEnable(); checkEnable();
return remoteSource.delAsync(idFor(key)); return remoteSource.delAsync(idFor(name, key));
} }
// -------------------------------------- both缓存 -------------------------------------- // -------------------------------------- both缓存 --------------------------------------
@@ -480,32 +505,35 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程获取缓存数据, 过期返回null * 远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> T bothGet(final String key, final Type type) { public <T> T bothGet(String name, String key, Type type) {
return CachedValue.get(bothGetCache(key, (Duration) null, type)); return CachedValue.get(bothGetCache(name, key, (Duration) null, type));
} }
/** /**
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> CompletableFuture<T> bothGetAsync(final String key, final Type type) { public <T> CompletableFuture<T> bothGetAsync(String name, String key, Type type) {
return bothGetCacheAsync(key, (Duration) null, type).thenApply(CachedValue::get); return bothGetCacheAsync(name, key, (Duration) null, type).thenApply(CachedValue::get);
} }
/** /**
* 远程获取缓存数据, 过期返回null * 远程获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -516,8 +544,9 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/ */
@Override @Override
public <T> T bothGetSet( public <T> T bothGetSet(
final String key, String name,
final Type type, String key,
Type type,
boolean nullable, boolean nullable,
Duration localExpire, Duration localExpire,
Duration remoteExpire, Duration remoteExpire,
@@ -533,20 +562,21 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
} }
if (remoteExpire == null) { // 只有本地缓存 if (remoteExpire == null) { // 只有本地缓存
Objects.requireNonNull(localExpire); Objects.requireNonNull(localExpire);
return localGetSet(key, type, nullable, localExpire, supplier); return localGetSet(name, key, type, nullable, localExpire, supplier);
} }
if (localExpire == null) { // 只有远程缓存 if (localExpire == null) { // 只有远程缓存
Objects.requireNonNull(remoteExpire); Objects.requireNonNull(remoteExpire);
return remoteGetSet(key, type, nullable, remoteExpire, supplier); return remoteGetSet(name, key, type, nullable, remoteExpire, supplier);
} }
return getSet( return getSet(
this::bothGetCache, this::bothGetCache,
(k, e, t, v) -> { (n, k, e, t, v) -> {
localSetCache(k, localExpire, t, v); localSetCache(n, k, localExpire, t, v);
if (remoteSource != null) { if (remoteSource != null) {
remoteSetCache(k, remoteExpire, t, v); remoteSetCache(n, k, remoteExpire, t, v);
} }
}, },
name,
key, key,
type, type,
nullable, nullable,
@@ -558,6 +588,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -568,6 +599,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/ */
@Override @Override
public <T> CompletableFuture<T> bothGetSetAsync( public <T> CompletableFuture<T> bothGetSetAsync(
String name,
String key, String key,
Type type, Type type,
boolean nullable, boolean nullable,
@@ -583,22 +615,23 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
} }
if (remoteExpire == null) { // 只有本地缓存 if (remoteExpire == null) { // 只有本地缓存
Objects.requireNonNull(localExpire); Objects.requireNonNull(localExpire);
return localGetSetAsync(key, type, nullable, localExpire, supplier); return localGetSetAsync(name, key, type, nullable, localExpire, supplier);
} }
if (localExpire == null) { // 只有远程缓存 if (localExpire == null) { // 只有远程缓存
Objects.requireNonNull(remoteExpire); Objects.requireNonNull(remoteExpire);
return remoteGetSetAsync(key, type, nullable, remoteExpire, supplier); return remoteGetSetAsync(name, key, type, nullable, remoteExpire, supplier);
} }
return getSetAsync( return getSetAsync(
this::bothGetCacheAsync, this::bothGetCacheAsync,
(k, e, t, v) -> { (n, k, e, t, v) -> {
localSetCache(k, localExpire, t, v); localSetCache(n, k, localExpire, t, v);
if (remoteSource != null) { if (remoteSource != null) {
return remoteSetCacheAsync(k, remoteExpire, t, v); return remoteSetCacheAsync(n, k, remoteExpire, t, v);
} else { } else {
return CompletableFuture.completedFuture(null); return CompletableFuture.completedFuture(null);
} }
}, },
name,
key, key,
type, type,
nullable, nullable,
@@ -610,6 +643,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程缓存数据 * 远程缓存数据
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
@@ -617,17 +651,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* @param remoteExpire 远程过期时长Duration.ZERO为永不过期为null表示不远程缓存 * @param remoteExpire 远程过期时长Duration.ZERO为永不过期为null表示不远程缓存
*/ */
@Override @Override
public <T> void bothSet( public <T> void bothSet(String name, String key, Type type, T value, Duration localExpire, Duration remoteExpire) {
final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) {
checkEnable(); checkEnable();
if (localExpire != null) { if (localExpire != null) {
localSetCache(key, type, value, localExpire); localSetCache(name, key, type, value, localExpire);
} }
if (remoteExpire != null && remoteSource != null) { if (remoteExpire != null && remoteSource != null) {
remoteSetCache(key, type, value, remoteExpire); remoteSetCache(name, key, type, value, remoteExpire);
} }
if (remoteSource != null && broadcastable) { 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 <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param value 数据值 * @param value 数据值
@@ -644,18 +678,18 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/ */
@Override @Override
public <T> CompletableFuture<Void> bothSetAsync( public <T> CompletableFuture<Void> bothSetAsync(
String key, Type type, T value, Duration localExpire, Duration remoteExpire) { String name, String key, Type type, T value, Duration localExpire, Duration remoteExpire) {
checkEnable(); checkEnable();
if (localExpire != null) { if (localExpire != null) {
localSetCache(key, type, value, localExpire); localSetCache(name, key, type, value, localExpire);
} }
CompletableFuture<Void> future = CompletableFuture.completedFuture(null); CompletableFuture<Void> future = CompletableFuture.completedFuture(null);
if (remoteSource != null && remoteExpire != null) { if (remoteSource != null && remoteExpire != null) {
future = remoteSetCacheAsync(key, type, value, remoteExpire); future = remoteSetCacheAsync(name, key, type, value, remoteExpire);
} }
if (remoteSource != null && broadcastable) { if (remoteSource != null && broadcastable) {
future = future.thenCompose(r -> remoteSource future = future.thenCompose(r -> remoteSource
.publishAsync(getChannelTopic(), new CachedEventMessage(node, key)) .publishAsync(getChannelTopic(), new CachedEventMessage(node, name, key))
.thenApply(n -> r)); .thenApply(n -> r));
} }
return future; return future;
@@ -664,18 +698,19 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
/** /**
* 远程删除缓存数据 * 远程删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
@Override @Override
public long bothDel(String key) { public long bothDel(String name, String key) {
checkEnable(); checkEnable();
String id = idFor(key); String id = idFor(name, key);
long v = localSource.del(id); long v = localSource.del(id);
if (remoteSource != null) { if (remoteSource != null) {
v = remoteSource.del(id); v = remoteSource.del(id);
if (broadcastable) { if (broadcastable) {
remoteSource.publish(getChannelTopic(), new CachedEventMessage(node, key)); remoteSource.publish(getChannelTopic(), new CachedEventMessage(node, name, key));
} }
} }
return v; return v;
@@ -684,19 +719,20 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
/** /**
* 远程异步删除缓存数据 * 远程异步删除缓存数据
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return 删除数量 * @return 删除数量
*/ */
@Override @Override
public CompletableFuture<Long> bothDelAsync(String key) { public CompletableFuture<Long> bothDelAsync(String name, String key) {
checkEnable(); checkEnable();
String id = idFor(key); String id = idFor(name, key);
long v = localSource.del(id); // 内存操作,无需异步 long v = localSource.del(id); // 内存操作,无需异步
if (remoteSource != null) { if (remoteSource != null) {
return remoteSource.delAsync(id).thenCompose(r -> { return remoteSource.delAsync(id).thenCompose(r -> {
return broadcastable return broadcastable
? remoteSource ? remoteSource
.publishAsync(getChannelTopic(), new CachedEventMessage(node, key)) .publishAsync(getChannelTopic(), new CachedEventMessage(node, name, key))
.thenApply(n -> r) .thenApply(n -> r)
: CompletableFuture.completedFuture(v); : CompletableFuture.completedFuture(v);
}); });
@@ -712,6 +748,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* @param <T> 泛型 * @param <T> 泛型
* @param getter 获取数据函数 * @param getter 获取数据函数
* @param setter 设置数据函数 * @param setter 设置数据函数
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -722,6 +759,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
protected <T> T getSet( protected <T> T getSet(
GetterFunc<CachedValue<T>> getter, GetterFunc<CachedValue<T>> getter,
SetterSyncFunc setter, SetterSyncFunc setter,
String name,
String key, String key,
Type type, Type type,
boolean nullable, boolean nullable,
@@ -732,8 +770,8 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
Objects.requireNonNull(expire); Objects.requireNonNull(expire);
Objects.requireNonNull(supplier); Objects.requireNonNull(supplier);
final Type cacheType = loadCacheType(type); final Type cacheType = loadCacheType(type);
final String id = idFor(key); final String id = idFor(name, key);
CachedValue<T> cacheVal = getter.get(key, expire, cacheType); CachedValue<T> cacheVal = getter.get(name, key, expire, cacheType);
if (CachedValue.isValid(cacheVal)) { if (CachedValue.isValid(cacheVal)) {
if (logable) { if (logable) {
logger.log(logLevel, "Cached got id(" + id + ") value from eitherSource"); logger.log(logLevel, "Cached got id(" + id + ") value from eitherSource");
@@ -741,7 +779,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
return cacheVal.getVal(); return cacheVal.getVal();
} }
Function<String, CachedValue> func = k -> { Function<String, CachedValue> func = k -> {
CachedValue<T> oldCacheVal = getter.get(key, expire, cacheType); CachedValue<T> oldCacheVal = getter.get(name, key, expire, cacheType);
if (CachedValue.isValid(oldCacheVal)) { if (CachedValue.isValid(oldCacheVal)) {
return oldCacheVal; return oldCacheVal;
} }
@@ -754,7 +792,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
throw new RedkaleException(t); throw new RedkaleException(t);
} }
if (CachedValue.isValid(newCacheVal)) { if (CachedValue.isValid(newCacheVal)) {
setter.set(key, expire, cacheType, newCacheVal); setter.set(name, key, expire, cacheType, newCacheVal);
} }
return newCacheVal; return newCacheVal;
}; };
@@ -772,6 +810,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* @param <T> 泛型 * @param <T> 泛型
* @param getter 获取数据函数 * @param getter 获取数据函数
* @param setter 设置数据函数 * @param setter 设置数据函数
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param type 数据类型 * @param type 数据类型
* @param nullable 是否缓存null值 * @param nullable 是否缓存null值
@@ -782,6 +821,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
protected <T> CompletableFuture<T> getSetAsync( protected <T> CompletableFuture<T> getSetAsync(
GetterFunc<CompletableFuture<CachedValue<T>>> getter, GetterFunc<CompletableFuture<CachedValue<T>>> getter,
SetterAsyncFunc setter, SetterAsyncFunc setter,
String name,
String key, String key,
Type type, Type type,
boolean nullable, boolean nullable,
@@ -791,8 +831,8 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
boolean logable = logger.isLoggable(logLevel); boolean logable = logger.isLoggable(logLevel);
Objects.requireNonNull(supplier); Objects.requireNonNull(supplier);
final Type cacheType = loadCacheType(type); final Type cacheType = loadCacheType(type);
final String id = idFor(key); final String id = idFor(name, key);
CompletableFuture<CachedValue<T>> sourceFuture = getter.get(key, expire, cacheType); CompletableFuture<CachedValue<T>> sourceFuture = getter.get(name, key, expire, cacheType);
return sourceFuture.thenCompose(val -> { return sourceFuture.thenCompose(val -> {
if (CachedValue.isValid(val)) { if (CachedValue.isValid(val)) {
if (logable) { if (logable) {
@@ -810,7 +850,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
} }
CachedValue<T> cacheVal = toCacheValue(nullable, v); CachedValue<T> cacheVal = toCacheValue(nullable, v);
if (CachedValue.isValid(cacheVal)) { 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))); .whenComplete((v2, e2) -> lock.success(CachedValue.get(cacheVal)));
} else { } else {
lock.success(CachedValue.get(cacheVal)); lock.success(CachedValue.get(cacheVal));
@@ -824,16 +864,17 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
}); });
} }
protected <T> void localSetCache(String key, Type type, T value, Duration expire) { protected <T> void localSetCache(String name, String key, Type type, T value, Duration expire) {
localSetCache(key, expire, loadCacheType(type, value), CachedValue.create(value)); localSetCache(name, key, expire, loadCacheType(type, value), CachedValue.create(value));
} }
protected <T> void localSetCache(String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) { protected <T> void localSetCache(
String name, String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) {
checkEnable(); checkEnable();
boolean logable = logger.isLoggable(logLevel); boolean logable = logger.isLoggable(logLevel);
Objects.requireNonNull(expire); Objects.requireNonNull(expire);
long millis = expire.toMillis(); long millis = expire.toMillis();
String id = idFor(key); String id = idFor(name, key);
if (logable) { if (logable) {
logger.log(logLevel, "Cached set id(" + id + ") value to localSource expire " + millis + " ms"); 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 <T> void remoteSetCache(String key, Type type, T value, Duration expire) { protected <T> void remoteSetCache(String name, String key, Type type, T value, Duration expire) {
remoteSetCache(key, expire, loadCacheType(type, value), CachedValue.create(value)); remoteSetCache(name, key, expire, loadCacheType(type, value), CachedValue.create(value));
} }
protected <T> void remoteSetCache(String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) { protected <T> void remoteSetCache(
String name, String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) {
checkEnable(); checkEnable();
boolean logable = logger.isLoggable(logLevel); boolean logable = logger.isLoggable(logLevel);
Objects.requireNonNull(expire); Objects.requireNonNull(expire);
long millis = expire.toMillis(); long millis = expire.toMillis();
String id = idFor(key); String id = idFor(name, key);
if (logable) { if (logable) {
logger.log(logLevel, "Cached set id(" + id + ") value to remoteSource expire " + millis + " ms"); 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 <T> CompletableFuture<Void> localSetCacheAsync(String key, Type type, T value, Duration expire) { protected <T> CompletableFuture<Void> localSetCacheAsync(
return localSetCacheAsync(key, expire, loadCacheType(type, value), CachedValue.create(value)); String name, String key, Type type, T value, Duration expire) {
return localSetCacheAsync(name, key, expire, loadCacheType(type, value), CachedValue.create(value));
} }
protected <T> CompletableFuture<Void> localSetCacheAsync( protected <T> CompletableFuture<Void> localSetCacheAsync(
String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) { String name, String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) {
checkEnable(); checkEnable();
boolean logable = logger.isLoggable(logLevel); boolean logable = logger.isLoggable(logLevel);
Objects.requireNonNull(expire); Objects.requireNonNull(expire);
String id = idFor(key); String id = idFor(name, key);
long millis = expire.toMillis(); long millis = expire.toMillis();
if (logable) { if (logable) {
logger.log(logLevel, "Cached set id(" + id + ") value to localSource expire " + millis + " ms"); 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 <T> CompletableFuture<Void> remoteSetCacheAsync(String key, Type type, T value, Duration expire) { protected <T> CompletableFuture<Void> remoteSetCacheAsync(
return remoteSetCacheAsync(key, expire, loadCacheType(type, value), CachedValue.create(value)); String name, String key, Type type, T value, Duration expire) {
return remoteSetCacheAsync(name, key, expire, loadCacheType(type, value), CachedValue.create(value));
} }
protected <T> CompletableFuture<Void> remoteSetCacheAsync( protected <T> CompletableFuture<Void> remoteSetCacheAsync(
String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) { String name, String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) {
checkEnable(); checkEnable();
boolean logable = logger.isLoggable(logLevel); boolean logable = logger.isLoggable(logLevel);
Objects.requireNonNull(expire); Objects.requireNonNull(expire);
String id = idFor(key); String id = idFor(name, key);
long millis = expire.toMillis(); long millis = expire.toMillis();
if (logable) { if (logable) {
logger.log(logLevel, "Cached set id(" + id + ") value to remoteSource expire " + millis + " ms"); 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 <T> CachedValue<T> bothGetCache(final String key, final Duration expire, final Type cacheType) { protected <T> CachedValue<T> bothGetCache(
final String name, final String key, final Duration expire, final Type cacheType) {
checkEnable(); checkEnable();
boolean logable = logger.isLoggable(logLevel); boolean logable = logger.isLoggable(logLevel);
String id = idFor(key); String id = idFor(name, key);
CachedValue<T> cacheVal = localSource.get(id, cacheType); CachedValue<T> cacheVal = localSource.get(id, cacheType);
if (CachedValue.isValid(cacheVal)) { if (CachedValue.isValid(cacheVal)) {
if (logable) { if (logable) {
@@ -924,7 +969,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
if (logable) { if (logable) {
logger.log(logLevel, "Cached set id(" + id + ") value to localSource from remoteSource"); 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) { if (logable) {
logger.log(logLevel, "Cached got id(" + id + ") value from remoteSource"); logger.log(logLevel, "Cached got id(" + id + ") value from remoteSource");
@@ -940,15 +985,17 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程异步获取缓存数据, 过期返回null * 远程异步获取缓存数据, 过期返回null
* *
* @param <T> 泛型 * @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @param expire 过期时长Duration.ZERO为永不过期 * @param expire 过期时长Duration.ZERO为永不过期
* @param cacheType 数据类型 * @param cacheType 数据类型
* @return 数据值 * @return 数据值
*/ */
protected <T> CompletableFuture<CachedValue<T>> bothGetCacheAsync(String key, Duration expire, Type cacheType) { protected <T> CompletableFuture<CachedValue<T>> bothGetCacheAsync(
String name, String key, Duration expire, Type cacheType) {
checkEnable(); checkEnable();
boolean logable = logger.isLoggable(logLevel); boolean logable = logger.isLoggable(logLevel);
String id = idFor(key); String id = idFor(name, key);
CachedValue<T> val = localSource.get(id, cacheType); // 内存操作,无需异步 CachedValue<T> val = localSource.get(id, cacheType); // 内存操作,无需异步
if (CachedValue.isValid(val)) { if (CachedValue.isValid(val)) {
if (logable) { if (logable) {
@@ -964,7 +1011,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
if (logable) { if (logable) {
logger.log(logLevel, "Cached set id(" + id + ") value to localSource from remoteSource"); 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) { if (logable) {
logger.log(logLevel, "Cached got id(" + id + ") value from remoteSource"); logger.log(logLevel, "Cached got id(" + id + ") value from remoteSource");
@@ -986,11 +1033,12 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
/** /**
* 创建一个锁key * 创建一个锁key
* *
* @param name 缓存名称
* @param key 缓存键 * @param key 缓存键
* @return key * @return key
*/ */
protected String idFor(String key) { protected String idFor(String name, String key) {
return schema + ':' + key; return schema + ':' + name + ':' + key;
} }
/** /**
@@ -1044,17 +1092,18 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
protected static interface GetterFunc<R> { protected static interface GetterFunc<R> {
public R get(String key, Duration expire, Type cacheType); public R get(String name, String key, Duration expire, Type cacheType);
} }
protected static interface SetterSyncFunc { 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 { protected static interface SetterAsyncFunc {
public CompletableFuture<Void> set(String key, Duration expire, Type cacheType, CachedValue cacheVal); public CompletableFuture<Void> set(
String name, String key, Duration expire, Type cacheType, CachedValue cacheVal);
} }
public class CacheRemoteListener implements CacheEventListener<CachedEventMessage> { public class CacheRemoteListener implements CacheEventListener<CachedEventMessage> {
@@ -1062,7 +1111,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
@Override @Override
public void onMessage(String topic, CachedEventMessage message) { public void onMessage(String topic, CachedEventMessage message) {
if (!Objects.equals(getNode(), message.getNode())) { if (!Objects.equals(getNode(), message.getNode())) {
localSource.del(idFor(message.getKey())); localSource.del(idFor(message.getName(), message.getKey()));
} }
} }
} }

View File

@@ -26,6 +26,8 @@ public @interface DynForCached {
String manager(); String manager();
String name();
String key(); String key();
String localExpire(); String localExpire();

View File

@@ -27,56 +27,57 @@ public class CachedInstance implements Service {
// 修改远程缓存的key值 // 修改远程缓存的key值
public void updateName(String code, Map<String, Long> map) { public void updateName(String code, Map<String, Long> 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<String, Long> map) { public String getName(String code, Map<String, Long> map) {
return code + "-" + map; return code + "-" + map;
} }
@Cached(key = "name", localExpire = "30") @Cached(name = "name", key = "name", localExpire = "30")
public String getName() { public String getName() {
return "haha"; return "haha";
} }
public void updateName(String val) { public void updateName2(String val) {
cachedManager.bothSet("name_2", String.class, val, Duration.ofSeconds(31), Duration.ofSeconds(60)); 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 { public String getName2() throws RedkaleException {
return "haha"; return "haha";
} }
@Cached(key = "dictcode", localExpire = "30", remoteExpire = "60") @Cached(name = "dictcode", key = "dictcode", localExpire = "30", remoteExpire = "60")
public CompletableFuture<String> getDictcodeAsync() { public CompletableFuture<String> getDictcodeAsync() {
System.out.println("执行了 getDictcodeAsync"); System.out.println("执行了 getDictcodeAsync");
return CompletableFuture.completedFuture("code001"); return CompletableFuture.completedFuture("code001");
} }
@Cached(key = "name", localExpire = "30") @Cached(name = "name", key = "name", localExpire = "30")
public CompletableFuture<String> getNameAsync() { public CompletableFuture<String> getNameAsync() {
return CompletableFuture.completedFuture("nameAsync"); return CompletableFuture.completedFuture("nameAsync");
} }
@Cached(key = "name", localExpire = "30", remoteExpire = "60") @Cached(name = "name", key = "name", localExpire = "30", remoteExpire = "60")
public CompletableFuture<String> getName2Async() throws IOException, InstantiationException { public CompletableFuture<String> getName2Async() throws IOException, InstantiationException {
return CompletableFuture.completedFuture("name2Async"); 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<String> idList, Map<String, File> files) { public File getInfo(ParamBean bean, int id, List<String> idList, Map<String, File> files) {
return new File("aa.txt"); 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<File> getInfoAsync(ParamBean bean, int id, List<String> idList, Map<String, File> files) { public CompletableFuture<File> getInfoAsync(ParamBean bean, int id, List<String> idList, Map<String, File> files) {
return CompletableFuture.completedFuture(new File("aa.txt")); return CompletableFuture.completedFuture(new File("aa.txt"));
} }
@Cached( @Cached(
key = "info_#{id}_file#{files.one}", name = "info",
key = "#{id}_#{files.one}",
localExpire = "30", localExpire = "30",
remoteExpire = "60", remoteExpire = "60",
timeUnit = TimeUnit.MILLISECONDS) timeUnit = TimeUnit.MILLISECONDS)

View File

@@ -83,12 +83,12 @@ public class CachedInstanceTest {
System.out.println("instance1.manager = " + instance.getCachedManager()); System.out.println("instance1.manager = " + instance.getCachedManager());
System.out.println("instance2.manager = " + instance2.getCachedManager()); System.out.println("instance2.manager = " + instance2.getCachedManager());
manager.updateBroadcastable(false); manager.updateBroadcastable(false);
instance.updateName("gege"); instance.updateName2("gege");
Assertions.assertEquals("gege", instance.getName2()); Assertions.assertEquals("gege", instance.getName2());
Assertions.assertEquals("haha", instance2.getName2()); Assertions.assertEquals("haha", instance2.getName2());
manager.updateBroadcastable(true); manager.updateBroadcastable(true);
System.out.println("准备设置 updateName"); System.out.println("准备设置 updateName");
instance.updateName("gege"); instance.updateName2("gege");
System.out.println("设置结束 updateName"); System.out.println("设置结束 updateName");
Utility.sleep(10); Utility.sleep(10);
Assertions.assertEquals("gege", instance.getName2()); Assertions.assertEquals("gege", instance.getName2());

View File

@@ -37,22 +37,22 @@ public class CachedManagerTest {
@Test @Test
public void run1() throws Exception { public void run1() throws Exception {
Duration expire = Duration.ofMillis(290); Duration expire = Duration.ofMillis(290);
manager.localSetString("cached-schema:name:haha", "myha", expire); manager.localSetString("name", "haha", "myha", expire);
Assertions.assertEquals(manager.localGetString("cached-schema:name:haha"), "myha"); Assertions.assertEquals(manager.localGetString("name", "haha"), "myha");
Utility.sleep(300); Utility.sleep(300);
Assertions.assertTrue(manager.localGetString("cached-schema:name:haha") == null); Assertions.assertTrue(manager.localGetString("name", "haha") == null);
CachingBean bean = new CachingBean(); CachingBean bean = new CachingBean();
bean.setName("tom"); bean.setName("tom");
bean.setRemark("这是名字备注"); bean.setRemark("这是名字备注");
String json = bean.toString(); String json = bean.toString();
manager.localSet(bean.getName(), CachingBean.class, bean, expire); manager.localSet("name", bean.getName(), CachingBean.class, bean, expire);
Assertions.assertEquals( Assertions.assertEquals(
manager.localGet(bean.getName(), CachingBean.class).toString(), json); manager.localGet("name", bean.getName(), CachingBean.class).toString(), json);
bean.setRemark(bean.getRemark() + "-新备注"); bean.setRemark(bean.getRemark() + "-新备注");
Assertions.assertEquals( Assertions.assertEquals(
manager.localGet(bean.getName(), CachingBean.class).toString(), json); manager.localGet("name", bean.getName(), CachingBean.class).toString(), json);
} }
@Test @Test
@@ -67,7 +67,13 @@ public class CachedManagerTest {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
new Thread(() -> { new Thread(() -> {
manager.bothGetSet( manager.bothGetSet(
"name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); "name",
"name",
String.class,
false,
localExpire,
remoteExpire,
() -> bean.getName());
cdl.countDown(); cdl.countDown();
}) })
.start(); .start();
@@ -76,7 +82,7 @@ public class CachedManagerTest {
} }
Assertions.assertEquals(1, ParallelBean.c1.get()); Assertions.assertEquals(1, ParallelBean.c1.get());
Utility.sleep(200); 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()); Assertions.assertEquals(1, ParallelBean.c1.get());
Utility.sleep(300); Utility.sleep(300);
{ {
@@ -84,7 +90,13 @@ public class CachedManagerTest {
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
new Thread(() -> { new Thread(() -> {
manager.bothGetSet( manager.bothGetSet(
"name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); "name",
"name",
String.class,
false,
localExpire,
remoteExpire,
() -> bean.getName());
cdl.countDown(); cdl.countDown();
}) })
.start(); .start();

View File

@@ -46,6 +46,7 @@ public class _DynLocalCacheInstance extends CachedInstance {
@DynForCached( @DynForCached(
dynField = "_redkale_getNameCachedAction1", dynField = "_redkale_getNameCachedAction1",
manager = "", manager = "",
name = "name",
key = "name", key = "name",
nullable = false, nullable = false,
timeUnit = TimeUnit.SECONDS, timeUnit = TimeUnit.SECONDS,
@@ -63,7 +64,8 @@ public class _DynLocalCacheInstance extends CachedInstance {
@DynForCached( @DynForCached(
dynField = "_redkale_getInfoCachedAction2", dynField = "_redkale_getInfoCachedAction2",
manager = "", manager = "",
key = "info_#{id}_file#{files.one}", name = "info",
key = "#{id}_#{files.one}",
nullable = false, nullable = false,
timeUnit = TimeUnit.SECONDS, timeUnit = TimeUnit.SECONDS,
remoteExpire = "60", remoteExpire = "60",
@@ -81,6 +83,7 @@ public class _DynLocalCacheInstance extends CachedInstance {
@DynForCached( @DynForCached(
dynField = "_redkale_getNameAsyncCachedAction3", dynField = "_redkale_getNameAsyncCachedAction3",
manager = "", manager = "",
name = "name",
key = "name", key = "name",
nullable = false, nullable = false,
timeUnit = TimeUnit.SECONDS, timeUnit = TimeUnit.SECONDS,
@@ -98,7 +101,8 @@ public class _DynLocalCacheInstance extends CachedInstance {
@DynForCached( @DynForCached(
dynField = "_redkale_getInfo2AsyncCachedAction4", dynField = "_redkale_getInfo2AsyncCachedAction4",
manager = "", manager = "",
key = "info_#{id}_file#{files.one}", name = "info",
key = "#{id}_#{files.one}",
nullable = false, nullable = false,
timeUnit = TimeUnit.SECONDS, timeUnit = TimeUnit.SECONDS,
remoteExpire = "60", remoteExpire = "60",
@@ -120,6 +124,7 @@ public class _DynLocalCacheInstance extends CachedInstance {
@DynForCached( @DynForCached(
dynField = "_redkale_getName2AsyncCachedAction5", dynField = "_redkale_getName2AsyncCachedAction5",
manager = "", manager = "",
name = "name",
key = "name", key = "name",
nullable = false, nullable = false,
timeUnit = TimeUnit.SECONDS, timeUnit = TimeUnit.SECONDS,
@@ -137,7 +142,8 @@ public class _DynLocalCacheInstance extends CachedInstance {
@DynForCached( @DynForCached(
dynField = "_redkale_getInfoAsyncCachedAction6", dynField = "_redkale_getInfoAsyncCachedAction6",
manager = "", manager = "",
key = "info_#{id}_file#{files.one}", name = "info",
key = "#{id}_#{files.one}",
nullable = false, nullable = false,
timeUnit = TimeUnit.SECONDS, timeUnit = TimeUnit.SECONDS,
remoteExpire = "60", remoteExpire = "60",
@@ -156,6 +162,7 @@ public class _DynLocalCacheInstance extends CachedInstance {
@DynForCached( @DynForCached(
dynField = "_redkale_getName2CachedAction7", dynField = "_redkale_getName2CachedAction7",
manager = "", manager = "",
name = "name",
key = "name", key = "name",
nullable = false, nullable = false,
timeUnit = TimeUnit.SECONDS, timeUnit = TimeUnit.SECONDS,