CacheManager
This commit is contained in:
@@ -254,7 +254,7 @@ public interface CacheManager {
|
|||||||
|
|
||||||
//-------------------------------------- both缓存 --------------------------------------
|
//-------------------------------------- both缓存 --------------------------------------
|
||||||
/**
|
/**
|
||||||
* 远程获取缓存数据, 过期返回null
|
* 本地或远程获取缓存数据, 过期返回null
|
||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
@@ -266,7 +266,7 @@ public interface CacheManager {
|
|||||||
public <T> T bothGet(final String hash, final String key, final Type type);
|
public <T> T bothGet(final String hash, final String key, final Type type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程获取字符串缓存数据, 过期返回null
|
* 本地或远程获取字符串缓存数据, 过期返回null
|
||||||
*
|
*
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
@@ -278,7 +278,7 @@ public interface CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程异步获取缓存数据, 过期返回null
|
* 本地或远程异步获取缓存数据, 过期返回null
|
||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
@@ -290,7 +290,7 @@ public interface CacheManager {
|
|||||||
public <T> CompletableFuture<T> bothGetAsync(final String hash, final String key, final Type type);
|
public <T> CompletableFuture<T> bothGetAsync(final String hash, final String key, final Type type);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程异步获取字符串缓存数据, 过期返回null
|
* 本地或远程异步获取字符串缓存数据, 过期返回null
|
||||||
*
|
*
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
@@ -302,7 +302,37 @@ public interface CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程缓存数据
|
* 本地或远程获取缓存数据, 过期返回null
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @param hash 缓存hash
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param type 数据类型
|
||||||
|
* @param localExpire 本地过期时长,为null表示永不过期
|
||||||
|
* @param remoteExpire 远程过期时长,为null表示永不过期
|
||||||
|
* @param supplier 数据函数
|
||||||
|
*
|
||||||
|
* @return 数据值
|
||||||
|
*/
|
||||||
|
public <T> T bothGet(String hash, String key, Type type, Duration localExpire, Duration remoteExpire, Supplier<T> supplier);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地或远程异步获取缓存数据, 过期返回null
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @param hash 缓存hash
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param type 数据类型
|
||||||
|
* @param localExpire 本地过期时长,为null表示永不过期
|
||||||
|
* @param remoteExpire 远程过期时长,为null表示永不过期
|
||||||
|
* @param supplier 数据函数
|
||||||
|
*
|
||||||
|
* @return 数据值
|
||||||
|
*/
|
||||||
|
public <T> CompletableFuture<T> bothGetAsync(String hash, String key, Type type, Duration localExpire, Duration remoteExpire, Supplier<CompletableFuture<T>> supplier);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 本地和远程缓存数据
|
||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
@@ -315,7 +345,7 @@ public interface CacheManager {
|
|||||||
public <T> void bothSet(final String hash, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire);
|
public <T> void bothSet(final String hash, final String key, final Type type, final T value, Duration localExpire, Duration remoteExpire);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程缓存字符串数据
|
* 本地和远程缓存字符串数据
|
||||||
*
|
*
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
@@ -328,7 +358,7 @@ public interface CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程异步缓存数据
|
* 本地和远程异步缓存数据
|
||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
@@ -341,7 +371,7 @@ public interface CacheManager {
|
|||||||
public <T> CompletableFuture<Void> bothSetAsync(String hash, String key, Type type, T value, Duration localExpire, Duration remoteExpire);
|
public <T> CompletableFuture<Void> bothSetAsync(String hash, String key, Type type, T value, Duration localExpire, Duration remoteExpire);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程异步缓存字符串数据
|
* 本地和远程异步缓存字符串数据
|
||||||
*
|
*
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
@@ -354,7 +384,7 @@ public interface CacheManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程删除缓存数据
|
* 本地和远程删除缓存数据
|
||||||
*
|
*
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
@@ -364,7 +394,7 @@ public interface CacheManager {
|
|||||||
public long bothDel(String hash, String key);
|
public long bothDel(String hash, String key);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程异步删除缓存数据
|
* 本地和远程异步删除缓存数据
|
||||||
*
|
*
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
package org.redkale.cache.support;
|
package org.redkale.cache.support;
|
||||||
|
|
||||||
import java.lang.reflect.Type;
|
import java.lang.reflect.Type;
|
||||||
|
import java.math.BigDecimal;
|
||||||
import java.time.Duration;
|
import java.time.Duration;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@@ -162,7 +163,7 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
* @return 数据值
|
* @return 数据值
|
||||||
*/
|
*/
|
||||||
public <T> T localGet(final String hash, final String key, final Type type, Duration expire, Supplier<T> supplier) {
|
public <T> T localGet(final String hash, final String key, final Type type, Duration expire, Supplier<T> supplier) {
|
||||||
return get(localSource, hash, key, type, expire, supplier);
|
return get(localSource::hget, localSource::hset, hash, key, type, expire, supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -179,7 +180,7 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
*/
|
*/
|
||||||
@Override
|
@Override
|
||||||
public <T> CompletableFuture<T> localGetAsync(String hash, String key, Type type, Duration expire, Supplier<CompletableFuture<T>> supplier) {
|
public <T> CompletableFuture<T> localGetAsync(String hash, String key, Type type, Duration expire, Supplier<CompletableFuture<T>> supplier) {
|
||||||
return get(localSource, hash, key, type, expire, supplier);
|
return getAsync(localSource::hgetAsync, localSource::hsetAsync, hash, key, type, expire, supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -260,7 +261,7 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
* @return 数据值
|
* @return 数据值
|
||||||
*/
|
*/
|
||||||
public <T> T remoteGet(final String hash, final String key, final Type type, Duration expire, Supplier<T> supplier) {
|
public <T> T remoteGet(final String hash, final String key, final Type type, Duration expire, Supplier<T> supplier) {
|
||||||
return get(remoteSource, hash, key, type, expire, supplier);
|
return get(remoteSource::hget, remoteSource::hset, hash, key, type, expire, supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -276,7 +277,7 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
* @return 数据值
|
* @return 数据值
|
||||||
*/
|
*/
|
||||||
public <T> CompletableFuture<T> remoteGetAsync(String hash, String key, Type type, Duration expire, Supplier<CompletableFuture<T>> supplier) {
|
public <T> CompletableFuture<T> remoteGetAsync(String hash, String key, Type type, Duration expire, Supplier<CompletableFuture<T>> supplier) {
|
||||||
return getAsync(remoteSource, hash, key, type, expire, supplier);
|
return getAsync(remoteSource::hgetAsync, remoteSource::hsetAsync, hash, key, type, expire, supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -346,17 +347,9 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
*
|
*
|
||||||
* @return 数据值
|
* @return 数据值
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public <T> T bothGet(final String hash, final String key, final Type type) {
|
public <T> T bothGet(final String hash, final String key, final Type type) {
|
||||||
Type t = loadCacheType(type);
|
return CacheValue.get(bothGetCache(hash, key, type));
|
||||||
CacheValue<T> val = localSource.hget(hash, key, t);
|
|
||||||
if (CacheValue.isValid(val)) {
|
|
||||||
return val.getValue();
|
|
||||||
}
|
|
||||||
if (remoteSource != null) {
|
|
||||||
return CacheValue.get(remoteSource.hget(hash, key, t));
|
|
||||||
} else {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -369,18 +362,57 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
*
|
*
|
||||||
* @return 数据值
|
* @return 数据值
|
||||||
*/
|
*/
|
||||||
|
@Override
|
||||||
public <T> CompletableFuture<T> bothGetAsync(final String hash, final String key, final Type type) {
|
public <T> CompletableFuture<T> bothGetAsync(final String hash, final String key, final Type type) {
|
||||||
Type t = loadCacheType(type);
|
return bothGetCacheAsync(hash, key, type).thenApply(CacheValue::get);
|
||||||
CacheValue<T> val = localSource.hget(hash, key, t);
|
|
||||||
if (CacheValue.isValid(val)) {
|
|
||||||
return CompletableFuture.completedFuture(val.getValue());
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 远程获取缓存数据, 过期返回null
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @param hash 缓存hash
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param type 数据类型
|
||||||
|
* @param localExpire 本地过期时长,为null表示永不过期
|
||||||
|
* @param remoteExpire 远程过期时长,为null表示永不过期
|
||||||
|
* @param supplier 数据函数
|
||||||
|
*
|
||||||
|
* @return 数据值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <T> T bothGet(final String hash, final String key, final Type type, Duration localExpire, Duration remoteExpire, Supplier<T> supplier) {
|
||||||
|
return get(this::bothGetCache, (h, k, t, v) -> {
|
||||||
|
localSource.hset(key, key, type, v);
|
||||||
if (remoteSource != null) {
|
if (remoteSource != null) {
|
||||||
CompletableFuture<CacheValue<T>> future = remoteSource.hgetAsync(hash, key, t);
|
remoteSource.hset(hash, key, t, CacheValue.create(v.getValue(), remoteExpire));
|
||||||
return future.thenApply(CacheValue::get);
|
}
|
||||||
|
}, hash, key, type, localExpire, supplier);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 远程异步获取缓存数据, 过期返回null
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @param hash 缓存hash
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param type 数据类型
|
||||||
|
* @param localExpire 本地过期时长,为null表示永不过期
|
||||||
|
* @param remoteExpire 远程过期时长,为null表示永不过期
|
||||||
|
* @param supplier 数据函数
|
||||||
|
*
|
||||||
|
* @return 数据值
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public <T> CompletableFuture<T> bothGetAsync(String hash, String key, Type type, Duration localExpire, Duration remoteExpire, Supplier<CompletableFuture<T>> supplier) {
|
||||||
|
return getAsync(this::bothGetCacheAsync, (h, k, t, v) -> {
|
||||||
|
localSource.hset(key, key, type, v);
|
||||||
|
if (remoteSource != null) {
|
||||||
|
return remoteSource.hsetAsync(hash, key, t, CacheValue.create(v.getValue(), remoteExpire));
|
||||||
} else {
|
} else {
|
||||||
return CompletableFuture.completedFuture(null);
|
return CompletableFuture.completedFuture(null);
|
||||||
}
|
}
|
||||||
|
}, hash, key, type, localExpire, supplier);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -402,6 +434,11 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static void main(String[] args) throws Throwable {
|
||||||
|
BigDecimal z = new BigDecimal("0");
|
||||||
|
System.out.println(Objects.equals(BigDecimal.ZERO, z));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 远程异步缓存数据
|
* 远程异步缓存数据
|
||||||
*
|
*
|
||||||
@@ -462,7 +499,8 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
* 获取缓存数据, 过期返回null
|
* 获取缓存数据, 过期返回null
|
||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
* @param source 缓存源
|
* @param getter 获取数据函数
|
||||||
|
* @param setter 设置数据函数
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
* @param type 数据类型
|
* @param type 数据类型
|
||||||
@@ -471,21 +509,22 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
*
|
*
|
||||||
* @return 数据值
|
* @return 数据值
|
||||||
*/
|
*/
|
||||||
protected <T> T get(CacheSource source, String hash, String key, Type type, Duration expire, Supplier<T> supplier) {
|
protected <T> T get(GetterFunc<CacheValue<T>> getter, SetterSyncFunc setter,
|
||||||
|
String hash, String key, Type type, Duration expire, Supplier<T> supplier) {
|
||||||
Objects.requireNonNull(supplier);
|
Objects.requireNonNull(supplier);
|
||||||
final Type t = loadCacheType(type);
|
final Type t = loadCacheType(type);
|
||||||
CacheValue<T> val = source.hget(hash, key, t);
|
CacheValue<T> val = getter.apply(hash, key, t);
|
||||||
if (CacheValue.isValid(val)) {
|
if (CacheValue.isValid(val)) {
|
||||||
return val.getValue();
|
return val.getValue();
|
||||||
}
|
}
|
||||||
Function<String, CacheValue> func = k -> {
|
Function<String, CacheValue> func = k -> {
|
||||||
CacheValue<T> oldVal = source.hget(hash, key, t);
|
CacheValue<T> oldVal = getter.apply(hash, key, t);
|
||||||
if (CacheValue.isValid(oldVal)) {
|
if (CacheValue.isValid(oldVal)) {
|
||||||
return oldVal;
|
return oldVal;
|
||||||
}
|
}
|
||||||
CacheValue<T> newVal = toCacheSupplier(expire, supplier).get();
|
CacheValue<T> newVal = toCacheSupplier(expire, supplier).get();
|
||||||
if (CacheValue.isValid(newVal)) {
|
if (CacheValue.isValid(newVal)) {
|
||||||
source.hset(hash, key, t, newVal);
|
setter.apply(hash, key, t, newVal);
|
||||||
}
|
}
|
||||||
return newVal;
|
return newVal;
|
||||||
};
|
};
|
||||||
@@ -502,7 +541,8 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
* 异步获取缓存数据, 过期返回null
|
* 异步获取缓存数据, 过期返回null
|
||||||
*
|
*
|
||||||
* @param <T> 泛型
|
* @param <T> 泛型
|
||||||
* @param source 缓存源
|
* @param getter 获取数据函数
|
||||||
|
* @param setter 设置数据函数
|
||||||
* @param hash 缓存hash
|
* @param hash 缓存hash
|
||||||
* @param key 缓存键
|
* @param key 缓存键
|
||||||
* @param type 数据类型
|
* @param type 数据类型
|
||||||
@@ -511,10 +551,12 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
*
|
*
|
||||||
* @return 数据值
|
* @return 数据值
|
||||||
*/
|
*/
|
||||||
protected <T> CompletableFuture<T> getAsync(CacheSource source, String hash, String key, Type type, Duration expire, Supplier<CompletableFuture<T>> supplier) {
|
protected <T> CompletableFuture<T> getAsync(GetterFunc<CompletableFuture<CacheValue<T>>> getter, SetterAsyncFunc setter,
|
||||||
|
String hash, String key, Type type, Duration expire, Supplier<CompletableFuture<T>> supplier) {
|
||||||
Objects.requireNonNull(supplier);
|
Objects.requireNonNull(supplier);
|
||||||
final Type t = loadCacheType(type);
|
final Type t = loadCacheType(type);
|
||||||
CacheValue<T> val = source.hget(hash, key, t);
|
CompletableFuture<CacheValue<T>> sourceFuture = getter.apply(hash, key, t);
|
||||||
|
return sourceFuture.thenCompose(val -> {
|
||||||
if (CacheValue.isValid(val)) {
|
if (CacheValue.isValid(val)) {
|
||||||
return CompletableFuture.completedFuture(val.getValue());
|
return CompletableFuture.completedFuture(val.getValue());
|
||||||
}
|
}
|
||||||
@@ -529,15 +571,54 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
}
|
}
|
||||||
CacheValue<T> rs = toCacheValue(expire, v);
|
CacheValue<T> rs = toCacheValue(expire, v);
|
||||||
if (CacheValue.isValid(val)) {
|
if (CacheValue.isValid(val)) {
|
||||||
source.hset(hash, key, t, val);
|
setter.apply(hash, key, t, val)
|
||||||
}
|
.whenComplete((v2, e2) -> entry.success(CacheValue.get(rs)));
|
||||||
|
} else {
|
||||||
entry.success(CacheValue.get(rs));
|
entry.success(CacheValue.get(rs));
|
||||||
|
}
|
||||||
});
|
});
|
||||||
} catch (Throwable e) {
|
} catch (Throwable e) {
|
||||||
entry.fail(e);
|
entry.fail(e);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return future;
|
return future;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
protected <T> CacheValue<T> bothGetCache(final String hash, final String key, final Type type) {
|
||||||
|
Type t = loadCacheType(type);
|
||||||
|
CacheValue<T> val = localSource.hget(hash, key, t);
|
||||||
|
if (CacheValue.isValid(val)) {
|
||||||
|
return val;
|
||||||
|
}
|
||||||
|
if (remoteSource != null) {
|
||||||
|
return remoteSource.hget(hash, key, t);
|
||||||
|
} else {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 远程异步获取缓存数据, 过期返回null
|
||||||
|
*
|
||||||
|
* @param <T> 泛型
|
||||||
|
* @param hash 缓存hash
|
||||||
|
* @param key 缓存键
|
||||||
|
* @param type 数据类型
|
||||||
|
*
|
||||||
|
* @return 数据值
|
||||||
|
*/
|
||||||
|
protected <T> CompletableFuture<CacheValue<T>> bothGetCacheAsync(final String hash, final String key, final Type type) {
|
||||||
|
Type t = loadCacheType(type);
|
||||||
|
CacheValue<T> val = localSource.hget(hash, key, t);
|
||||||
|
if (CacheValue.isValid(val)) {
|
||||||
|
return CompletableFuture.completedFuture(val);
|
||||||
|
}
|
||||||
|
if (remoteSource != null) {
|
||||||
|
return remoteSource.hgetAsync(hash, key, t);
|
||||||
|
} else {
|
||||||
|
return CompletableFuture.completedFuture(null);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -604,6 +685,21 @@ public class CacheManagerService implements CacheManager, Service {
|
|||||||
|
|
||||||
private static final Object NIL = new Object();
|
private static final Object NIL = new Object();
|
||||||
|
|
||||||
|
protected static interface GetterFunc<R> {
|
||||||
|
|
||||||
|
public R apply(String hash, String key, Type type);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static interface SetterSyncFunc {
|
||||||
|
|
||||||
|
public void apply(String hash, String key, Type type, CacheValue value);
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static interface SetterAsyncFunc {
|
||||||
|
|
||||||
|
public CompletableFuture<Void> apply(String hash, String key, Type type, CacheValue value);
|
||||||
|
}
|
||||||
|
|
||||||
protected class CacheAsyncEntry {
|
protected class CacheAsyncEntry {
|
||||||
|
|
||||||
private final AtomicBoolean state = new AtomicBoolean();
|
private final AtomicBoolean state = new AtomicBoolean();
|
||||||
|
|||||||
Reference in New Issue
Block a user