All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.springframework.data.redis.cache.RedisCacheConfiguration Maven / Gradle / Ivy

There is a newer version: 3.2.5
Show newest version
/*
 * Copyright 2017-2023 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.springframework.data.redis.cache;

import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.util.function.Consumer;

import org.springframework.cache.Cache;
import org.springframework.cache.interceptor.SimpleKey;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.data.redis.serializer.RedisSerializationContext.SerializationPair;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.format.support.DefaultFormattingConversionService;
import org.springframework.lang.Nullable;
import org.springframework.util.Assert;

/**
 * Immutable {@link RedisCacheConfiguration} used to customize {@link RedisCache} behaviour, such as caching
 * {@literal null} values, computing cache key prefixes and handling binary serialization.
 * 

* Start with {@link RedisCacheConfiguration#defaultCacheConfig()} and customize {@link RedisCache} behaviour * from that point on. * * @author Christoph Strobl * @author Mark Paluch * @author John Blum * @since 2.0 */ public class RedisCacheConfiguration { protected static final boolean DEFAULT_CACHE_NULL_VALUES = true; protected static final boolean DEFAULT_USE_PREFIX = true; protected static final boolean DO_NOT_CACHE_NULL_VALUES = false; protected static final boolean DO_NOT_USE_PREFIX = false; /** * Default {@link RedisCacheConfiguration} using the following: *

*
key expiration
*
eternal
*
cache null values
*
yes
*
prefix cache keys
*
yes
*
default prefix
*
[the actual cache name]
*
key serializer
*
{@link org.springframework.data.redis.serializer.StringRedisSerializer}
*
value serializer
*
{@link org.springframework.data.redis.serializer.JdkSerializationRedisSerializer}
*
conversion service
*
{@link DefaultFormattingConversionService} with {@link #registerDefaultConverters(ConverterRegistry) default} * cache key converters
*
* * @return new {@link RedisCacheConfiguration}. */ public static RedisCacheConfiguration defaultCacheConfig() { return defaultCacheConfig(null); } /** * Create default {@link RedisCacheConfiguration} given {@link ClassLoader} using the following: *
*
key expiration
*
eternal
*
cache null values
*
yes
*
prefix cache keys
*
yes
*
default prefix
*
[the actual cache name]
*
key serializer
*
{@link org.springframework.data.redis.serializer.StringRedisSerializer}
*
value serializer
*
{@link org.springframework.data.redis.serializer.JdkSerializationRedisSerializer}
*
conversion service
*
{@link DefaultFormattingConversionService} with {@link #registerDefaultConverters(ConverterRegistry) default} * cache key converters
*
* * @param classLoader the {@link ClassLoader} used for deserialization by the * {@link org.springframework.data.redis.serializer.JdkSerializationRedisSerializer}. * @return new {@link RedisCacheConfiguration}. * @since 2.1 */ public static RedisCacheConfiguration defaultCacheConfig(@Nullable ClassLoader classLoader) { DefaultFormattingConversionService conversionService = new DefaultFormattingConversionService(); registerDefaultConverters(conversionService); return new RedisCacheConfiguration(Duration.ZERO, DEFAULT_CACHE_NULL_VALUES, DEFAULT_USE_PREFIX, CacheKeyPrefix.simple(), SerializationPair.fromSerializer(RedisSerializer.string()), SerializationPair.fromSerializer(RedisSerializer.java(classLoader)), conversionService); } private final boolean cacheNullValues; private final boolean usePrefix; private final CacheKeyPrefix keyPrefix; private final ConversionService conversionService; private final Duration ttl; private final SerializationPair keySerializationPair; private final SerializationPair valueSerializationPair; @SuppressWarnings("unchecked") private RedisCacheConfiguration(Duration ttl, Boolean cacheNullValues, Boolean usePrefix, CacheKeyPrefix keyPrefix, SerializationPair keySerializationPair, SerializationPair valueSerializationPair, ConversionService conversionService) { this.ttl = ttl; this.cacheNullValues = cacheNullValues; this.usePrefix = usePrefix; this.keyPrefix = keyPrefix; this.keySerializationPair = keySerializationPair; this.valueSerializationPair = (SerializationPair) valueSerializationPair; this.conversionService = conversionService; } /** * Prefix the {@link RedisCache#getName() cache name} with the given value.
* The generated cache key will be: {@code prefix + cache name + "::" + cache entry key}. * * @param prefix the prefix to prepend to the cache name. * @return new {@link RedisCacheConfiguration}. * @see #computePrefixWith(CacheKeyPrefix) * @see CacheKeyPrefix#prefixed(String) * @since 2.3 */ public RedisCacheConfiguration prefixCacheNameWith(String prefix) { return computePrefixWith(CacheKeyPrefix.prefixed(prefix)); } /** * Use the given {@link CacheKeyPrefix} to compute the prefix for the actual Redis {@literal key} given the * {@literal cache name} as function input. * * @param cacheKeyPrefix must not be {@literal null}. * @return new {@link RedisCacheConfiguration}. * @since 2.0.4 * @see CacheKeyPrefix */ public RedisCacheConfiguration computePrefixWith(CacheKeyPrefix cacheKeyPrefix) { Assert.notNull(cacheKeyPrefix, "Function for computing prefix must not be null"); return new RedisCacheConfiguration(ttl, cacheNullValues, DEFAULT_USE_PREFIX, cacheKeyPrefix, keySerializationPair, valueSerializationPair, conversionService); } /** * Disable caching {@literal null} values.
* NOTE any {@link org.springframework.cache.Cache#put(Object, Object)} operation involving * {@literal null} value will error. Nothing will be written to Redis, nothing will be removed. An already existing * key will still be there afterwards with the very same value as before. * * @return new {@link RedisCacheConfiguration}. */ public RedisCacheConfiguration disableCachingNullValues() { return new RedisCacheConfiguration(ttl, DO_NOT_CACHE_NULL_VALUES, usePrefix, keyPrefix, keySerializationPair, valueSerializationPair, conversionService); } /** * Disable using cache key prefixes.
* NOTE: {@link Cache#clear()} might result in unintended removal of {@literal key}s in Redis. Make * sure to use a dedicated Redis instance when disabling prefixes. * * @return new {@link RedisCacheConfiguration}. */ public RedisCacheConfiguration disableKeyPrefix() { return new RedisCacheConfiguration(ttl, cacheNullValues, DO_NOT_USE_PREFIX, keyPrefix, keySerializationPair, valueSerializationPair, conversionService); } /** * Set the ttl to apply for cache entries. Use {@link Duration#ZERO} to declare an eternal cache. * * @param ttl must not be {@literal null}. * @return new {@link RedisCacheConfiguration}. */ public RedisCacheConfiguration entryTtl(Duration ttl) { Assert.notNull(ttl, "TTL duration must not be null"); return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair, valueSerializationPair, conversionService); } /** * Define the {@link SerializationPair} used for de-/serializing cache keys. * * @param keySerializationPair must not be {@literal null}. * @return new {@link RedisCacheConfiguration}. */ public RedisCacheConfiguration serializeKeysWith(SerializationPair keySerializationPair) { Assert.notNull(keySerializationPair, "KeySerializationPair must not be null"); return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair, valueSerializationPair, conversionService); } /** * Define the {@link SerializationPair} used for de-/serializing cache values. * * @param valueSerializationPair must not be {@literal null}. * @return new {@link RedisCacheConfiguration}. */ public RedisCacheConfiguration serializeValuesWith(SerializationPair valueSerializationPair) { Assert.notNull(valueSerializationPair, "ValueSerializationPair must not be null"); return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair, valueSerializationPair, conversionService); } /** * Define the {@link ConversionService} used for cache key to {@link String} conversion. * * @param conversionService must not be {@literal null}. * @return new {@link RedisCacheConfiguration}. */ public RedisCacheConfiguration withConversionService(ConversionService conversionService) { Assert.notNull(conversionService, "ConversionService must not be null"); return new RedisCacheConfiguration(ttl, cacheNullValues, usePrefix, keyPrefix, keySerializationPair, valueSerializationPair, conversionService); } /** * @return {@literal true} if caching {@literal null} is allowed. */ public boolean getAllowCacheNullValues() { return cacheNullValues; } /** * @return {@literal true} if cache keys need to be prefixed with the {@link #getKeyPrefixFor(String)} if present or * the default which resolves to {@link Cache#getName()}. */ public boolean usePrefix() { return usePrefix; } /** * @return The {@link ConversionService} used for cache key to {@link String} conversion. Never {@literal null}. */ public ConversionService getConversionService() { return conversionService; } /** * Get the computed {@literal key} prefix for a given {@literal cacheName}. * * @return never {@literal null}. * @since 2.0.4 */ public String getKeyPrefixFor(String cacheName) { Assert.notNull(cacheName, "Cache name must not be null"); return keyPrefix.compute(cacheName); } /** * @return never {@literal null}. */ public SerializationPair getKeySerializationPair() { return keySerializationPair; } /** * @return never {@literal null}. */ public SerializationPair getValueSerializationPair() { return valueSerializationPair; } /** * @return The expiration time (ttl) for cache entries. Never {@literal null}. */ public Duration getTtl() { return ttl; } /** * Adds a {@link Converter} to extract the {@link String} representation of a {@literal cache key} * if no suitable {@link Object#toString()} method is present. * * @param cacheKeyConverter {@link Converter} used to convert a {@literal cache key} into a {@link String}. * @throws IllegalStateException if {@link #getConversionService()} does not allow {@link Converter} registration. * @see org.springframework.core.convert.converter.Converter * @since 2.2 */ public void addCacheKeyConverter(Converter cacheKeyConverter) { configureKeyConverters(it -> it.addConverter(cacheKeyConverter)); } /** * Configure the underlying {@link ConversionService} used to extract the {@literal cache key}. * * @param registryConsumer {@link Consumer} used to register a {@link Converter} * with the configured {@link ConverterRegistry}; never {@literal null}. * @throws IllegalStateException if {@link #getConversionService()} does not allow {@link Converter} registration. * @see org.springframework.core.convert.converter.ConverterRegistry * @since 2.2 */ public void configureKeyConverters(Consumer registryConsumer) { if (!(getConversionService() instanceof ConverterRegistry)) { String message = "'%s' returned by getConversionService() does not allow Converter registration;" + " Please make sure to provide a ConversionService that implements ConverterRegistry"; throw new IllegalStateException(String.format(message, getConversionService().getClass().getName())); } registryConsumer.accept((ConverterRegistry) getConversionService()); } /** * Registers default cache {@link Converter key converters}. *

* The following converters get registered: *

*

    *
  • {@link String} to {@link byte byte[]} using UTF-8 encoding.
  • *
  • {@link SimpleKey} to {@link String}
  • *
* * @param registry {@link ConverterRegistry} in which the {@link Converter key converters} are registered; * must not be {@literal null}. * @see org.springframework.core.convert.converter.ConverterRegistry */ public static void registerDefaultConverters(ConverterRegistry registry) { Assert.notNull(registry, "ConverterRegistry must not be null"); registry.addConverter(String.class, byte[].class, source -> source.getBytes(StandardCharsets.UTF_8)); registry.addConverter(SimpleKey.class, String.class, SimpleKey::toString); } }