From 791a5f25966ab9317db8d71c00eed68b9fb4c657 Mon Sep 17 00:00:00 2001 From: smallchill Date: Wed, 23 Oct 2024 21:08:20 +0800 Subject: [PATCH] =?UTF-8?q?:zap:=20=E4=BC=98=E5=8C=96CacheUtil?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../core/tool/utils/CacheUtil.java | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java index e557fb2..49848c6 100644 --- a/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java +++ b/blade-core-tool/src/main/java/org/springblade/core/tool/utils/CacheUtil.java @@ -105,8 +105,26 @@ public class CacheUtil { if (Func.hasEmpty(cacheName, keyPrefix, key)) { return null; } - String cacheKey = keyPrefix.concat(String.valueOf(key)); - return getCache(cacheName).get(cacheKey, valueLoader); + + final String fullKey = keyPrefix.concat(String.valueOf(key)); + Cache cache = getCache(cacheName); + Cache.ValueWrapper valueWrapper = cache.get(fullKey); + + if (valueWrapper != null) { + return (T) valueWrapper.get(); + } + + try { + T value = valueLoader.call(); + if (value == null) { + return null; + } + cache.put(fullKey, value); + return value; + } catch (Exception ex) { + ex.printStackTrace(); + return null; + } } /**