Commit 56b3702c authored by Alok Joshi's avatar Alok Joshi
Browse files

Merge branch 'elastic_cache' into 'master'

adds ability use own  ElasticCredentialsCache impl

See merge request !36
parents e8acaa9c 3ff69e2e
Pipeline #15411 passed with stages
in 9 minutes and 23 seconds
......@@ -59,6 +59,7 @@ The following software have components provided under the terms of this license:
- Jackson datatype: JSR310 (from http://wiki.fasterxml.com/JacksonModuleJSR310)
- Jackson-annotations (from http://github.com/FasterXML/jackson)
- Jackson-core (from https://github.com/FasterXML/jackson-core)
- Jackson-dataformat-Smile (from http://github.com/FasterXML/jackson-dataformat-smile)
- Jackson-dataformat-XML (from http://wiki.fasterxml.com/JacksonExtensionXmlDataBinding)
- Jackson-dataformat-YAML (from https://github.com/FasterXML/jackson)
- Jackson-datatype-Joda (from http://wiki.fasterxml.com/JacksonModuleJoda)
......
......@@ -15,13 +15,12 @@
~ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.opengroup.osdu</groupId>
<artifactId>core-lib-azure</artifactId>
<packaging>jar</packaging>
<version>0.0.40</version>
<version>0.0.41</version>
<name>core-lib-azure</name>
<properties>
......@@ -204,7 +203,6 @@
</dependency>
<!-- Other dependencies -->
<dependency>
<groupId>javax.inject</groupId>
......
......@@ -14,86 +14,28 @@
package org.opengroup.osdu.azure.cache;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.core.common.model.search.ClusterSettings;
import org.opengroup.osdu.core.common.provider.interfaces.IElasticCredentialsCache;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
import javax.inject.Named;
/**
* Elastic credential cache used by the Azure implementation of OSDU.
* abstract class for Elastic credential cache.
*/
@Component
@Lazy
public class ElasticCredentialsCache implements IElasticCredentialsCache<String, ClusterSettings> {
/**
* Underlying cache.
*/
private VmCache<String, ClusterSettings> cache;
/**
* @param cacheExpirationMinutes The cache expiration time, in minutes.
* @param maxCachedObjectEntries The max number of objects that can be in the cache.
*/
public ElasticCredentialsCache(
@Named("ELASTIC_CACHE_EXPIRATION") final Integer cacheExpirationMinutes,
@Named("MAX_CACHE_VALUE_SIZE") final Integer maxCachedObjectEntries) {
cache = new VmCache<>(cacheExpirationMinutes * 60, maxCachedObjectEntries);
}
/**
* @param s Key of item to insert.
* @param o The data to insert.
*/
@Override
public void put(final String s, final ClusterSettings o) {
cache.put(s, o);
}
/**
* @param s The cache key.
* @return The data cached with that key.
*/
@Override
public ClusterSettings get(final String s) {
return cache.get(s);
}
/**
* Delete an item from the cache.
*
* @param s The key to use for the delete operation.
*/
@Override
public void delete(final String s) {
cache.delete(s);
}
/**
* Clear all items from the cache.
*/
@Override
public void clearAll() {
cache.clearAll();
}
/**
* @param key cache key
* @return true if found in cache
*/
public boolean containsKey(final String key) {
return this.get(key) != null;
}
/**
* @param partitionId the tenant for which the request should be cached for.
* @return cache key for the tenant.
*/
public String getCacheKey(final String partitionId) {
return String.format("%s-clusterSettings", partitionId);
}
public abstract class ElasticCredentialsCache implements IElasticCredentialsCache<String, ClusterSettings> {
/**
* @param key cache key
* @return true if found in cache
*/
public boolean containsKey(final String key) {
return get(key) != null;
}
/**
* @param partitionId the tenant for which the request should be cached for.
* @return cache key for the tenant.
*/
public String getCacheKey(final String partitionId) {
return String.format("%s-clusterSettings", partitionId);
}
}
// Copyright © Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package org.opengroup.osdu.azure.cache;
import javax.inject.Named;
import org.opengroup.osdu.core.common.cache.VmCache;
import org.opengroup.osdu.core.common.model.search.ClusterSettings;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Component;
/**
* Elastic credential cache used by the Azure implementation of OSDU.
*/
@Component("clusterSettingsCache")
@Lazy
@ConditionalOnProperty(value = "cache.provider", havingValue = "vm", matchIfMissing = true)
public class ElasticCredentialsCacheImpl extends ElasticCredentialsCache {
/**
* Underlying cache.
*/
private VmCache<String, ClusterSettings> cache;
/**
* @param cacheExpirationMinutes The cache expiration time, in minutes.
* @param maxCachedObjectEntries The max number of objects that can be in the cache.
*/
public ElasticCredentialsCacheImpl(
@Named("ELASTIC_CACHE_EXPIRATION") final Integer cacheExpirationMinutes,
@Named("MAX_CACHE_VALUE_SIZE") final Integer maxCachedObjectEntries) {
cache = new VmCache<>(cacheExpirationMinutes * 60, maxCachedObjectEntries);
}
/**
* @param s Key of item to insert.
* @param o The data to insert.
*/
@Override
public void put(final String s, final ClusterSettings o) {
cache.put(s, o);
}
/**
* @param s The cache key.
* @return The data cached with that key.
*/
@Override
public ClusterSettings get(final String s) {
return cache.get(s);
}
/**
* Delete an item from the cache.
*
* @param s The key to use for the delete operation.
*/
@Override
public void delete(final String s) {
cache.delete(s);
}
/**
* Clear all items from the cache.
*/
@Override
public void clearAll() {
cache.clearAll();
}
}
......@@ -21,6 +21,7 @@ import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.opengroup.osdu.azure.cache.ElasticCredentialsCache;
import org.opengroup.osdu.azure.cache.ElasticCredentialsCacheImpl;
import org.opengroup.osdu.core.common.logging.JaxRsDpsLog;
import org.opengroup.osdu.core.common.model.search.ClusterSettings;
import org.opengroup.osdu.core.common.model.tenant.TenantInfo;
......@@ -46,7 +47,7 @@ class ElasticClusterSettingsServiceTest {
private JaxRsDpsLog log;
@Spy
private ElasticCredentialsCache cache = new ElasticCredentialsCache(10000, 10000);
private ElasticCredentialsCache cache = new ElasticCredentialsCacheImpl(10000, 10000);
@InjectMocks
private ElasticClusterSettingsService esSettingsService;
......
......@@ -16,13 +16,14 @@ package org.opengroup.osdu.elastic.dependencies;
import org.junit.jupiter.api.Test;
import org.opengroup.osdu.azure.cache.ElasticCredentialsCache;
import org.opengroup.osdu.azure.cache.ElasticCredentialsCacheImpl;
import org.opengroup.osdu.core.common.model.search.ClusterSettings;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
class ElasticCredentialsCacheTest {
private ElasticCredentialsCache cache = new ElasticCredentialsCache(10000, 10000);
private ElasticCredentialsCache cache = new ElasticCredentialsCacheImpl(10000, 10000);
@Test
void put_thenGet_works() {
......
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