Commit caf297c8 authored by Yifei Xu's avatar Yifei Xu
Browse files

Modify GroupCache.java and DmsRegistrationCache.java to allow "DISABLE_CACHE".

parent 0b72c410
......@@ -78,7 +78,7 @@ public class DatasetDmsServiceMapImpl implements IDatasetDmsServiceMap {
protected DmsRegistrations getServicesInfoFromCacheOrDynamo(DpsHeaders headers) {
String cacheKey = DmsRegistrationCache.getCacheKey(headers);
DmsRegistrations dmsRegistrations = this.cache.get(cacheKey);
DmsRegistrations dmsRegistrations = (DmsRegistrations) this.cache.get(cacheKey);
if (dmsRegistrations == null) {
try {
......
......@@ -14,6 +14,13 @@
package org.opengroup.osdu.dataset.provider.aws.cache;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.opengroup.osdu.core.aws.cache.DummyCache;
import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider;
import org.opengroup.osdu.core.aws.ssm.K8sParameterNotFoundException;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.core.common.model.entitlements.Groups;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.cache.RedisCache;
......@@ -22,10 +29,37 @@ import org.opengroup.osdu.dataset.provider.aws.model.DmsRegistrations;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class DmsRegistrationCache extends RedisCache<String, DmsRegistrations> {
public DmsRegistrationCache(@Value("${aws.elasticache.cluster.endpoint}") final String REDIS_HOST, @Value("${aws.elasticache.cluster.port}") final String REDIS_PORT, @Value("${aws.elasticache.cluster.key}") final String REDIS_KEY) {
super(REDIS_HOST, Integer.parseInt(REDIS_PORT), REDIS_KEY, 300, String.class, DmsRegistrations.class);
public class DmsRegistrationCache<K, V> implements ICache<K, V> {
@Value("${aws.elasticache.cluster.endpoint:null}")
String REDIS_SEARCH_HOST;
@Value("${aws.elasticache.cluster.port:null}")
String REDIS_SEARCH_PORT;
@Value("${aws.elasticache.cluster.key:null}")
String REDIS_SEARCH_KEY;
private ICache cache;
public DmsRegistrationCache() throws K8sParameterNotFoundException, JsonProcessingException {
K8sLocalParameterProvider provider = new K8sLocalParameterProvider();
if (provider.getLocalMode()) {
if (Boolean.parseBoolean(System.getenv("DISABLE_CACHE"))) {
this.cache = new DummyCache();
}
this.cache = new VmCache<>(60, 10);
} else {
String host = provider.getParameterAsStringOrDefault("CACHE_CLUSTER_ENDPOINT", REDIS_SEARCH_HOST);
int port = Integer.parseInt(provider.getParameterAsStringOrDefault("CACHE_CLUSTER_PORT", REDIS_SEARCH_PORT));
Map<String, String> credential = provider.getCredentialsAsMap("CACHE_CLUSTER_KEY");
String password;
if (credential != null) {
password = credential.get("token");
} else {
password = REDIS_SEARCH_KEY;
}
this.cache = new RedisCache(host, port, password, 60, String.class, Groups.class);
}
}
public static String getCacheKey(DpsHeaders headers) {
......@@ -34,4 +68,23 @@ public class DmsRegistrationCache extends RedisCache<String, DmsRegistrations> {
return Crc32c.hashToBase64EncodedString(key);
}
@Override
public void put(K k, V o) {
this.cache.put(k, o);
}
@Override
public V get(K k) {
return (V) this.cache.get(k);
}
@Override
public void delete(K k) {
this.cache.delete(k);
}
@Override
public void clearAll() {
this.cache.clearAll();
}
}
......@@ -14,17 +14,52 @@
package org.opengroup.osdu.dataset.provider.aws.cache;
import com.fasterxml.jackson.core.JsonProcessingException;
import org.opengroup.osdu.core.aws.ssm.K8sLocalParameterProvider;
import org.opengroup.osdu.core.aws.ssm.K8sParameterNotFoundException;
import org.opengroup.osdu.core.common.cache.ICache;
import org.opengroup.osdu.core.common.cache.RedisCache;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.core.common.model.http.DpsHeaders;
import org.opengroup.osdu.core.common.model.entitlements.Groups;
import org.opengroup.osdu.core.common.cache.RedisCache;
import org.opengroup.osdu.core.common.util.Crc32c;
import org.opengroup.osdu.core.aws.cache.DummyCache;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;
import java.util.Map;
@Component
public class GroupCache extends RedisCache<String, Groups> {
public GroupCache(@Value("${aws.elasticache.cluster.endpoint}") final String REDIS_GROUP_HOST, @Value("${aws.elasticache.cluster.port}") final String REDIS_GROUP_PORT, @Value("${aws.elasticache.cluster.key}") final String REDIS_GROUP_KEY ) {
super(REDIS_GROUP_HOST, Integer.parseInt(REDIS_GROUP_PORT), REDIS_GROUP_KEY, 60, String.class, Groups.class);
@Primary
public class GroupCache<K, V> implements ICache<K, V> {
@Value("${aws.elasticache.cluster.endpoint:null}")
String REDIS_SEARCH_HOST;
@Value("${aws.elasticache.cluster.port:null}")
String REDIS_SEARCH_PORT;
@Value("${aws.elasticache.cluster.key:null}")
String REDIS_SEARCH_KEY;
private ICache cache;
public GroupCache() throws K8sParameterNotFoundException, JsonProcessingException {
K8sLocalParameterProvider provider = new K8sLocalParameterProvider();
if (provider.getLocalMode()) {
if (Boolean.parseBoolean(System.getenv("DISABLE_CACHE"))) {
this.cache = new DummyCache();
}
this.cache = new VmCache<>(60, 10);
} else {
String host = provider.getParameterAsStringOrDefault("CACHE_CLUSTER_ENDPOINT", REDIS_SEARCH_HOST);
int port = Integer.parseInt(provider.getParameterAsStringOrDefault("CACHE_CLUSTER_PORT", REDIS_SEARCH_PORT));
Map<String, String> credential = provider.getCredentialsAsMap("CACHE_CLUSTER_KEY");
String password;
if (credential != null) {
password = credential.get("token");
} else {
password = REDIS_SEARCH_KEY;
}
this.cache = new RedisCache(host, port, password, 60, String.class, Groups.class);
}
}
public static String getGroupCacheKey(DpsHeaders headers) {
......@@ -37,4 +72,24 @@ public class GroupCache extends RedisCache<String, Groups> {
String key = String.format("entitlement-groups:data-partition:%s", dataPartitionId);
return Crc32c.hashToBase64EncodedString(key);
}
}
@Override
public void put(K k, V o) {
this.cache.put(k, o);
}
@Override
public V get(K k) {
return (V) this.cache.get(k);
}
@Override
public void delete(K k) {
this.cache.delete(k);
}
@Override
public void clearAll() {
this.cache.clearAll();
}
}
\ No newline at end of file
......@@ -37,9 +37,9 @@ aws.region=${AWS_REGION}
# S3_DATA_BUCKET=""
## AWS ElastiCache configuration
aws.elasticache.cluster.endpoint=${CACHE_CLUSTER_ENDPOINT}
aws.elasticache.cluster.port=${CACHE_CLUSTER_PORT}
aws.elasticache.cluster.key=${CACHE_CLUSTER_KEY}
aws.elasticache.cluster.endpoint=${CACHE_CLUSTER_ENDPOINT:null}
aws.elasticache.cluster.port=${CACHE_CLUSTER_PORT:null}
aws.elasticache.cluster.key=${CACHE_CLUSTER_KEY:null}
# aws.ssm=${SSM_ENABLED}
aws.parameter.prefix=/osdu/${ENVIRONMENT}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment