Skip to content

Commit

Permalink
Extract memory cache and register with cache manager
Browse files Browse the repository at this point in the history
  • Loading branch information
ErgoSoftUK committed May 18, 2017
1 parent b1c9de9 commit 7d096ba
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven-compiler-plugin.version>3.1</maven-compiler-plugin.version>

<endeavour-common.version>2.78</endeavour-common.version>
<endeavour-common.version>2.81</endeavour-common.version>
<hibernate.version>5.2.5.Final</hibernate.version>
<hikari.version>2.5.1</hikari.version>
<junit.version>4.12</junit.version>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.endeavourhealth.common.config;

import org.endeavourhealth.common.cache.ICache;
import org.endeavourhealth.common.config.models.ConfigCacheEntry;

import java.util.HashMap;
import java.util.Map;

public class ConfigCache implements ICache {
private static Map<String, ConfigCacheEntry> _configCache = new HashMap<>();

@Override
public String getName() {
return "ConfigCache";
}

@Override
public long getSize() {
return _configCache.size();
}

@Override
public void clearCache() {
_configCache.clear();
}


public ConfigCacheEntry get(String configId, String appId) {
return _configCache.get(configId + appId);
}

public void put(String configId, String appId, ConfigCacheEntry cacheEntry) {
_configCache.put(configId + appId, cacheEntry);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ch.qos.logback.classic.LoggerContext;
import ch.qos.logback.classic.joran.JoranConfigurator;
import com.fasterxml.jackson.databind.JsonNode;
import org.endeavourhealth.common.cache.CacheManager;
import org.endeavourhealth.common.cache.ObjectMapperPool;
import org.endeavourhealth.common.config.db.DatabaseLayer;
import org.endeavourhealth.common.config.db.JdbcLayer;
Expand All @@ -12,8 +13,6 @@

import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

public class ConfigManager {
Expand All @@ -24,14 +23,16 @@ public class ConfigManager {

static DatabaseLayer _databaseLayer;
private static String _appId;
private static Map<String, ConfigCacheEntry> _configCache = new HashMap<>();
private static ConfigCache _configCache;

static { //runs when the main class is loaded.
LOG.info("Config manager created");
System.setProperty("org.jboss.logging.provider", "slf4j");
LOG.info("Jboss logging switched to slf4j");

_databaseLayer = new JdbcLayer();
_configCache = new ConfigCache();
CacheManager.registerCache(_configCache);
}

public synchronized static void Initialize(String appId) throws ConfigManagerException {
Expand Down Expand Up @@ -80,27 +81,28 @@ public static String getConfiguration(String configId) {

public synchronized static String getConfiguration(String configId, String appIdParam) {
// Try to get from cache
ConfigCacheEntry cacheEntry = _configCache.get(configId + appIdParam);
ConfigCacheEntry cacheEntry = _configCache.get(configId, appIdParam);
if (cacheEntry!=null && !cacheEntry.isExpired())
return cacheEntry.getConfigData();

cacheEntry = _configCache.get(configId + APP_GLOBAL);
// If no app specific result, look for a global setting
cacheEntry = _configCache.get(configId, APP_GLOBAL);
if (cacheEntry!=null && !cacheEntry.isExpired())
return cacheEntry.getConfigData();

// Failed, now try DB
String data = _databaseLayer.getConfiguration(configId, appIdParam);
if (data != null) {
cacheEntry = new ConfigCacheEntry(data);
_configCache.put(configId + appIdParam, cacheEntry);
_configCache.put(configId, appIdParam, cacheEntry);
return data;
}

// If no app specific result, look for a global setting
data = _databaseLayer.getConfiguration(configId, APP_GLOBAL);
if (data != null) {
cacheEntry = new ConfigCacheEntry(data);
_configCache.put(configId + APP_GLOBAL, cacheEntry);
_configCache.put(configId, APP_GLOBAL, cacheEntry);
return data;
}

Expand Down

0 comments on commit 7d096ba

Please sign in to comment.