Class TransactionAwareCacheDecorator
java.lang.Object
org.springframework.cache.transaction.TransactionAwareCacheDecorator
- All Implemented Interfaces:
org.springframework.cache.Cache
public class TransactionAwareCacheDecorator
extends Object
implements org.springframework.cache.Cache
Cache decorator which synchronizes its
put(java.lang.Object, java.lang.Object)
, evict(java.lang.Object)
and
clear()
operations with Spring-managed transactions (through Spring's
TransactionSynchronizationManager
), performing the actual cache
put/evict/clear operation only in the after-commit phase of a successful
transaction. If no transaction is active, put(java.lang.Object, java.lang.Object)
, evict(java.lang.Object)
and
clear()
operations will be performed immediately, as usual.
Note: Use of immediate operations such as putIfAbsent(java.lang.Object, java.lang.Object)
and
evictIfPresent(java.lang.Object)
cannot be deferred to the after-commit phase of a
running transaction. Use these with care in a transactional environment.
- Since:
- 3.2
- Author:
- Juergen Hoeller, Stephane Nicoll, Stas Volsky
- See Also:
-
Nested Class Summary
Nested classes/interfaces inherited from interface org.springframework.cache.Cache
org.springframework.cache.Cache.ValueRetrievalException, org.springframework.cache.Cache.ValueWrapper
-
Constructor Summary
ConstructorDescriptionTransactionAwareCacheDecorator
(org.springframework.cache.Cache targetCache) Create a new TransactionAwareCache for the given target Cache. -
Method Summary
Modifier and TypeMethodDescriptionvoid
clear()
void
boolean
evictIfPresent
(Object key) org.springframework.cache.Cache.ValueWrapper
<T> T
<T> T
getName()
org.springframework.cache.Cache
Return the target Cache that this Cache should delegate to.boolean
void
org.springframework.cache.Cache.ValueWrapper
putIfAbsent
(Object key, Object value) <T> CompletableFuture<T>
retrieve
(Object key, Supplier<CompletableFuture<T>> valueLoader)
-
Constructor Details
-
TransactionAwareCacheDecorator
public TransactionAwareCacheDecorator(org.springframework.cache.Cache targetCache) Create a new TransactionAwareCache for the given target Cache.- Parameters:
targetCache
- the target Cache to decorate
-
-
Method Details
-
getTargetCache
public org.springframework.cache.Cache getTargetCache()Return the target Cache that this Cache should delegate to. -
getName
- Specified by:
getName
in interfaceorg.springframework.cache.Cache
-
getNativeCache
- Specified by:
getNativeCache
in interfaceorg.springframework.cache.Cache
-
get
- Specified by:
get
in interfaceorg.springframework.cache.Cache
-
get
- Specified by:
get
in interfaceorg.springframework.cache.Cache
-
get
- Specified by:
get
in interfaceorg.springframework.cache.Cache
-
retrieve
- Specified by:
retrieve
in interfaceorg.springframework.cache.Cache
-
retrieve
- Specified by:
retrieve
in interfaceorg.springframework.cache.Cache
-
put
- Specified by:
put
in interfaceorg.springframework.cache.Cache
-
putIfAbsent
@Nullable public org.springframework.cache.Cache.ValueWrapper putIfAbsent(Object key, @Nullable Object value) - Specified by:
putIfAbsent
in interfaceorg.springframework.cache.Cache
-
evict
- Specified by:
evict
in interfaceorg.springframework.cache.Cache
-
evictIfPresent
- Specified by:
evictIfPresent
in interfaceorg.springframework.cache.Cache
-
clear
public void clear()- Specified by:
clear
in interfaceorg.springframework.cache.Cache
-
invalidate
public boolean invalidate()- Specified by:
invalidate
in interfaceorg.springframework.cache.Cache
-