From cf052b5777b7e021c852474ceb9d50f7ed6580c7 Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Thu, 6 Apr 2023 21:01:44 -0400 Subject: [PATCH] fixes #1697 check cache configuration first before creating CacheManager instance (#1698) --- .../com/networknt/cache/CacheManager.java | 25 +++++++++++-------- .../networknt/db/provider/DbProviderImpl.java | 4 +++ 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/cache-manager/src/main/java/com/networknt/cache/CacheManager.java b/cache-manager/src/main/java/com/networknt/cache/CacheManager.java index 533599d812..033d7231fb 100644 --- a/cache-manager/src/main/java/com/networknt/cache/CacheManager.java +++ b/cache-manager/src/main/java/com/networknt/cache/CacheManager.java @@ -5,23 +5,28 @@ import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.util.concurrent.TimeUnit; +import java.util.List; public interface CacheManager { Logger logger = LoggerFactory.getLogger(CacheManager.class); static CacheManager getInstance() { - CacheManager cacheManager = SingletonServiceFactory.getBean(CacheManager.class); - if (cacheManager == null) { - logger.warn("No cache manager configured in service.yml; default to CaffeineCacheManager"); - cacheManager = new CaffeineCacheManager(); - for(CacheItem cacheItem: CacheConfig.load().getCaches()) { - cacheManager.addCache(cacheItem.getCacheName(), cacheItem.getMaxSize(), cacheItem.getExpiryInMinutes()); + List caches = CacheConfig.load().getCaches(); + if(caches != null && caches.size() > 0) { + CacheManager cacheManager = SingletonServiceFactory.getBean(CacheManager.class); + if (cacheManager == null) { + logger.warn("No cache manager configured in service.yml; default to CaffeineCacheManager"); + cacheManager = new CaffeineCacheManager(); + for(CacheItem cacheItem: CacheConfig.load().getCaches()) { + cacheManager.addCache(cacheItem.getCacheName(), cacheItem.getMaxSize(), cacheItem.getExpiryInMinutes()); + } + SingletonServiceFactory.setBean(CacheManager.class.getName(), cacheManager); } - SingletonServiceFactory.setBean(CacheManager.class.getName(), cacheManager); + return cacheManager; + } else { + return null; } - return cacheManager; - } + void addCache(String cacheName, long maxSize, long expiryInMinutes); Cache getCache(String cacheName); void removeCache(String cacheName); diff --git a/db-provider/src/main/java/com/networknt/db/provider/DbProviderImpl.java b/db-provider/src/main/java/com/networknt/db/provider/DbProviderImpl.java index 61b704f861..2348c596e9 100644 --- a/db-provider/src/main/java/com/networknt/db/provider/DbProviderImpl.java +++ b/db-provider/src/main/java/com/networknt/db/provider/DbProviderImpl.java @@ -11,6 +11,10 @@ * @author Steve Hu */ public class DbProviderImpl implements DbProvider { + public static final String SQL_EXCEPTION = "ERR10017"; + public static final String GENERIC_EXCEPTION = "ERR10014"; + public static final String OBJECT_NOT_FOUND = "ERR11637"; + private static final Logger logger = LoggerFactory.getLogger(DbProviderImpl.class);