增加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

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

View File

@@ -91,39 +91,42 @@ public interface CachedManager extends Resourcable {
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 处理函数
*/
public void acceptCachedAction(String templetKey, Consumer<CachedAction> consumer);
public void acceptCachedAction(String name, Consumer<CachedAction> consumer);
// -------------------------------------- 本地缓存 --------------------------------------
/**
* 本地获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
public <T> T localGet(final String key, final Type type);
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -131,12 +134,14 @@ public interface CachedManager extends Resourcable {
* @param supplier 数据函数
* @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
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -145,83 +150,96 @@ public interface CachedManager extends Resourcable {
* @return 数据值
*/
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 name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @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 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
public <T> T remoteGet(final String key, final Type type);
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @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
*
* @param name 缓存名称
* @param key 缓存键
* @return 数据值
*/
default CompletableFuture<String> remoteGetStringAsync(final String key) {
return remoteGetAsync(key, String.class);
default CompletableFuture<String> remoteGetStringAsync(String name, String key) {
return remoteGetAsync(name, key, String.class);
}
/**
* 远程获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -229,12 +247,14 @@ public interface CachedManager extends Resourcable {
* @param supplier 数据函数
* @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
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -243,115 +263,131 @@ public interface CachedManager extends Resourcable {
* @return 数据值
*/
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 name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @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 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期
* @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 value 数据值
* @param expire 过期时长Duration.ZERO为永不过期
* @return void
*/
default CompletableFuture<Void> remoteSetStringAsync(final String key, final String value, Duration expire) {
return remoteSetAsync(key, String.class, value, expire);
default CompletableFuture<Void> 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<Long> remoteDelAsync(String key);
public CompletableFuture<Long> remoteDelAsync(String name, String key);
// -------------------------------------- both缓存 --------------------------------------
/**
* 本地或远程获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
public <T> T bothGet(final String key, final Type type);
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
public <T> CompletableFuture<T> bothGetAsync(final String key, final Type type);
public <T> CompletableFuture<T> bothGetAsync(String name, String key, Type type);
/**
* 本地或远程异步获取字符串缓存数据, 过期返回null
*
* @param name 缓存名称
* @param key 缓存键
* @return 数据值
*/
default CompletableFuture<String> bothGetStringAsync(final String key) {
return bothGetAsync(key, String.class);
default CompletableFuture<String> bothGetStringAsync(String name, String key) {
return bothGetAsync(name, key, String.class);
}
/**
* 本地或远程获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -361,6 +397,7 @@ public interface CachedManager extends Resourcable {
* @return 数据值
*/
public <T> T bothGetSet(
String name,
String key,
Type type,
boolean nullable,
@@ -372,6 +409,7 @@ public interface CachedManager extends Resourcable {
* 本地或远程异步获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -381,6 +419,7 @@ public interface CachedManager extends Resourcable {
* @return 数据值
*/
public <T> CompletableFuture<T> bothGetSetAsync(
String name,
String key,
Type type,
boolean nullable,
@@ -392,30 +431,33 @@ public interface CachedManager extends Resourcable {
* 本地和远程缓存数据
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param localExpire 本地过期时长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 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
@@ -424,11 +466,12 @@ public interface CachedManager extends Resourcable {
* @return void
*/
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 value 数据值
* @param localExpire 本地过期时长Duration.ZERO为永不过期为null表示不本地缓存
@@ -436,23 +479,25 @@ public interface CachedManager extends Resourcable {
* @return void
*/
default CompletableFuture<Void> 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<Long> bothDelAsync(String key);
public CompletableFuture<Long> bothDelAsync(String name, String key);
}

View File

@@ -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> T get(ThrowSupplier<T> 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
+ "}";

View File

@@ -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() {

View File

@@ -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;
}

View File

@@ -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 "";
}
};

View File

@@ -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);

View File

@@ -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<CachedAction> consumer) {
actions.stream()
.filter(v -> Objects.equals(v.getTempletKey(), templetKey))
.forEach(consumer);
public void acceptCachedAction(String name, Consumer<CachedAction> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
@Override
public <T> T localGet(final String key, final Type type) {
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -284,10 +284,11 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/
@Override
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(
(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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -308,10 +310,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/
@Override
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(
(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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期
*/
@Override
public <T> void localSet(String key, Type type, T value, Duration expire) {
localSetCache(key, type, value, expire);
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
@Override
public <T> T remoteGet(final String key, final Type type) {
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
@Override
public <T> CompletableFuture<T> remoteGetAsync(final String key, final Type type) {
public <T> CompletableFuture<T> remoteGetAsync(String name, String key, Type type) {
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);
}
@@ -379,6 +391,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
* 远程获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -388,10 +401,11 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/
@Override
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(
(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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -412,10 +427,16 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/
@Override
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(
(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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期
*/
@Override
public <T> void remoteSet(final String key, final Type type, final T value, Duration expire) {
remoteSetCache(key, type, value, expire);
public <T> void remoteSet(String name, String key, Type type, T value, Duration expire) {
remoteSetCache(name, key, type, value, expire);
}
/**
* 远程异步缓存数据
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
* @param expire 过期时长Duration.ZERO为永不过期
*/
@Override
public <T> CompletableFuture<Void> remoteSetAsync(String key, Type type, T value, Duration expire) {
return remoteSetCacheAsync(key, type, value, expire);
public <T> CompletableFuture<Void> 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<Long> remoteDelAsync(String key) {
public CompletableFuture<Long> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
@Override
public <T> T bothGet(final String key, final Type type) {
return CachedValue.get(bothGetCache(key, (Duration) null, type));
public <T> T bothGet(String name, String key, Type type) {
return CachedValue.get(bothGetCache(name, key, (Duration) null, type));
}
/**
* 远程异步获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @return 数据值
*/
@Override
public <T> CompletableFuture<T> bothGetAsync(final String key, final Type type) {
return bothGetCacheAsync(key, (Duration) null, type).thenApply(CachedValue::get);
public <T> CompletableFuture<T> bothGetAsync(String name, String key, Type type) {
return bothGetCacheAsync(name, key, (Duration) null, type).thenApply(CachedValue::get);
}
/**
* 远程获取缓存数据, 过期返回null
*
* @param <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -516,8 +544,9 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/
@Override
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param nullable 是否缓存null值
@@ -568,6 +599,7 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/
@Override
public <T> CompletableFuture<T> 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 <T> 泛型
* @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 <T> void bothSet(
final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire) {
public <T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param type 数据类型
* @param value 数据值
@@ -644,18 +678,18 @@ public class CachedManagerService implements CachedManager, CachedActionFunc, Se
*/
@Override
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();
if (localExpire != null) {
localSetCache(key, type, value, localExpire);
localSetCache(name, key, type, value, localExpire);
}
CompletableFuture<Void> 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<Long> bothDelAsync(String key) {
public CompletableFuture<Long> 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 <T> 泛型
* @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> T getSet(
GetterFunc<CachedValue<T>> 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<T> cacheVal = getter.get(key, expire, cacheType);
final String id = idFor(name, key);
CachedValue<T> 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<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)) {
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 <T> 泛型
* @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 <T> CompletableFuture<T> getSetAsync(
GetterFunc<CompletableFuture<CachedValue<T>>> 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<CachedValue<T>> sourceFuture = getter.get(key, expire, cacheType);
final String id = idFor(name, key);
CompletableFuture<CachedValue<T>> 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<T> 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 <T> void localSetCache(String key, Type type, T value, Duration expire) {
localSetCache(key, expire, loadCacheType(type, value), CachedValue.create(value));
protected <T> void localSetCache(String name, String key, Type type, T value, Duration expire) {
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();
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 <T> void remoteSetCache(String key, Type type, T value, Duration expire) {
remoteSetCache(key, expire, loadCacheType(type, value), CachedValue.create(value));
protected <T> void remoteSetCache(String name, String key, Type type, T value, Duration expire) {
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();
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 <T> CompletableFuture<Void> localSetCacheAsync(String key, Type type, T value, Duration expire) {
return localSetCacheAsync(key, expire, loadCacheType(type, value), CachedValue.create(value));
protected <T> CompletableFuture<Void> localSetCacheAsync(
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(
String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) {
String name, String key, Duration expire, Type cacheType, CachedValue<T> 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 <T> CompletableFuture<Void> remoteSetCacheAsync(String key, Type type, T value, Duration expire) {
return remoteSetCacheAsync(key, expire, loadCacheType(type, value), CachedValue.create(value));
protected <T> CompletableFuture<Void> remoteSetCacheAsync(
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(
String key, Duration expire, Type cacheType, CachedValue<T> cacheVal) {
String name, String key, Duration expire, Type cacheType, CachedValue<T> 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 <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();
boolean logable = logger.isLoggable(logLevel);
String id = idFor(key);
String id = idFor(name, key);
CachedValue<T> 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 <T> 泛型
* @param name 缓存名称
* @param key 缓存键
* @param expire 过期时长Duration.ZERO为永不过期
* @param cacheType 数据类型
* @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();
boolean logable = logger.isLoggable(logLevel);
String id = idFor(key);
String id = idFor(name, key);
CachedValue<T> 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<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 {
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<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> {
@@ -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()));
}
}
}

View File

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

View File

@@ -27,56 +27,57 @@ public class CachedInstance implements Service {
// 修改远程缓存的key值
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) {
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<String> getDictcodeAsync() {
System.out.println("执行了 getDictcodeAsync");
return CompletableFuture.completedFuture("code001");
}
@Cached(key = "name", localExpire = "30")
@Cached(name = "name", key = "name", localExpire = "30")
public CompletableFuture<String> getNameAsync() {
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 {
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) {
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) {
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)

View File

@@ -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());

View File

@@ -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();

View File

@@ -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,