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:
  • 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

      public String getName()
      Specified by:
      getName in interface org.springframework.cache.Cache
    • getNativeCache

      public Object getNativeCache()
      Specified by:
      getNativeCache in interface org.springframework.cache.Cache
    • get

      @Nullable public org.springframework.cache.Cache.ValueWrapper get(Object key)
      Specified by:
      get in interface org.springframework.cache.Cache
    • get

      public <T> T get(Object key, @Nullable Class<T> type)
      Specified by:
      get in interface org.springframework.cache.Cache
    • get

      @Nullable public <T> T get(Object key, Callable<T> valueLoader)
      Specified by:
      get in interface org.springframework.cache.Cache
    • put

      public void put(Object key, @Nullable Object value)
      Specified by:
      put in interface org.springframework.cache.Cache
    • putIfAbsent

      @Nullable public org.springframework.cache.Cache.ValueWrapper putIfAbsent(Object key, @Nullable Object value)
      Specified by:
      putIfAbsent in interface org.springframework.cache.Cache
    • evict

      public void evict(Object key)
      Specified by:
      evict in interface org.springframework.cache.Cache
    • evictIfPresent

      public boolean evictIfPresent(Object key)
      Specified by:
      evictIfPresent in interface org.springframework.cache.Cache
    • clear

      public void clear()
      Specified by:
      clear in interface org.springframework.cache.Cache
    • invalidate

      public boolean invalidate()
      Specified by:
      invalidate in interface org.springframework.cache.Cache