diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java index 2c08033ebafb9a2b4e5f2224da2a59153bd49d8c..c2bf190b18084b34162ea37b9148b30b57fea480 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/IndexCacheImpl.java @@ -14,27 +14,64 @@ package org.opengroup.osdu.indexer.aws.cache; +import com.fasterxml.jackson.core.JsonProcessingException; +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.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.provider.interfaces.IIndexCache; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Component; +import java.util.Map; + @Component public class IndexCacheImpl implements IIndexCache<String, Boolean>, AutoCloseable { - private RedisCache<String, Boolean> cache; - - public IndexCacheImpl(@Value("${aws.elasticache.cluster.endpoint}") final String REDIS_SEARCH_HOST, - @Value("${aws.elasticache.cluster.port}") final String REDIS_SEARCH_PORT, - @Value("${aws.elasticache.cluster.key}") final String REDIS_SEARCH_KEY, - @Value("${aws.elasticache.cluster.index.expiration}") final String INDEX_CACHE_EXPIRATION) { - cache = new RedisCache<>(REDIS_SEARCH_HOST, Integer.parseInt(REDIS_SEARCH_PORT), REDIS_SEARCH_KEY, - Integer.parseInt(INDEX_CACHE_EXPIRATION) * 60, String.class, Boolean.class); + private ICache<String, Boolean> cache; + private Boolean local; + @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; + @Value("${aws.elasticache.cluster.index.expiration}") + String INDEX_CACHE_EXPIRATION; + public IndexCacheImpl() throws K8sParameterNotFoundException, JsonProcessingException { + int expTimeSeconds = Integer.parseInt(INDEX_CACHE_EXPIRATION) * 60; + K8sLocalParameterProvider provider = new K8sLocalParameterProvider(); + if (provider.getLocalMode()){ + if (Boolean.parseBoolean(System.getenv("DISABLE_CACHE"))){ + cache = new DummyCache<>(); + }else{ + cache = new VmCache<String,Boolean>(expTimeSeconds, 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; + } + cache = new RedisCache<String, Boolean>(host, port, password, expTimeSeconds, String.class,Boolean.class); + } + local = cache instanceof AutoCloseable; } @Override public void close() throws Exception { - this.cache.close(); + if (local){ + + }else{ + ((AutoCloseable)this.cache).close(); + } + } @Override diff --git a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java index ca02bfaeb407a0f0c13ebe1b3d45432c60a4bfeb..d28f02f76d4900583f403d64c51bc1e3d6f17c8e 100644 --- a/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java +++ b/provider/indexer-aws/src/main/java/org/opengroup/osdu/indexer/aws/cache/SchemaCacheImpl.java @@ -29,13 +29,13 @@ import java.util.Map; @Component public class SchemaCacheImpl implements ISchemaCache<String, String>, AutoCloseable { - @Value("${aws.elasticache.cluster.endpoint}") + @Value("${aws.elasticache.cluster.endpoint:null}") String REDIS_SEARCH_HOST; - @Value("${aws.elasticache.cluster.port}") + @Value("${aws.elasticache.cluster.port:null}") String REDIS_SEARCH_PORT; - @Value("${aws.elasticache.cluster.key}") + @Value("${aws.elasticache.cluster.key:null}") String REDIS_SEARCH_KEY; - @Value("${aws.elasticache.cluster.schema.expiration}") + @Value("${aws.elasticache.cluster.schema.expiration:null}") String SCHEMA_CACHE_EXPIRATION; private ICache<String, String> cache; private Boolean local = false;