From 910134a611563d415d9c9b8bccb3e8be0640ac88 Mon Sep 17 00:00:00 2001 From: Nikita Koksharov Date: Mon, 25 Apr 2022 10:10:35 +0300 Subject: [PATCH] Feature - transactionAware setting added to RedissonSpringCacheManager. #4261 --- .../cache/RedissonSpringCacheManager.java | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java b/redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java index c7d8971fe..e8e786f9a 100644 --- a/redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java +++ b/redisson/src/main/java/org/redisson/spring/cache/RedissonSpringCacheManager.java @@ -30,6 +30,7 @@ import org.springframework.beans.factory.BeanDefinitionStoreException; import org.springframework.beans.factory.InitializingBean; import org.springframework.cache.Cache; import org.springframework.cache.CacheManager; +import org.springframework.cache.transaction.TransactionAwareCacheDecorator; import org.springframework.context.ResourceLoaderAware; import org.springframework.core.io.Resource; import org.springframework.core.io.ResourceLoader; @@ -49,7 +50,9 @@ public class RedissonSpringCacheManager implements CacheManager, ResourceLoaderA private boolean dynamic = true; private boolean allowNullValues = true; - + + private boolean transactionAware = false; + Codec codec; RedissonClient redisson; @@ -133,12 +136,24 @@ public class RedissonSpringCacheManager implements CacheManager, ResourceLoaderA *

* Default is true * - * @param allowNullValues - stores if true + * @param allowNullValues stores if true */ public void setAllowNullValues(boolean allowNullValues) { this.allowNullValues = allowNullValues; } + /** + * Defines if cache aware of Spring-managed transactions. + * If {@code true} put/evict operations are executed only for successful transaction in after-commit phase. + *

+ * Default is false + * + * @param transactionAware cache is transaction aware if true + */ + public void setTransactionAware(boolean transactionAware) { + this.transactionAware = transactionAware; + } + /** * Defines 'fixed' cache names. * A new cache instance will not be created in dynamic for non-defined names. @@ -225,6 +240,9 @@ public class RedissonSpringCacheManager implements CacheManager, ResourceLoaderA RMap map = getMap(name, config); Cache cache = new RedissonCache(map, allowNullValues); + if (transactionAware) { + cache = new TransactionAwareCacheDecorator(cache); + } Cache oldCache = instanceMap.putIfAbsent(name, cache); if (oldCache != null) { cache = oldCache; @@ -243,6 +261,9 @@ public class RedissonSpringCacheManager implements CacheManager, ResourceLoaderA RMapCache map = getMapCache(name, config); Cache cache = new RedissonCache(map, config, allowNullValues); + if (transactionAware) { + cache = new TransactionAwareCacheDecorator(cache); + } Cache oldCache = instanceMap.putIfAbsent(name, cache); if (oldCache != null) { cache = oldCache;