Commit 9b383806 authored by Aliaksei Darafeyeu's avatar Aliaksei Darafeyeu
Browse files

adds ability use own ElasticCredentialsCache impl

parent e8acaa9c
......@@ -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