org.springframework.cache.guava

## Class GuavaCacheManager

- java.lang.Object
- - org.springframework.cache.guava.GuavaCacheManager

- All Implemented Interfaces:
    CacheManager

      public class GuavaCacheManager
      extends Object
      implements CacheManager

  `CacheManager` implementation that lazily builds `GuavaCache` instances for each `getCache(java.lang.String)` request. Also supports a 'static' mode where the set of cache names is pre-defined through `setCacheNames(java.util.Collection<java.lang.String>)`, with no dynamic creation of further cache regions at runtime.

  The configuration of the underlying cache can be fine-tuned through a Guava `CacheBuilder` or `CacheBuilderSpec`, passed into this CacheManager through `setCacheBuilder(com.google.common.cache.CacheBuilder<java.lang.Object, java.lang.Object>)`/`setCacheBuilderSpec(com.google.common.cache.CacheBuilderSpec)`. A `CacheBuilderSpec`-compliant expression value can also be applied via the `"cacheSpecification"` bean property.

  Requires Google Guava 12.0 or higher.

  Since:
    4.0
  Author:
    Juergen Hoeller
  See Also:
    `GuavaCache`

- - ### Constructor Summary

    Constructors
    Constructor and Description
    `GuavaCacheManager()` Construct a dynamic GuavaCacheManager, lazily creating cache instances as they are being requested.
    `GuavaCacheManager(String... cacheNames)` Construct a static GuavaCacheManager, managing caches for the specified cache names only.

  - ### Method Summary

    All Methods Instance Methods Concrete Methods
    Modifier and Type | Method and Description
    --- | ---
    `protected Cache` | `createGuavaCache(String name)` Create a new GuavaCache instance for the specified cache name.
    `protected com.google.common.cache.Cache<Object,Object>` | `createNativeGuavaCache(String name)` Create a native Guava Cache instance for the specified cache name.
    `Cache` | `getCache(String name)` Return the cache associated with the given name.
    `Collection<String>` | `getCacheNames()` Return a collection of the cache names known by this manager.
    `boolean` | `isAllowNullValues()` Return whether this cache manager accepts and converts `null` values for all of its caches.
    `void` | `setAllowNullValues(boolean allowNullValues)` Specify whether to accept and convert `null` values for all caches in this cache manager.
    `void` | `setCacheBuilder(com.google.common.cache.CacheBuilder<Object,Object> cacheBuilder)` Set the Guava CacheBuilder to use for building each individual `GuavaCache` instance.
    `void` | `setCacheBuilderSpec(com.google.common.cache.CacheBuilderSpec cacheBuilderSpec)` Set the Guava CacheBuilderSpec to use for building each individual `GuavaCache` instance.
    `void` | `setCacheLoader(com.google.common.cache.CacheLoader<Object,Object> cacheLoader)` Set the Guava CacheLoader to use for building each individual `GuavaCache` instance, turning it into a LoadingCache.
    `void` | `setCacheNames(Collection<String> cacheNames)` Specify the set of cache names for this CacheManager's 'static' mode.
    `void` | `setCacheSpecification(String cacheSpecification)` Set the Guava cache specification String to use for building each individual `GuavaCache` instance.

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

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

- - ### Constructor Detail

    - #### GuavaCacheManager

          public GuavaCacheManager()

      Construct a dynamic GuavaCacheManager, lazily creating cache instances as they are being requested.

    - #### GuavaCacheManager

          public GuavaCacheManager(String... cacheNames)

      Construct a static GuavaCacheManager, managing caches for the specified cache names only.

  - ### Method Detail

    - #### setCacheNames

          public void setCacheNames(Collection<String> cacheNames)

      Specify the set of cache names for this CacheManager's 'static' mode.

      The number of caches and their names will be fixed after a call to this method, with no creation of further cache regions at runtime.

    - #### setCacheBuilder

          public void setCacheBuilder(com.google.common.cache.CacheBuilder<Object,Object> cacheBuilder)

      Set the Guava CacheBuilder to use for building each individual `GuavaCache` instance.
      See Also:
        `createNativeGuavaCache(java.lang.String)`, `CacheBuilder.build()`

    - #### setCacheBuilderSpec

          public void setCacheBuilderSpec(com.google.common.cache.CacheBuilderSpec cacheBuilderSpec)

      Set the Guava CacheBuilderSpec to use for building each individual `GuavaCache` instance.
      See Also:
        `createNativeGuavaCache(java.lang.String)`, `CacheBuilder.from(CacheBuilderSpec)`

    - #### setCacheSpecification

          public void setCacheSpecification(String cacheSpecification)

      Set the Guava cache specification String to use for building each individual `GuavaCache` instance. The given value needs to comply with Guava's `CacheBuilderSpec` (see its javadoc).
      See Also:
        `createNativeGuavaCache(java.lang.String)`, `CacheBuilder.from(String)`

    - #### setCacheLoader

          public void setCacheLoader(com.google.common.cache.CacheLoader<Object,Object> cacheLoader)

      Set the Guava CacheLoader to use for building each individual `GuavaCache` instance, turning it into a LoadingCache.
      See Also:
        `createNativeGuavaCache(java.lang.String)`, `CacheBuilder.build(CacheLoader)`, `LoadingCache`

    - #### setAllowNullValues

          public void setAllowNullValues(boolean allowNullValues)

      Specify whether to accept and convert `null` values for all caches in this cache manager.

      Default is "true", despite Guava itself not supporting `null` values. An internal holder object will be used to store user-level `null`s.

    - #### isAllowNullValues

          public boolean isAllowNullValues()

      Return whether this cache manager accepts and converts `null` values for all of its caches.

    - #### getCacheNames

          public Collection<String> getCacheNames()

      Description copied from interface: `CacheManager`
      Return a collection of the cache names known by this manager.
      Specified by:
        `getCacheNames` in interface `CacheManager`
      Returns:
        the names of all caches known by the cache manager

    - #### getCache

          public Cache getCache(String name)

      Description copied from interface: `CacheManager`
      Return the cache associated with the given name.
      Specified by:
        `getCache` in interface `CacheManager`
      Parameters:
        `name` - the cache identifier (must not be `null`)
      Returns:
        the associated cache, or `null` if none found

    - #### createGuavaCache

          protected Cache createGuavaCache(String name)

      Create a new GuavaCache instance for the specified cache name.
      Parameters:
        `name` - the name of the cache
      Returns:
        the Spring GuavaCache adapter (or a decorator thereof)

    - #### createNativeGuavaCache

          protected com.google.common.cache.Cache<Object,Object> createNativeGuavaCache(String name)

      Create a native Guava Cache instance for the specified cache name.
      Parameters:
        `name` - the name of the cache
      Returns:
        the native Guava Cache instance