-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #935 from gisaia/fix/nocacheManager
Adding a LocalCacheFactory
- Loading branch information
Showing
23 changed files
with
828 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
arlas-commons/src/main/java/io/arlas/commons/cache/BaseLocalCacheFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
arlas-commons/src/main/java/io/arlas/commons/cache/BaseLocalCacheManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
36 changes: 36 additions & 0 deletions
36
arlas-commons/src/main/java/io/arlas/commons/cache/NoBaseCacheFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.