Class CacheStateManager


  • public class CacheStateManager
    extends Object
    Provides functionality to support interaction with the CacheManager. This includes detection of ability to cache, as well as cache acquisition functionality.
    Author:
    Jeff Fischer
    • Field Detail

      • NULL_VALUE_WRAPPER

        public static final org.springframework.cache.Cache.ValueWrapper NULL_VALUE_WRAPPER
    • Method Detail

      • isEnabled

        public boolean isEnabled​(List<?> caches)
        Whether or not cache should be leveraged for the current call using the candidate caches.
        Parameters:
        caches - The caches to evaluate for enablement for the current call.
        Returns:
        Whether or not cache should be leveraged.
      • isEnabled

        public boolean isEnabled​(List<?> caches,
                                 Object... params)
        Whether or not cache should be leveraged for the current call using the candidate caches.
        Parameters:
        caches - The caches to evaluate for enablement for the current call.
        params - The params passed to that method.
        Returns:
        Whether or not cache should be leveraged.
      • computeIfAbsent

        public <P> List<P> computeIfAbsent​(@NonNull
                                           Function<List<String>,​List<P>> computeFunction,
                                           @NonNull
                                           ImperativeListCacheInfo cacheInfo)
        Given a Function instance meant to generate values that are cacheable, check the cache using keys based on a collection of imperative key fragments. If a cache value is available for the complex key generated based on one of the imperative keys combined with the args, then that cache value is used. Any cached values not found will utilize the computeFunction to generate the uncached values, and those generated values will be cached. The sum of originally found cache and generated values are returned.

        Note, the results are not automatically sorted, and it's possible they'll be returned in a different order than how they were originally computed (for example, if the values are computed via database query employing a sort). If reliable sorting is required, then the results should be sorted in the calling method. Also, features like sorting can be universally applied via one or more implementations of CacheResultPostProcessor, which are executed before results are returned from here.
        Type Parameters:
        P - The return type
        Parameters:
        computeFunction - The Function instance responsible for generating uncached values
        cacheInfo - Required information to generate keys and interact with the CacheManager.
        Returns:
        The sum of cache hits and generated values
      • computeIfAbsent

        public <P> P computeIfAbsent​(@NonNull
                                     Supplier<P> computeFunction,
                                     @NonNull
                                     CacheInfo cacheInfo)
        Given a Supplier instance meant to generate values that are cacheable, check the cache using a key based on a collection of key fragments. If a cache value is available for the complex key generated based on the args, then that cache value is used. If a cached values is not found it will utilize the computeFunction to generate the uncached value, and that generated value will be cached. The cached or generated value is returned. Also, additional processing can be universally applied via one or more implementations of CacheResultPostProcessor, which are executed before the result is returned from here.
        Type Parameters:
        P - The return type
        Parameters:
        computeFunction - The Function instance responsible for generating uncached values
        cacheInfo - Required information to generate keys and interact with the CacheManager.
        Returns:
        The sum of cache hits and generated values