diff --git a/cellbase-core/pom.xml b/cellbase-core/pom.xml
index 1232a17874..83e89593ba 100644
--- a/cellbase-core/pom.xml
+++ b/cellbase-core/pom.xml
@@ -23,7 +23,6 @@
org.opencb.biodata
biodata-tools
-
org.glassfish.jersey.core
jersey-client
@@ -32,7 +31,21 @@
org.slf4j
slf4j-log4j12
-
+
+ org.redisson
+ redisson
+ 3.5.3
+
+
+ com.esotericsoftware.kryo
+ kryo
+ 2.24.0
+
+
+ commons-codec
+ commons-codec
+ 1.10
+
junit
junit
@@ -125,9 +138,11 @@
protobuf-java directly, you will be transitively depending on the
protobuf-java version that grpc depends on.
-->
- com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
+ com.google.protobuf:protoc:${protobuf.version}:exe:${os.detected.classifier}
+
grpc-java
- io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
+ io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
+
diff --git a/cellbase-core/src/main/java/org/opencb/cellbase/core/api/DBAdaptorFactory.java b/cellbase-core/src/main/java/org/opencb/cellbase/core/api/DBAdaptorFactory.java
index 29af3d5f0f..1101185845 100644
--- a/cellbase-core/src/main/java/org/opencb/cellbase/core/api/DBAdaptorFactory.java
+++ b/cellbase-core/src/main/java/org/opencb/cellbase/core/api/DBAdaptorFactory.java
@@ -17,7 +17,6 @@
package org.opencb.cellbase.core.api;
import org.opencb.cellbase.core.config.CellBaseConfiguration;
-import org.opencb.cellbase.core.config.Species;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -105,7 +104,7 @@ public void setConfiguration(CellBaseConfiguration cellBaseConfiguration) {
public abstract ConservationDBAdaptor getConservationDBAdaptor(String species, String assembly);
- protected Species getSpecies(String speciesName) {
+/* protected Species getSpecies(String speciesName) {
Species species = null;
for (Species sp : cellBaseConfiguration.getAllSpecies()) {
if (speciesName.equalsIgnoreCase(sp.getId()) || speciesName.equalsIgnoreCase(sp.getScientificName())) {
@@ -128,6 +127,6 @@ protected String getAssembly(Species species, String assemblyName) {
}
}
return assembly;
- }
+ }*/
}
diff --git a/cellbase-core/src/main/java/org/opencb/cellbase/core/cache/CacheManager.java b/cellbase-core/src/main/java/org/opencb/cellbase/core/cache/CacheManager.java
new file mode 100644
index 0000000000..3f54ed85bb
--- /dev/null
+++ b/cellbase-core/src/main/java/org/opencb/cellbase/core/cache/CacheManager.java
@@ -0,0 +1,145 @@
+package org.opencb.cellbase.core.cache;
+
+import org.apache.commons.codec.digest.DigestUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.opencb.cellbase.core.config.CacheProperties;
+import org.opencb.cellbase.core.config.CellBaseConfiguration;
+import org.opencb.commons.datastore.core.Query;
+import org.opencb.commons.datastore.core.QueryOptions;
+import org.opencb.commons.datastore.core.QueryResult;
+import org.redisson.Redisson;
+import org.redisson.api.RKeys;
+import org.redisson.api.RMap;
+import org.redisson.api.RedissonClient;
+import org.redisson.client.RedisConnectionException;
+import org.redisson.codec.JsonJacksonCodec;
+import org.redisson.codec.KryoCodec;
+import org.redisson.config.Config;
+
+import java.util.*;
+import java.util.regex.Pattern;
+
+
+/**
+ * Created by imedina on 20/10/16.
+ */
+public class CacheManager {
+
+ private final String DATABASE = "cb:";
+ private CellBaseConfiguration cellBaseConfiguration;
+ private Config redissonConfig;
+ private static RedissonClient redissonClient;
+ private CacheProperties cache;
+ private boolean redisState;
+
+ public CacheManager() {
+ }
+
+ public CacheManager(CellBaseConfiguration configuration) {
+
+ if (configuration != null && configuration.getCache() != null) {
+ this.cellBaseConfiguration = configuration;
+ cache = configuration.getCache();
+ redissonConfig = new Config();
+ redisState = true;
+ String host = (StringUtils.isNotEmpty(cache.getHost()))
+ ? cache.getHost() : cache.DEFAULT_HOST;
+ redissonConfig.useSingleServer().setAddress("redis://" + host);
+ String codec = (StringUtils.isNotEmpty(cache.getSerialization()))
+ ? cache.getSerialization() : cache.DEFAULT_SERIALIZATION;
+ if ("Kryo".equalsIgnoreCase(codec)) {
+ redissonConfig.setCodec(new KryoCodec());
+ } else if ("JSON".equalsIgnoreCase(codec)) {
+ redissonConfig.setCodec(new JsonJacksonCodec());
+ }
+ }
+ }
+
+ public QueryResult get(String key, Class clazz) {
+
+ QueryResult queryResult = new QueryResult();
+ if (isActive()) {
+ long start = System.currentTimeMillis();
+ RMap> map = getRedissonClient().getMap(key);
+ Map> result = new HashMap>();
+ Set set = new HashSet(Arrays.asList(0));
+
+ try {
+ result = map.getAll(set);
+ } catch (RedisConnectionException e) {
+ redisState = false;
+ queryResult.setWarningMsg("Unable to connect to Redis Cache, Please query WITHOUT Cache (Falling back to Database)");
+ return queryResult;
+ }
+ if (!result.isEmpty()) {
+ queryResult = (QueryResult) result.get(0).get("result");
+ // we are getting two objects, second one is query, not used at the moment
+ queryResult.setWarningMsg("Data is originated from Redis Cache !!!");
+ queryResult.setDbTime((int) (System.currentTimeMillis() - start));
+ }
+ }
+ return queryResult;
+ }
+
+ public void set(String key, Query query, QueryResult queryResult) {
+
+ if (isActive() && queryResult.getDbTime() > cache.getSlowThreshold()) {
+ RMap> map = getRedissonClient().getMap(key);
+ Map record = new HashMap();
+ record.put("query", query);
+ record.put("result", queryResult);
+ try {
+ map.fastPut(0, record);
+ } catch (RedisConnectionException e) {
+ redisState = false;
+ queryResult.setWarningMsg("Unable to connect to Redis Cache, Please query WITHOUT Cache (Falling back to Database)");
+ }
+ }
+ }
+
+ public String createKey(String species, Query query, QueryOptions queryOptions) {
+
+ queryOptions.remove("cache");
+ StringBuilder key = new StringBuilder(DATABASE);
+ key.append(cellBaseConfiguration.getVersion()).append(":").append(species);
+ SortedMap> map = new TreeMap>();
+ for (String item : query.keySet()) {
+ map.put(item.toLowerCase(), new TreeSet
junit