CacheManager

This commit is contained in:
redkale
2023-12-21 10:32:33 +08:00
parent cda78cfc7d
commit 63c3326265
4 changed files with 30 additions and 30 deletions

View File

@@ -58,7 +58,7 @@ public interface CacheManager {
* *
* @return 数据值 * @return 数据值
*/ */
public <T> T localGet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier<T> supplier); public <T> T localGetSet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier<T> supplier);
/** /**
* 本地异步获取缓存数据, 过期返回null * 本地异步获取缓存数据, 过期返回null
@@ -73,7 +73,7 @@ public interface CacheManager {
* *
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> localGetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier); public <T> CompletableFuture<T> localGetSetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier);
/** /**
* 本地缓存数据 * 本地缓存数据
@@ -171,7 +171,7 @@ public interface CacheManager {
* *
* @return 数据值 * @return 数据值
*/ */
public <T> T remoteGet(final String hash, final String key, final Type type, boolean nullable, public <T> T remoteGetSet(final String hash, final String key, final Type type, boolean nullable,
Duration expire, Supplier<T> supplier); Duration expire, Supplier<T> supplier);
/** /**
@@ -187,7 +187,7 @@ public interface CacheManager {
* *
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> remoteGetAsync(String hash, String key, Type type, boolean nullable, public <T> CompletableFuture<T> remoteGetSetAsync(String hash, String key, Type type, boolean nullable,
Duration expire, Supplier<CompletableFuture<T>> supplier); Duration expire, Supplier<CompletableFuture<T>> supplier);
/** /**
@@ -325,7 +325,7 @@ public interface CacheManager {
* *
* @return 数据值 * @return 数据值
*/ */
public <T> T bothGet(String hash, String key, Type type, boolean nullable, public <T> T bothGetSet(String hash, String key, Type type, boolean nullable,
Duration localExpire, Duration remoteExpire, Supplier<T> supplier); Duration localExpire, Duration remoteExpire, Supplier<T> supplier);
/** /**
@@ -342,7 +342,7 @@ public interface CacheManager {
* *
* @return 数据值 * @return 数据值
*/ */
public <T> CompletableFuture<T> bothGetAsync(String hash, String key, Type type, boolean nullable, public <T> CompletableFuture<T> bothGetSetAsync(String hash, String key, Type type, boolean nullable,
Duration localExpire, Duration remoteExpire, Supplier<CompletableFuture<T>> supplier); Duration localExpire, Duration remoteExpire, Supplier<CompletableFuture<T>> supplier);
/** /**

View File

@@ -16,8 +16,8 @@ import org.redkale.cache.Cached;
import org.redkale.convert.json.JsonConvert; import org.redkale.convert.json.JsonConvert;
import org.redkale.net.sncp.Sncp; import org.redkale.net.sncp.Sncp;
import org.redkale.util.Environment; import org.redkale.util.Environment;
import org.redkale.util.TypeToken;
import org.redkale.util.MultiHashKey; import org.redkale.util.MultiHashKey;
import org.redkale.util.TypeToken;
/** /**
* *
@@ -102,9 +102,9 @@ public class CacheAction {
public <T> T get(Supplier<T> supplier, Object... args) { public <T> T get(Supplier<T> supplier, Object... args) {
if (async) { if (async) {
Supplier supplier0 = supplier; Supplier supplier0 = supplier;
return (T) manager.bothGetAsync(hash, dynKey.keyFor(args), resultType, nullable, localExpire, remoteExpire, supplier0); return (T) manager.bothGetSetAsync(hash, dynKey.keyFor(args), resultType, nullable, localExpire, remoteExpire, supplier0);
} else { } else {
return manager.bothGet(hash, dynKey.keyFor(args), resultType, nullable, localExpire, remoteExpire, supplier); return manager.bothGetSet(hash, dynKey.keyFor(args), resultType, nullable, localExpire, remoteExpire, supplier);
} }
} }

View File

@@ -157,8 +157,8 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> T localGet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier<T> supplier) { public <T> T localGetSet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier<T> supplier) {
return get(localSource::hget, localSource::hset, hash, key, type, nullable, expire, supplier); return getSet(localSource::hget, localSource::hset, hash, key, type, nullable, expire, supplier);
} }
/** /**
@@ -175,8 +175,8 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> CompletableFuture<T> localGetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier) { public <T> CompletableFuture<T> localGetSetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier) {
return getAsync(localSource::hgetAsync, localSource::hsetAsync, hash, key, type, nullable, expire, supplier); return getSetAsync(localSource::hgetAsync, localSource::hsetAsync, hash, key, type, nullable, expire, supplier);
} }
/** /**
@@ -263,8 +263,8 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> T remoteGet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier<T> supplier) { public <T> T remoteGetSet(final String hash, final String key, final Type type, boolean nullable, Duration expire, Supplier<T> supplier) {
return get(remoteSource::hget, remoteSource::hset, hash, key, type, nullable, expire, supplier); return getSet(remoteSource::hget, remoteSource::hset, hash, key, type, nullable, expire, supplier);
} }
/** /**
@@ -281,8 +281,8 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> CompletableFuture<T> remoteGetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier) { public <T> CompletableFuture<T> remoteGetSetAsync(String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier) {
return getAsync(remoteSource::hgetAsync, remoteSource::hsetAsync, hash, key, type, nullable, expire, supplier); return getSetAsync(remoteSource::hgetAsync, remoteSource::hsetAsync, hash, key, type, nullable, expire, supplier);
} }
/** /**
@@ -398,19 +398,19 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> T bothGet(final String hash, final String key, final Type type, boolean nullable, public <T> T bothGetSet(final String hash, final String key, final Type type, boolean nullable,
Duration localExpire, Duration remoteExpire, Supplier<T> supplier) { Duration localExpire, Duration remoteExpire, Supplier<T> supplier) {
if (!enabled) { if (!enabled) {
return supplier.get(); return supplier.get();
} }
if (localExpire == null) { //只有远程缓存 if (localExpire == null) { //只有远程缓存
Objects.requireNonNull(remoteExpire); Objects.requireNonNull(remoteExpire);
return remoteGet(hash, key, type, nullable, remoteExpire, supplier); return remoteGetSet(hash, key, type, nullable, remoteExpire, supplier);
} }
if (remoteExpire == null) { //只有本地缓存 if (remoteExpire == null) { //只有本地缓存
return localGet(hash, key, type, nullable, localExpire, supplier); return localGetSet(hash, key, type, nullable, localExpire, supplier);
} }
return get(this::bothGetCache, (h, k, t, v) -> { return getSet(this::bothGetCache, (h, k, t, v) -> {
localSource.hset(h, k, t, v); localSource.hset(h, k, t, v);
if (remoteSource != null) { if (remoteSource != null) {
remoteSource.hset(h, k, t, CacheValue.create(v.getValue(), remoteExpire)); remoteSource.hset(h, k, t, CacheValue.create(v.getValue(), remoteExpire));
@@ -433,19 +433,19 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值 * @return 数据值
*/ */
@Override @Override
public <T> CompletableFuture<T> bothGetAsync(String hash, String key, Type type, boolean nullable, public <T> CompletableFuture<T> bothGetSetAsync(String hash, String key, Type type, boolean nullable,
Duration localExpire, Duration remoteExpire, Supplier<CompletableFuture<T>> supplier) { Duration localExpire, Duration remoteExpire, Supplier<CompletableFuture<T>> supplier) {
if (!enabled) { if (!enabled) {
return supplier.get(); return supplier.get();
} }
if (localExpire == null) { //只有远程缓存 if (localExpire == null) { //只有远程缓存
Objects.requireNonNull(remoteExpire); Objects.requireNonNull(remoteExpire);
return remoteGetAsync(hash, key, type, nullable, remoteExpire, supplier); return remoteGetSetAsync(hash, key, type, nullable, remoteExpire, supplier);
} }
if (remoteExpire == null) { //只有本地缓存 if (remoteExpire == null) { //只有本地缓存
return localGetAsync(hash, key, type, nullable, localExpire, supplier); return localGetSetAsync(hash, key, type, nullable, localExpire, supplier);
} }
return getAsync(this::bothGetCacheAsync, (h, k, t, v) -> { return getSetAsync(this::bothGetCacheAsync, (h, k, t, v) -> {
localSource.hset(h, k, t, v); localSource.hset(h, k, t, v);
if (remoteSource != null) { if (remoteSource != null) {
return remoteSource.hsetAsync(h, k, t, CacheValue.create(v.getValue(), remoteExpire)); return remoteSource.hsetAsync(h, k, t, CacheValue.create(v.getValue(), remoteExpire));
@@ -559,7 +559,7 @@ public class CacheManagerService implements CacheManager, Service {
* *
* @return 数据值 * @return 数据值
*/ */
protected <T> T get(GetterFunc<CacheValue<T>> getter, SetterSyncFunc setter, protected <T> T getSet(GetterFunc<CacheValue<T>> getter, SetterSyncFunc setter,
String hash, String key, Type type, boolean nullable, Duration expire, Supplier<T> supplier) { String hash, String key, Type type, boolean nullable, Duration expire, Supplier<T> supplier) {
checkEnable(); checkEnable();
Objects.requireNonNull(expire); Objects.requireNonNull(expire);
@@ -604,7 +604,7 @@ public class CacheManagerService implements CacheManager, Service {
* *
* @return 数据值 * @return 数据值
*/ */
protected <T> CompletableFuture<T> getAsync(GetterFunc<CompletableFuture<CacheValue<T>>> getter, SetterAsyncFunc setter, protected <T> CompletableFuture<T> getSetAsync(GetterFunc<CompletableFuture<CacheValue<T>>> getter, SetterAsyncFunc setter,
String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier) { String hash, String key, Type type, boolean nullable, Duration expire, Supplier<CompletableFuture<T>> supplier) {
checkEnable(); checkEnable();
Objects.requireNonNull(supplier); Objects.requireNonNull(supplier);

View File

@@ -64,7 +64,7 @@ public class CacheManagerTest {
CountDownLatch cdl = new CountDownLatch(count); CountDownLatch cdl = new CountDownLatch(count);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
new Thread(() -> { new Thread(() -> {
manager.bothGet("ParallelBean", "name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); manager.bothGetSet("ParallelBean", "name", String.class, false, localExpire, remoteExpire, () -> bean.getName());
cdl.countDown(); cdl.countDown();
}).start(); }).start();
} }
@@ -72,14 +72,14 @@ public class CacheManagerTest {
} }
Assertions.assertEquals(1, ParallelBean.c1.get()); Assertions.assertEquals(1, ParallelBean.c1.get());
Utility.sleep(200); Utility.sleep(200);
manager.bothGet("ParallelBean", "name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); manager.bothGetSet("ParallelBean", "name", String.class, false, localExpire, remoteExpire, () -> bean.getName());
Assertions.assertEquals(1, ParallelBean.c1.get()); Assertions.assertEquals(1, ParallelBean.c1.get());
Utility.sleep(200); Utility.sleep(200);
{ {
CountDownLatch cdl = new CountDownLatch(count); CountDownLatch cdl = new CountDownLatch(count);
for (int i = 0; i < count; i++) { for (int i = 0; i < count; i++) {
new Thread(() -> { new Thread(() -> {
manager.bothGet("ParallelBean", "name", String.class, false, localExpire, remoteExpire, () -> bean.getName()); manager.bothGetSet("ParallelBean", "name", String.class, false, localExpire, remoteExpire, () -> bean.getName());
cdl.countDown(); cdl.countDown();
}).start(); }).start();
} }