Package org.springframework.cache.caffeine

# Class CaffeineCache

java.lang.Object
org.springframework.cache.support.AbstractValueAdaptingCache
org.springframework.cache.caffeine.CaffeineCache

All Implemented Interfaces:
  `org.springframework.cache.Cache`

public class CaffeineCache extends org.springframework.cache.support.AbstractValueAdaptingCache
Spring `Cache` adapter implementation on top of a Caffeine `Cache` instance.

Supports the `retrieve(Object)` and `retrieve(Object, Supplier)` operations through Caffeine's `AsyncCache`, when provided via the `CaffeineCache(String, AsyncCache, boolean)` constructor.

Requires Caffeine 3.0 or higher, as of Spring Framework 6.1.

Since:
  4.3
Author:
  Ben Manes, Juergen Hoeller, Stephane Nicoll
See Also:
  - `CaffeineCacheManager`

## 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
`CaffeineCache(String name, com.github.benmanes.caffeine.cache.AsyncCache<Object,Object> cache, boolean allowNullValues)`
Create a `CaffeineCache` instance with the specified name and the given internal `AsyncCache` to use.
`CaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Object,Object> cache)`
Create a `CaffeineCache` instance with the specified name and the given internal `Cache` to use.
`CaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Object,Object> cache, boolean allowNullValues)`
Create a `CaffeineCache` instance with the specified name and the given internal `Cache` to use.

## Method Summary

Modifier and Type
Method
Description
`void`
`clear()`
`void`
`evict(Object key)`
`boolean`
`evictIfPresent(Object key)`
`<T> T`
`get(Object key, Callable<T> valueLoader)`
`final com.github.benmanes.caffeine.cache.AsyncCache<Object,Object>`
`getAsyncCache()`
Return the internal Caffeine AsyncCache.
`final String`
`getName()`
`final com.github.benmanes.caffeine.cache.Cache<Object,Object>`
`getNativeCache()`
Return the internal Caffeine Cache (possibly an adapter on top of an `getAsyncCache()`).
`boolean`
`invalidate()`
`protected Object`
`lookup(Object key)`
`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 org.springframework.cache.support.AbstractValueAdaptingCache

`fromStoreValue, get, get, isAllowNullValues, toStoreValue, toValueWrapper`

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

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

## Constructor Details

### CaffeineCache

public CaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Object,Object> cache)
Create a `CaffeineCache` instance with the specified name and the given internal `Cache` to use.

Parameters:
  `name` - the name of the cache
  `cache` - the backing Caffeine Cache instance

### CaffeineCache

public CaffeineCache(String name, com.github.benmanes.caffeine.cache.Cache<Object,Object> cache, boolean allowNullValues)
Create a `CaffeineCache` instance with the specified name and the given internal `Cache` to use.

Parameters:
  `name` - the name of the cache
  `cache` - the backing Caffeine Cache instance
  `allowNullValues` - whether to accept and convert `null` values for this cache

### CaffeineCache

public CaffeineCache(String name, com.github.benmanes.caffeine.cache.AsyncCache<Object,Object> cache, boolean allowNullValues)
Create a `CaffeineCache` instance with the specified name and the given internal `AsyncCache` to use.

Parameters:
  `name` - the name of the cache
  `cache` - the backing Caffeine AsyncCache instance
  `allowNullValues` - whether to accept and convert `null` values for this cache
Since:
  6.1

## Method Details

### getName

public final String getName()

### getNativeCache

public final com.github.benmanes.caffeine.cache.Cache<Object,Object> getNativeCache()
Return the internal Caffeine Cache (possibly an adapter on top of an `getAsyncCache()`).

### getAsyncCache

public final com.github.benmanes.caffeine.cache.AsyncCache<Object,Object> getAsyncCache()
Return the internal Caffeine AsyncCache.

Throws:
  `IllegalStateException` - if no AsyncCache is available
Since:
  6.1
See Also:
  - `CaffeineCache(String, AsyncCache, boolean)`
  - `CaffeineCacheManager.setAsyncCacheMode(boolean)`

### get

@Nullable public <T> T get(Object key, Callable<T> valueLoader)

### retrieve

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

### retrieve

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

### lookup

@Nullable protected Object lookup(Object key)

Specified by:
  `lookup` in class `org.springframework.cache.support.AbstractValueAdaptingCache`

### put

public void put(Object key, @Nullable Object value)

### putIfAbsent

@Nullable public org.springframework.cache.Cache.ValueWrapper putIfAbsent(Object key, @Nullable Object value)

### evict

public void evict(Object key)

### evictIfPresent

public boolean evictIfPresent(Object key)

### clear

public void clear()

### invalidate

public boolean invalidate()