Skip to content

Commit

Permalink
fixes networknt#1697 check cache configuration first before creating …
Browse files Browse the repository at this point in the history
…CacheManager instance (networknt#1698)
  • Loading branch information
stevehu authored Apr 7, 2023
1 parent aa7f350 commit cf052b5
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
25 changes: 15 additions & 10 deletions cache-manager/src/main/java/com/networknt/cache/CacheManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<CacheItem> 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<Object, Object> getCache(String cacheName);
void removeCache(String cacheName);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);


Expand Down

0 comments on commit cf052b5

Please sign in to comment.