Package org.springframework.cache.transaction

# 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:
  - `TransactionAwareCacheManagerProxy`

## 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

Constructors
Constructor
Description
`TransactionAwareCacheDecorator(org.springframework.cache.Cache targetCache)`
Create a new TransactionAwareCache for the given target Cache.

## Method Summary

Modifier and Type
Method
Description
`void`
`clear()`
`void`
`evict(Object key)`
`boolean`
`evictIfPresent(Object key)`
`org.springframework.cache.Cache.ValueWrapper`
`get(Object key)`
`<T> T`
`get(Object key, Class<T> type)`
`<T> T`
`get(Object key, Callable<T> valueLoader)`
`String`
`getName()`
`Object`
`getNativeCache()`
`org.springframework.cache.Cache`
`getTargetCache()`
Return the target Cache that this Cache should delegate to.
`boolean`
`invalidate()`
`void`
`put(Object key, Object value)`
`org.springframework.cache.Cache.ValueWrapper`
`putIfAbsent(Object key, Object value)`
`CompletableFuture<?>`
`retrieve(Object key)`
`<T> CompletableFuture<T>`
`retrieve(Object key, Supplier<CompletableFuture<T>> valueLoader)`

### Methods inherited from class java.lang.Object

`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

## 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

@Nullable 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`

### retrieve

@Nullable public CompletableFuture<?> retrieve(Object key)

Specified by:
  `retrieve` in interface `org.springframework.cache.Cache`

### retrieve

public <T> CompletableFuture<T> retrieve(Object key, Supplier<CompletableFuture<T>> valueLoader)

Specified by:
  `retrieve` 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`