ThrowSupplier

This commit is contained in:
redkale
2023-12-21 19:52:15 +08:00
parent 95b08e1db0
commit cfbf4bbe85
11 changed files with 357 additions and 110 deletions

View File

@@ -6,7 +6,7 @@ package org.redkale.cache;
import java.lang.reflect.Type;
import java.time.Duration;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import org.redkale.util.ThrowSupplier;
/**
* 缓存管理器
@@ -58,7 +58,7 @@ public interface CacheManager {
*
* @return 数据值
*/
public <T> T localGetSet(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, ThrowSupplier<T> supplier);
/**
* 本地异步获取缓存数据, 过期返回null
@@ -73,7 +73,7 @@ public interface CacheManager {
*
* @return 数据值
*/
public <T> CompletableFuture<T> localGetSetAsync(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, ThrowSupplier<CompletableFuture<T>> supplier);
/**
* 本地缓存数据
@@ -172,7 +172,7 @@ public interface CacheManager {
* @return 数据值
*/
public <T> T remoteGetSet(final String hash, final String key, final Type type, boolean nullable,
Duration expire, Supplier<T> supplier);
Duration expire, ThrowSupplier<T> supplier);
/**
* 远程异步获取缓存数据, 过期返回null
@@ -188,7 +188,7 @@ public interface CacheManager {
* @return 数据值
*/
public <T> CompletableFuture<T> remoteGetSetAsync(String hash, String key, Type type, boolean nullable,
Duration expire, Supplier<CompletableFuture<T>> supplier);
Duration expire, ThrowSupplier<CompletableFuture<T>> supplier);
/**
* 远程缓存数据
@@ -326,7 +326,7 @@ public interface CacheManager {
* @return 数据值
*/
public <T> T bothGetSet(String hash, String key, Type type, boolean nullable,
Duration localExpire, Duration remoteExpire, Supplier<T> supplier);
Duration localExpire, Duration remoteExpire, ThrowSupplier<T> supplier);
/**
* 本地或远程异步获取缓存数据, 过期返回null
@@ -343,7 +343,7 @@ public interface CacheManager {
* @return 数据值
*/
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, ThrowSupplier<CompletableFuture<T>> supplier);
/**
* 本地和远程缓存数据

View File

@@ -8,7 +8,6 @@ import java.lang.reflect.Type;
import java.time.Duration;
import java.util.Objects;
import java.util.concurrent.CompletableFuture;
import java.util.function.Supplier;
import org.redkale.annotation.Nullable;
import org.redkale.annotation.Resource;
import org.redkale.cache.CacheManager;
@@ -16,6 +15,7 @@ import org.redkale.convert.json.JsonConvert;
import org.redkale.net.sncp.Sncp;
import org.redkale.util.Environment;
import org.redkale.util.MultiHashKey;
import org.redkale.util.ThrowSupplier;
import org.redkale.util.TypeToken;
/**
@@ -100,9 +100,9 @@ public class CacheAction {
this.remoteExpire = createDuration(cached.getRemoteExpire());
}
public <T> T get(Supplier<T> supplier, Object... args) {
public <T> T get(ThrowSupplier<T> supplier, Object... args) {
if (async) {
Supplier supplier0 = supplier;
ThrowSupplier supplier0 = supplier;
return (T) manager.bothGetSetAsync(hash, dynKey.keyFor(args), resultType, nullable, localExpire, remoteExpire, supplier0);
} else {
return manager.bothGetSet(hash, dynKey.keyFor(args), resultType, nullable, localExpire, remoteExpire, supplier);

View File

@@ -14,7 +14,6 @@ import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Function;
import java.util.function.Supplier;
import org.redkale.annotation.AutoLoad;
import org.redkale.annotation.Component;
import org.redkale.annotation.Nullable;
@@ -28,6 +27,7 @@ import org.redkale.source.CacheMemorySource;
import org.redkale.source.CacheSource;
import org.redkale.util.AnyValue;
import org.redkale.util.RedkaleException;
import org.redkale.util.ThrowSupplier;
import org.redkale.util.TypeToken;
import org.redkale.util.Utility;
@@ -157,7 +157,7 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值
*/
@Override
public <T> T localGetSet(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, ThrowSupplier<T> supplier) {
return getSet(localSource::hget, localSource::hset, hash, key, type, nullable, expire, supplier);
}
@@ -175,7 +175,7 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值
*/
@Override
public <T> CompletableFuture<T> localGetSetAsync(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, ThrowSupplier<CompletableFuture<T>> supplier) {
return getSetAsync(localSource::hgetAsync, localSource::hsetAsync, hash, key, type, nullable, expire, supplier);
}
@@ -263,7 +263,7 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值
*/
@Override
public <T> T remoteGetSet(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, ThrowSupplier<T> supplier) {
return getSet(remoteSource::hget, remoteSource::hset, hash, key, type, nullable, expire, supplier);
}
@@ -281,7 +281,7 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值
*/
@Override
public <T> CompletableFuture<T> remoteGetSetAsync(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, ThrowSupplier<CompletableFuture<T>> supplier) {
return getSetAsync(remoteSource::hgetAsync, remoteSource::hsetAsync, hash, key, type, nullable, expire, supplier);
}
@@ -399,9 +399,15 @@ public class CacheManagerService implements CacheManager, Service {
*/
@Override
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, ThrowSupplier<T> supplier) {
if (!enabled) {
return supplier.get();
try {
return supplier.get();
} catch (RuntimeException e) {
throw e;
} catch (Throwable t) {
throw new RedkaleException(t);
}
}
if (localExpire == null) { //只有远程缓存
Objects.requireNonNull(remoteExpire);
@@ -434,9 +440,13 @@ public class CacheManagerService implements CacheManager, Service {
*/
@Override
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, ThrowSupplier<CompletableFuture<T>> supplier) {
if (!enabled) {
return supplier.get();
try {
return supplier.get();
} catch (Throwable t) {
return CompletableFuture.failedFuture(t);
}
}
if (localExpire == null) { //只有远程缓存
Objects.requireNonNull(remoteExpire);
@@ -560,7 +570,7 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值
*/
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, ThrowSupplier<T> supplier) {
checkEnable();
Objects.requireNonNull(expire);
Objects.requireNonNull(supplier);
@@ -574,7 +584,14 @@ public class CacheManagerService implements CacheManager, Service {
if (CacheValue.isValid(oldCacheVal)) {
return oldCacheVal;
}
CacheValue<T> newCacheVal = toCacheSupplier(nullable, expire, supplier).get();
CacheValue<T> newCacheVal;
try {
newCacheVal = toCacheSupplier(nullable, expire, supplier).get();
} catch (RuntimeException e) {
throw e;
} catch (Throwable t) {
throw new RedkaleException(t);
}
if (CacheValue.isValid(newCacheVal)) {
setter.set(hash, key, cacheType, newCacheVal);
}
@@ -605,7 +622,7 @@ public class CacheManagerService implements CacheManager, Service {
* @return 数据值
*/
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, ThrowSupplier<CompletableFuture<T>> supplier) {
checkEnable();
Objects.requireNonNull(supplier);
final Type cacheType = loadCacheType(type);
@@ -722,7 +739,7 @@ public class CacheManagerService implements CacheManager, Service {
*
* @return CacheValue函数
*/
protected <T> Supplier<CacheValue<T>> toCacheSupplier(boolean nullable, Duration expire, Supplier<T> supplier) {
protected <T> ThrowSupplier<CacheValue<T>> toCacheSupplier(boolean nullable, Duration expire, ThrowSupplier<T> supplier) {
return () -> toCacheValue(nullable, expire, supplier.get());
}

View File

@@ -0,0 +1,26 @@
/*
*
*/
package org.redkale.util;
/**
* 抛异常版的Supplier
*
* <p>
* 详情见: https://redkale.org
*
* @author zhangjx
*
* @since 2.8.0
*/
@FunctionalInterface
public interface ThrowSupplier<T> {
/**
* Gets a result.
*
* @return a result
*/
T get() throws Throwable;
}