From a65b775faa96b4688257efdb7ea0438b3a8fe603 Mon Sep 17 00:00:00 2001 From: Greg Burd Date: Fri, 27 Oct 2017 14:33:04 -0400 Subject: [PATCH] Abstract the SessionCache and allow outside implementations. --- helenus-core.iml | 5 --- pom.xml | 14 ------ .../java/net/helenus/core/HelenusSession.java | 2 +- .../net/helenus/core/SessionInitializer.java | 11 ++++- .../net/helenus/core/cache/CaffeineCache.java | 43 ------------------- .../helenus/core/cache/MemcacheDbCache.java | 41 ------------------ .../net/helenus/core/cache/RedisCache.java | 41 ------------------ .../core/operation/UpdateOperation.java | 16 ++++--- 8 files changed, 21 insertions(+), 152 deletions(-) delete mode 100644 src/main/java/net/helenus/core/cache/CaffeineCache.java delete mode 100644 src/main/java/net/helenus/core/cache/MemcacheDbCache.java delete mode 100644 src/main/java/net/helenus/core/cache/RedisCache.java diff --git a/helenus-core.iml b/helenus-core.iml index fdf1252..be96637 100644 --- a/helenus-core.iml +++ b/helenus-core.iml @@ -125,11 +125,6 @@ - - - - - diff --git a/pom.xml b/pom.xml index 4579ff7..71feca8 100644 --- a/pom.xml +++ b/pom.xml @@ -259,20 +259,6 @@ 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 2e7ee3d..e67b387 100644 --- a/src/main/java/net/helenus/core/HelenusSession.java +++ b/src/main/java/net/helenus/core/HelenusSession.java @@ -190,7 +190,7 @@ public final class HelenusSession extends AbstractSessionOperations implements C Object result = null; for (String[] combination : facetCombinations) { String cacheKey = tableName + "." + Arrays.toString(combination); - result = sessionCache.getIfPresent(cacheKey); + result = sessionCache.get(cacheKey); if (result != null) { return result; } diff --git a/src/main/java/net/helenus/core/SessionInitializer.java b/src/main/java/net/helenus/core/SessionInitializer.java index d131b7c..d5c4117 100644 --- a/src/main/java/net/helenus/core/SessionInitializer.java +++ b/src/main/java/net/helenus/core/SessionInitializer.java @@ -27,6 +27,7 @@ import com.datastax.driver.core.*; import com.google.common.util.concurrent.MoreExecutors; import brave.Tracer; +import net.helenus.core.cache.SessionCache; import net.helenus.core.reflect.DslExportable; import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.HelenusEntityType; @@ -56,6 +57,7 @@ public final class SessionInitializer extends AbstractSessionOperations { private boolean dropUnusedIndexes = false; private KeyspaceMetadata keyspaceMetadata; private AutoDdl autoDdl = AutoDdl.UPDATE; + private SessionCache sessionCache = null; SessionInitializer(Session session) { this.session = Objects.requireNonNull(session, "empty session"); @@ -123,6 +125,11 @@ public final class SessionInitializer extends AbstractSessionOperations { return this; } + public SessionInitializer setSessionCache(SessionCache sessionCache) { + this.sessionCache = sessionCache; + return this; + } + public ConsistencyLevel getDefaultConsistencyLevel() { return consistencyLevel; } @@ -243,8 +250,8 @@ public final class SessionInitializer extends AbstractSessionOperations { public synchronized HelenusSession get() { initialize(); return new HelenusSession(session, usingKeyspace, registry, showCql, printStream, sessionRepository, executor, - autoDdl == AutoDdl.CREATE_DROP, consistencyLevel, idempotent, unitOfWorkClass, metricRegistry, - zipkinTracer); + autoDdl == AutoDdl.CREATE_DROP, consistencyLevel, idempotent, unitOfWorkClass, sessionCache, + metricRegistry, zipkinTracer); } private void initialize() { diff --git a/src/main/java/net/helenus/core/cache/CaffeineCache.java b/src/main/java/net/helenus/core/cache/CaffeineCache.java deleted file mode 100644 index 0f5a9a4..0000000 --- a/src/main/java/net/helenus/core/cache/CaffeineCache.java +++ /dev/null @@ -1,43 +0,0 @@ -/* - * 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/MemcacheDbCache.java b/src/main/java/net/helenus/core/cache/MemcacheDbCache.java deleted file mode 100644 index 4f9a09c..0000000 --- a/src/main/java/net/helenus/core/cache/MemcacheDbCache.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 deleted file mode 100644 index 6d11aa5..0000000 --- a/src/main/java/net/helenus/core/cache/RedisCache.java +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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/operation/UpdateOperation.java b/src/main/java/net/helenus/core/operation/UpdateOperation.java index 44e1de9..40edffb 100644 --- a/src/main/java/net/helenus/core/operation/UpdateOperation.java +++ b/src/main/java/net/helenus/core/operation/UpdateOperation.java @@ -36,6 +36,7 @@ import net.helenus.mapping.HelenusEntity; import net.helenus.mapping.HelenusProperty; import net.helenus.mapping.MappingUtil; import net.helenus.mapping.value.BeanColumnValueProvider; +import net.helenus.mapping.value.ValueProviderMap; import net.helenus.support.HelenusMappingException; import net.helenus.support.Immutables; @@ -66,6 +67,7 @@ public final class UpdateOperation extends AbstractFilterOperation extends AbstractFilterOperation map = ((MapExportable)pojo).toMap(); - if (map.get(key) != value) { - map.put(key, value); + if (entity != null) { + if (entity.isCacheable() && pojo != null && pojo instanceof MapExportable) { + String key = prop.getPropertyName(); + Map map = ((MapExportable) pojo).toMap(); + if (!(map instanceof ValueProviderMap)) { + if (map.get(key) != value) { + map.put(key, value); + } + } } }