Commit 64adc3bf by guiq

bug: [none] getEbookPdf4Wechat 接口优化

parent 2908e4a4
...@@ -404,6 +404,11 @@ ...@@ -404,6 +404,11 @@
<artifactId>nacos-spring-context</artifactId> <artifactId>nacos-spring-context</artifactId>
</dependency> </dependency>
<dependency>
<groupId>com.github.ben-manes.caffeine</groupId>
<artifactId>caffeine</artifactId>
</dependency>
</dependencies> </dependencies>
</project> </project>
package com.pcloud.common.utils.cache.caffeine;
import com.github.benmanes.caffeine.cache.Cache;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.collections4.MapUtils;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
public final class CaffeineUtils {
private CaffeineUtils() {
}
private static <K, V> void putAll(Cache<K, V> cache, Map<K, V> m) {
if (MapUtils.isNotEmpty(m)) {
cache.putAll(m);
}
}
public static <K, V> Map<K, V> getByCache(Cache<K, V> cache, List<K> keyList, Function<Set<K>, Map<K, V>> function) {
Map<K, V> result = new HashMap<>();
if (CollectionUtils.isEmpty(keyList)) {
return result;
}
Set<K> needQueryIds = new HashSet<>();
Map<K, V> mapInCache = new HashMap<>();
keyList.forEach((key) -> {
V cacheValue = cache.getIfPresent(key);
if (cacheValue != null) {
mapInCache.put(key, cacheValue);
} else {
needQueryIds.add(key);
}
});
if (CollectionUtils.isEmpty(needQueryIds)) {
return mapInCache;
}
Map<K, V> queryResult = function.apply(needQueryIds);
// 放入缓存
putAll(cache, queryResult);
// 汇总结果集合
result.putAll(mapInCache);
result.putAll(queryResult);
return result;
}
public static <K, V> Optional<V> getByCache(Cache<K, V> cache, K key, Supplier<V> supplier) {
if (key == null) {
return Optional.empty();
}
V cacheValue = cache.getIfPresent(key);
if (cacheValue != null) {
return Optional.of(cacheValue);
}
V queryResult = supplier.get();
if (queryResult != null) {
// 放入缓存
cache.put(key, queryResult);
}
return Optional.ofNullable(queryResult);
}
}
package com.pcloud.common.utils.exceptioin;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.function.Failable;
import org.apache.commons.lang3.function.FailableRunnable;
import org.apache.commons.lang3.function.FailableSupplier;
import java.util.function.Consumer;
/**
* 异常处理工具类
*/
@Slf4j
public final class ExceptionUtils {
private ExceptionUtils() {
}
/**
* 出现错误时,返回默认值
*
* @param supplier (可能抛出异常的)带返回值的操作
* @param onError 错误时的处理方法
* @param defaultValue 错误时返回的默认值
* @param <T> 类型
* @return 成功返回执行结果,失败返回默认值
*/
public static <T> T defaultWhenException(final FailableSupplier<T, ? extends Exception> supplier, final Consumer<Exception> onError, final T defaultValue) {
try {
return Failable.get(supplier);
} catch (final Exception e) {
onError.accept(e);
return defaultValue;
}
}
/**
* 出现错误时,返回默认值, 默认打印warn日志
*
* @param supplier (可能抛出异常的)带返回值的操作
* @param defaultValue 错误时返回的默认值
* @param <T> 类型
* @return 成功返回执行结果,失败返回默认值
*/
public static <T> T defaultWhenException(final FailableSupplier<T, ? extends Exception> supplier, final T defaultValue) {
try {
return Failable.get(supplier);
} catch (final Exception e) {
log.warn("[{}] 操作失败! ERR:{}", Thread.currentThread().getStackTrace()[2].getMethodName(), e.getMessage(), e);
return defaultValue;
}
}
/**
* 出现错误时,执行指定方法
*
* @param runnable (可能抛出异常的)无返回值的操作
* @param onError 错误时无返回值的处理方法
*/
public static void runWhenException(final FailableRunnable<? extends Exception> runnable, final Consumer<Exception> onError) {
try {
Failable.run(runnable);
} catch (final Exception e) {
onError.accept(e);
}
}
/**
* 出现错误时,忽略异常,打印warn日志
*
* @param runnable (可能抛出异常的)无返回值的操作
*/
public static void runWhenException(final FailableRunnable<? extends Exception> runnable) {
try {
Failable.run(runnable);
} catch (final Exception e) {
log.warn("[{}] 操作失败! ERR:{}", Thread.currentThread().getStackTrace()[2].getMethodName(), e.getMessage(), e);
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment