diff --git a/common/cache/pom.xml b/common/cache/pom.xml
index 80000b80a0..58a8a503ad 100644
--- a/common/cache/pom.xml
+++ b/common/cache/pom.xml
@@ -77,13 +77,13 @@
logback-classic
- junit
- junit
+ org.springframework.boot
+ spring-boot-starter-test
test
- org.mockito
- mockito-core
+ org.awaitility
+ awaitility
test
diff --git a/common/cache/src/main/java/org/thingsboard/server/cache/CaffeineCacheConfiguration.java b/common/cache/src/main/java/org/thingsboard/server/cache/CaffeineCacheConfiguration.java
index 7a0ce251fe..7f4b3edee0 100644
--- a/common/cache/src/main/java/org/thingsboard/server/cache/CaffeineCacheConfiguration.java
+++ b/common/cache/src/main/java/org/thingsboard/server/cache/CaffeineCacheConfiguration.java
@@ -27,6 +27,7 @@ import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCache;
import org.springframework.cache.support.SimpleCacheManager;
+import org.springframework.cache.transaction.TransactionAwareCacheManagerProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -47,9 +48,14 @@ public class CaffeineCacheConfiguration {
private Map specs;
+
+ /**
+ * Transaction aware CaffeineCache implementation with TransactionAwareCacheManagerProxy
+ * to synchronize cache put/evict operations with ongoing Spring-managed transactions.
+ */
@Bean
public CacheManager cacheManager() {
- log.trace("Initializing cache: {}", Arrays.toString(RemovalCause.values()));
+ log.trace("Initializing cache: {} specs {}", Arrays.toString(RemovalCause.values()), specs);
SimpleCacheManager manager = new SimpleCacheManager();
if (specs != null) {
List caches =
@@ -59,7 +65,11 @@ public class CaffeineCacheConfiguration {
.collect(Collectors.toList());
manager.setCaches(caches);
}
- return manager;
+
+ //SimpleCacheManager is not a bean (will be wrapped), so call initializeCaches manually
+ manager.initializeCaches();
+
+ return new TransactionAwareCacheManagerProxy(manager);
}
private CaffeineCache buildCache(String name, CacheSpecs cacheSpec) {
diff --git a/common/cache/src/main/java/org/thingsboard/server/cache/TBRedisCacheConfiguration.java b/common/cache/src/main/java/org/thingsboard/server/cache/TBRedisCacheConfiguration.java
index b57e1579c8..dd5af3418e 100644
--- a/common/cache/src/main/java/org/thingsboard/server/cache/TBRedisCacheConfiguration.java
+++ b/common/cache/src/main/java/org/thingsboard/server/cache/TBRedisCacheConfiguration.java
@@ -79,13 +79,19 @@ public abstract class TBRedisCacheConfiguration {
protected abstract JedisConnectionFactory loadFactory();
+ /**
+ * Transaction aware RedisCacheManager.
+ * Enable RedisCaches to synchronize cache put/evict operations with ongoing Spring-managed transactions.
+ */
@Bean
public CacheManager cacheManager(RedisConnectionFactory cf) {
DefaultFormattingConversionService redisConversionService = new DefaultFormattingConversionService();
RedisCacheConfiguration.registerDefaultConverters(redisConversionService);
registerDefaultConverters(redisConversionService);
RedisCacheConfiguration configuration = RedisCacheConfiguration.defaultCacheConfig().withConversionService(redisConversionService);
- return RedisCacheManager.builder(cf).cacheDefaults(configuration).build();
+ return RedisCacheManager.builder(cf).cacheDefaults(configuration)
+ .transactionAware()
+ .build();
}
@Bean
diff --git a/common/cache/src/test/java/org/thingsboard/server/cache/CaffeineCacheConfigurationTest.java b/common/cache/src/test/java/org/thingsboard/server/cache/CaffeineCacheConfigurationTest.java
new file mode 100644
index 0000000000..7dbe92cc37
--- /dev/null
+++ b/common/cache/src/test/java/org/thingsboard/server/cache/CaffeineCacheConfigurationTest.java
@@ -0,0 +1,62 @@
+/**
+ * Copyright © 2016-2021 The Thingsboard 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 org.thingsboard.server.cache;
+
+import lombok.extern.slf4j.Slf4j;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
+import org.springframework.cache.CacheManager;
+import org.springframework.cache.transaction.TransactionAwareCacheDecorator;
+import org.springframework.cache.transaction.TransactionAwareCacheManagerProxy;
+import org.springframework.test.context.ContextConfiguration;
+import org.springframework.test.context.TestPropertySource;
+import org.springframework.test.context.junit.jupiter.SpringExtension;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+@ExtendWith(SpringExtension.class)
+@ContextConfiguration(classes = CaffeineCacheConfiguration.class)
+@EnableConfigurationProperties
+@TestPropertySource(properties = {
+ "cache.type=caffeine",
+ "caffeine.specs.relations.timeToLiveInMinutes=1440",
+ "caffeine.specs.relations.maxSize=0",
+ "caffeine.specs.devices.timeToLiveInMinutes=60",
+ "caffeine.specs.devices.maxSize=100"})
+@Slf4j
+public class CaffeineCacheConfigurationTest {
+
+ @Autowired
+ CacheManager cacheManager;
+
+ @Test
+ public void verifyTransactionAwareCacheManagerProxy() {
+ assertThat(cacheManager).isInstanceOf(TransactionAwareCacheManagerProxy.class);
+ }
+
+ @Test
+ public void givenCacheConfig_whenCacheManagerReady_thenVerifyExistedCachesWithTransactionAwareCacheDecorator() {
+ assertThat(cacheManager.getCache("relations")).isInstanceOf(TransactionAwareCacheDecorator.class);
+ assertThat(cacheManager.getCache("devices")).isInstanceOf(TransactionAwareCacheDecorator.class);
+ }
+
+ @Test
+ public void givenCacheConfig_whenCacheManagerReady_thenVerifyNonExistedCaches() {
+ assertThat(cacheManager.getCache("rainbows_and_unicorns")).isNull();
+ }
+}
\ No newline at end of file
diff --git a/common/cache/src/test/resources/logback.xml b/common/cache/src/test/resources/logback.xml
new file mode 100644
index 0000000000..29dddea8df
--- /dev/null
+++ b/common/cache/src/test/resources/logback.xml
@@ -0,0 +1,15 @@
+
+
+
+
+
+ %d{ISO8601} [%thread] %-5level %logger{36} - %msg%n
+
+
+
+
+
+
+
+
+