diff --git a/helenus-core.iml b/helenus-core.iml index be96637..fdf1252 100644 --- a/helenus-core.iml +++ b/helenus-core.iml @@ -125,6 +125,11 @@ + + + + + diff --git a/pom.xml b/pom.xml index 71feca8..4579ff7 100644 --- a/pom.xml +++ b/pom.xml @@ -259,6 +259,20 @@ test + + + com.github.ben-manes.caffeine + jcache + 2.5.6 + test + + + + net.spy + spymemcached + 2.12.3 + + org.slf4j diff --git a/src/main/java/net/helenus/core/HelenusSession.java b/src/main/java/net/helenus/core/HelenusSession.java index 58549ad..7bb1199 100644 --- a/src/main/java/net/helenus/core/HelenusSession.java +++ b/src/main/java/net/helenus/core/HelenusSession.java @@ -27,6 +27,7 @@ import java.util.concurrent.TimeUnit; import java.util.function.Function; import java.util.stream.Collectors; +import net.helenus.core.cache.SessionCache; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -57,8 +58,6 @@ public final class HelenusSession extends AbstractSessionOperations implements C public static final Object deleted = new Object(); private static final Logger LOG = LoggerFactory.getLogger(HelenusSession.class); - private final int MAX_CACHE_SIZE = 10000; - private final int MAX_CACHE_EXPIRE_SECONDS = 600; private final Session session; private final CodecRegistry registry; @@ -71,7 +70,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C private final SessionRepository sessionRepository; private final Executor executor; private final boolean dropSchemaOnClose; - private final Cache sessionCache; + private final SessionCache sessionCache; private final RowColumnValueProvider valueProvider; private final StatementColumnValuePreparer valuePreparer; private final Metadata metadata; @@ -81,7 +80,8 @@ public final class HelenusSession extends AbstractSessionOperations implements C HelenusSession(Session session, String usingKeyspace, CodecRegistry registry, boolean showCql, PrintStream printStream, SessionRepositoryBuilder sessionRepositoryBuilder, Executor executor, boolean dropSchemaOnClose, ConsistencyLevel consistencyLevel, boolean defaultQueryIdempotency, - Class unitOfWorkClass, MetricRegistry metricRegistry, Tracer tracer) { + Class unitOfWorkClass, SessionCache sessionCache, + MetricRegistry metricRegistry, Tracer tracer) { this.session = session; this.registry = registry == null ? CodecRegistry.DEFAULT_INSTANCE : registry; this.usingKeyspace = Objects.requireNonNull(usingKeyspace, @@ -97,9 +97,11 @@ public final class HelenusSession extends AbstractSessionOperations implements C this.metricRegistry = metricRegistry; this.zipkinTracer = tracer; - this.sessionCache = CacheBuilder.newBuilder().maximumSize(MAX_CACHE_SIZE) - .expireAfterAccess(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS) - .expireAfterWrite(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).recordStats().build(); + if (sessionCache == null) { + this.sessionCache = SessionCache.defaultCache(); + } else { + this.sessionCache = sessionCache; + } this.valueProvider = new RowColumnValueProvider(this.sessionRepository); this.valuePreparer = new StatementColumnValuePreparer(this.sessionRepository); @@ -289,7 +291,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C Object merged = null; for (String[] combination : facetCombinations) { String cacheKey = tableName + "." + Arrays.toString(combination); - Object value = sessionCache.getIfPresent(cacheKey); + Object value = sessionCache.get(cacheKey); if (value == null) { sessionCache.put(cacheKey, pojo); } else { diff --git a/src/main/java/net/helenus/core/aspect/RetryAspect.java b/src/main/java/net/helenus/core/aspect/RetryAspect.java index ed7d50e..58d1f1d 100644 --- a/src/main/java/net/helenus/core/aspect/RetryAspect.java +++ b/src/main/java/net/helenus/core/aspect/RetryAspect.java @@ -89,7 +89,7 @@ public class RetryAspect { return retryAnnotation; } - Class[] argClasses = new Class[pjp.getArgs().length]; + Class[] argClasses = new Class[pjp.getArgs().length]; for (int i = 0; i < pjp.getArgs().length; i++) { argClasses[i] = pjp.getArgs()[i].getClass(); } diff --git a/src/main/java/net/helenus/core/cache/CaffeineCache.java b/src/main/java/net/helenus/core/cache/CaffeineCache.java new file mode 100644 index 0000000..0f5a9a4 --- /dev/null +++ b/src/main/java/net/helenus/core/cache/CaffeineCache.java @@ -0,0 +1,43 @@ +/* + * Copyright (C) 2015 The Helenus Authors + * + * 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 net.helenus.core.cache; + +public class CaffeineCache implements SessionCache { + + + final Cache cache; + + CaffeineCache(Cache cache) { + this.cache = cache; + } + + @Override + public void invalidate(K key) { + cache.invalidate(key); + } + + @Override + public V get(K key) { + return cache.getIfPresent(key); + } + + @Override + public void put(K key, V value) { + cache.put(key, value); + } + +} diff --git a/src/main/java/net/helenus/core/cache/GuavaCache.java b/src/main/java/net/helenus/core/cache/GuavaCache.java new file mode 100644 index 0000000..7438c63 --- /dev/null +++ b/src/main/java/net/helenus/core/cache/GuavaCache.java @@ -0,0 +1,44 @@ +/* + * Copyright (C) 2015 The Helenus Authors + * + * 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 net.helenus.core.cache; + +import com.google.common.cache.Cache; + +public class GuavaCache implements SessionCache { + + final Cache cache; + + GuavaCache(Cache cache) { + this.cache = cache; + } + + @Override + public void invalidate(K key) { + cache.invalidate(key); + } + + @Override + public V get(K key) { + return cache.getIfPresent(key); + } + + @Override + public void put(K key, V value) { + cache.put(key, value); + } + +} diff --git a/src/main/java/net/helenus/core/cache/MemcacheDbCache.java b/src/main/java/net/helenus/core/cache/MemcacheDbCache.java new file mode 100644 index 0000000..4f9a09c --- /dev/null +++ b/src/main/java/net/helenus/core/cache/MemcacheDbCache.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2015 The Helenus Authors + * + * 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 net.helenus.core.cache; + +public class MemcacheDbCache implements SessionCache { + //final Cache cache; + + MemcacheDbCache() { + //this.cache = cache; + } + + @Override + public void invalidate(K key) { + //cache.invalidate(key); + } + + @Override + public V get(K key) { + return null; + } + + @Override + public void put(K key, V value) { + //cache.put(key, value); + } + +} diff --git a/src/main/java/net/helenus/core/cache/RedisCache.java b/src/main/java/net/helenus/core/cache/RedisCache.java new file mode 100644 index 0000000..6d11aa5 --- /dev/null +++ b/src/main/java/net/helenus/core/cache/RedisCache.java @@ -0,0 +1,41 @@ +/* + * Copyright (C) 2015 The Helenus Authors + * + * 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 net.helenus.core.cache; + +public class RedisCache implements SessionCache { + //final Cache cache; + + RedisCache() { + //this.cache = cache; + } + + @Override + public void invalidate(K key) { + //cache.invalidate(key); + } + + @Override + public V get(K key) { + return null; + } + + @Override + public void put(K key, V value) { + //cache.put(key, value); + } + +} diff --git a/src/main/java/net/helenus/core/cache/SessionCache.java b/src/main/java/net/helenus/core/cache/SessionCache.java new file mode 100644 index 0000000..02b15c6 --- /dev/null +++ b/src/main/java/net/helenus/core/cache/SessionCache.java @@ -0,0 +1,36 @@ +/* + * Copyright (C) 2015 The Helenus Authors + * + * 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 net.helenus.core.cache; + +import com.google.common.cache.CacheBuilder; + +import java.util.concurrent.TimeUnit; + +public interface SessionCache { + + static SessionCache defaultCache() { + int MAX_CACHE_SIZE = 10000; + int MAX_CACHE_EXPIRE_SECONDS = 600; + return new GuavaCache(CacheBuilder.newBuilder().maximumSize(MAX_CACHE_SIZE) + .expireAfterAccess(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS) + .expireAfterWrite(MAX_CACHE_EXPIRE_SECONDS, TimeUnit.SECONDS).recordStats().build()); + } + + void invalidate(K key); + V get(K key); + void put(K key, V value); +}