优化CacheUtil

This commit is contained in:
smallchill 2024-10-23 21:08:20 +08:00
parent 4c9546af8c
commit 791a5f2596

View File

@ -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;
}
}
/**