Skip to content

Commit

Permalink
Merge pull request #935 from gisaia/fix/nocacheManager
Browse files Browse the repository at this point in the history
Adding a LocalCacheFactory
  • Loading branch information
alainbodiguel authored Jan 15, 2024
2 parents 736693a + 599ed11 commit 4179cb5
Show file tree
Hide file tree
Showing 23 changed files with 828 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,19 +22,9 @@
public interface BaseCacheManager {
Object getObject(String key, String ref);

void putObject(String key, String ref, Object col, long timeout);

void putObject(String key, String ref, Object col);

void removeObject(String key, String ref);

void putDecision(String p, Boolean decision);

Boolean getDecision(String p);

void removeDecision(String p);

void putPermission(String token, String p);

String getPermission(String token);

void removePermission(String token);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,6 @@

import java.util.concurrent.TimeUnit;

/**
* This cache holds a replicated map (named 'collections') for storing the collection references
* and one replicated map per collection (named '<collection name>') for storing the elastic types.
*/
public class BaseHazelcastCacheManager implements BaseCacheManager {
Logger LOGGER = LoggerFactory.getLogger(BaseHazelcastCacheManager.class);
final private Config hzConfig;
Expand Down Expand Up @@ -69,12 +65,13 @@ public Object getObject(String key, String ref) {
init();
c = this.instance.getReplicatedMap(key).get(ref);
}
LOGGER.debug("Returning object '" + ref + "' from cache (key=" + key + ") with value " + (c == null ? "null" : c.toString()));
LOGGER.debug(String.format("Returning {'%s':{'%s': '%s'}}", key, ref, (c == null ? "null" : c.toString())));
return c;
}

private void putObject(String key, String ref, Object o, int timeout) {
LOGGER.debug("Inserting object '" + ref + "' with value '" + o.toString() + "' in cache (key=" + key +")");
@Override
public void putObject(String key, String ref, Object o, long timeout) {
LOGGER.debug(String.format("Inserting {'%s':{'%s': '%s'}}", key, ref, o));
try {
this.instance.getReplicatedMap(key).put(ref, o, timeout, TimeUnit.SECONDS);
} catch (HazelcastInstanceNotActiveException e) { // recover from unexpected shutdown
Expand All @@ -90,45 +87,12 @@ public void putObject(String key, String ref, Object o) {

@Override
public void removeObject(String key, String ref) {
LOGGER.debug("Clearing object '" + ref + "' from cache (key=" + key +")");
LOGGER.debug(String.format("Clearing {'%s':{'%s': ''}}", key, ref));
try {
this.instance.getReplicatedMap(key).remove(ref);
} catch (HazelcastInstanceNotActiveException e) { // recover from unexpected shutdown
init();
this.instance.getReplicatedMap(key).remove(ref);
}
}

@Override
public void putDecision(String path, Boolean decision) {
// we don't want IAM decisions to be cached more than 1 minute
putObject("decisions", path, decision, cacheTimeout > 60 ? 60 : cacheTimeout);
}

@Override
public Boolean getDecision(String path) {
return (Boolean) getObject("decisions", path);
}

@Override
public void removeDecision(String path) {
removeObject("decisions", path);
}

@Override
public void putPermission(String token, String rpt) {
// we don't want IAM permissions to be cached more than 1 minute
putObject("permissions", token, rpt, cacheTimeout > 60 ? 60 : cacheTimeout);
}

@Override
public String getPermission(String token) {
return (String) getObject("permissions", token);
}

@Override
public void removePermission(String token) {
removeObject("permissions", token);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to Gisaïa under one or more contributor
* license agreements. See the NOTICE.txt file distributed with
* this work for additional information regarding copyright
* ownership. Gisaïa licenses this file to you 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 io.arlas.commons.cache;

import io.arlas.commons.config.ArlasConfiguration;

public class BaseLocalCacheFactory extends CacheFactory {
private final BaseCacheManager cacheManager;

public BaseLocalCacheFactory(ArlasConfiguration configuration) {
super(configuration);
this.cacheManager = new BaseLocalCacheManager(configuration.arlasCacheTimeout);
}

@Override
public BaseCacheManager getCacheManager() {
return this.cacheManager;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Licensed to Gisaïa under one or more contributor
* license agreements. See the NOTICE.txt file distributed with
* this work for additional information regarding copyright
* ownership. Gisaïa licenses this file to you 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 io.arlas.commons.cache;

import io.arlas.commons.utils.SelfExpiringHashMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
* This is a local cache implementation (no network replication)
*/
public class BaseLocalCacheManager implements BaseCacheManager {
Logger LOGGER = LoggerFactory.getLogger(BaseLocalCacheManager.class);
final protected long cacheTimeout;
final protected Map<String, SelfExpiringHashMap<String, Object>> cache;

public BaseLocalCacheManager(int cacheTimeout) {
this.cacheTimeout = cacheTimeout;
this.cache = new ConcurrentHashMap<>();
}

@Override
public Object getObject(String key, String ref) {
Object c = this.cache.computeIfAbsent(key, k -> new SelfExpiringHashMap<>()).get(ref);
LOGGER.debug(String.format("Returning {'%s':{'%s': '%s'}}", key, ref, (c == null ? "null" : c.toString())));
return c;
}

@Override
public void putObject(String key, String ref, Object o, long timeout) {
LOGGER.debug(String.format("Inserting {'%s':{'%s': '%s'}}", key, ref, o));
this.cache.computeIfAbsent(key, k -> new SelfExpiringHashMap<>()).put(ref, o, timeout * 1000L);
}

@Override
public void putObject(String key, String ref, Object o) {
putObject(key, ref, o, this.cacheTimeout);
}

@Override
public void removeObject(String key, String ref) {
LOGGER.debug(String.format("Clearing {'%s':{'%s': ''}}", key, ref));
this.cache.computeIfAbsent(key, k -> new SelfExpiringHashMap<>()).remove(ref);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Licensed to Gisaïa under one or more contributor
* license agreements. See the NOTICE.txt file distributed with
* this work for additional information regarding copyright
* ownership. Gisaïa licenses this file to you 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 io.arlas.commons.cache;

import io.arlas.commons.config.ArlasConfiguration;

public class NoBaseCacheFactory extends CacheFactory {
private final BaseCacheManager cacheManager;

public NoBaseCacheFactory(ArlasConfiguration configuration) {
super(configuration);
this.cacheManager = new NoBaseCacheManager(configuration.arlasCacheTimeout);
}

@Override
public BaseCacheManager getCacheManager() {
return this.cacheManager;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@
package io.arlas.commons.cache;

/**
* This cache holds a replicated map (named 'collections') for storing the collection references
* and one replicated map per collection (named '<collection name>') for storing the elastic types.
* This is a No Cache implementation (does nothing)
*/
public class NoCacheManager implements BaseCacheManager {
public NoCacheManager(int cacheTimeout) {
public class NoBaseCacheManager implements BaseCacheManager {
public NoBaseCacheManager(int cacheTimeout) {
}

@Override
Expand All @@ -33,37 +32,14 @@ public Object getObject(String key, String ref) {
}

@Override
public void putObject(String key, String ref, Object o) {
}

@Override
public void removeObject(String key, String ref) {
}

@Override
public void putDecision(String path, Boolean decision) {
}

@Override
public Boolean getDecision(String path) {
return null;
}

@Override
public void removeDecision(String path) {
public void putObject(String key, String ref, Object col, long timeout) {
}

@Override
public void putPermission(String token, String rpt) {
}

@Override
public String getPermission(String token) {
return null;
public void putObject(String key, String ref, Object o) {
}

@Override
public void removePermission(String token) {
public void removeObject(String key, String ref) {
}

}
Loading

0 comments on commit 4179cb5

Please sign in to comment.