Class RedisCache

All Implemented Interfaces:
Cache

public class RedisCache extends AbstractValueAdaptingCache
Cache implementation using for Redis as underlying store.

Use RedisCacheManager to create RedisCache instances.

Since:
2.0
Author:
Christoph Strobl, Mark Paluch, Piotr Mionskowski, Jos Roseboom
See Also:
  • Constructor Details

  • Method Details

    • lookup

      protected Object lookup(Object key)
      Specified by:
      lookup in class AbstractValueAdaptingCache
    • getName

      public String getName()
    • getNativeCache

      public RedisCacheWriter getNativeCache()
    • get

      public <T> T get(Object key, Callable<T> valueLoader)
    • put

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

      public Cache.ValueWrapper putIfAbsent(Object key, @Nullable Object value)
    • evict

      public void evict(Object key)
    • clear

      public void clear()
    • clear

      public void clear(String keyPattern)
      Clear keys that match the provided keyPattern.

      Useful when cache keys are formatted in a style where Redis patterns can be used for matching these.

      Parameters:
      keyPattern - the pattern of the key
      Since:
      3.0
    • getStatistics

      public CacheStatistics getStatistics()
      Return the CacheStatistics snapshot for this cache instance. Statistics are accumulated per cache instance and not from the backing Redis data store.
      Returns:
      statistics object for this RedisCache.
      Since:
      2.4
    • clearStatistics

      public void clearStatistics()
      Reset all statistics counters and gauges for this cache.
      Since:
      2.4
    • getCacheConfiguration

      public RedisCacheConfiguration getCacheConfiguration()
      Returns:
      immutable RedisCacheConfiguration. Never null.
    • preProcessCacheValue

      @Nullable protected Object preProcessCacheValue(@Nullable Object value)
      Customization hook called before passing object to RedisSerializer.
      Parameters:
      value - can be null.
      Returns:
      preprocessed value. Can be null.
    • serializeCacheKey

      protected byte[] serializeCacheKey(String cacheKey)
      Serialize the key.
      Parameters:
      cacheKey - must not be null.
      Returns:
      never null.
    • serializeCacheValue

      protected byte[] serializeCacheValue(Object value)
      Serialize the value to cache.
      Parameters:
      value - must not be null.
      Returns:
      never null.
    • deserializeCacheValue

      @Nullable protected Object deserializeCacheValue(byte[] value)
      Deserialize the given value to the actual cache value.
      Parameters:
      value - must not be null.
      Returns:
      can be null.
    • createCacheKey

      protected String createCacheKey(Object key)
      Customization hook for creating cache key before it gets serialized.
      Parameters:
      key - will never be null.
      Returns:
      never null.
    • convertKey

      protected String convertKey(Object key)
      Convert key to a String representation used for cache key creation.
      Parameters:
      key - will never be null.
      Returns:
      never null.
      Throws:
      IllegalStateException - if key cannot be converted to String.