SpringBoot 2.X @Cacheable,redis-cache 如何根据key设置缓存时间

demo


import com.fasterxml.jackson.annotation.JsonAutoDetect;
import com.fasterxml.jackson.annotation.PropertyAccessor;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.benmanes.caffeine.cache.Caffeine;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.CachingConfigurerSupport;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.caffeine.CaffeineCacheManager;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.Jackson2JsonRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializationContext;
import org.springframework.data.redis.serializer.RedisSerializer;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * Spring Cache 自动配置类
 */
@Configuration
@EnableCaching
public class SpringCacheAutoConfiguration extends CachingConfigurerSupport {

    /**
     * 在没有指定缓存 key 的情况下,key 的默认生成策略
     */
    @Override
    @Bean
    @Primary
    public KeyGenerator keyGenerator() {
        return (target, method, params) -> {
            StringBuilder builder = new StringBuilder();
            builder.append(target.getClass().getName());
            builder.append("#").append(method.getName());
            if (params.length == 0) {
                builder.append("()");
            } else {
                builder.append("(");
                for (Object obj : params) {
                    builder.append(obj).append(",");
                }
                builder.deleteCharAt(builder.length() - 1).append(")");
            }
            return builder.toString();
        };
    }

    /**
     * spring cache 注解,redis 缓存管理器配置,默认使用
     */
    @Bean
    @Primary
    public CacheManager redisCacheManager(RedisTemplate<Object, Object> redisTemplate) {
        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                .serializeKeysWith(RedisSerializationContext.SerializationPair.fromSerializer((RedisSerializer<String>) redisTemplate.getKeySerializer()))  // key 序列化方式
                .serializeValuesWith(RedisSerializationContext.SerializationPair.fromSerializer(redisTemplate.getValueSerializer()))                        // value 序列化方式
                .disableCachingNullValues()         // 不缓存 null 值
                .entryTtl(Duration.ofHours(3L));    // 默认缓存过期时间

        return RedisCacheManager.builder(Objects.requireNonNull(redisTemplate.getConnectionFactory()))
                .cacheDefaults(redisCacheConfiguration)
                .transactionAware()
                .withInitialCacheConfigurations(this.getRedisCacheConfigurationMap())// 指定 key 策略
                .build();
    }

    /**
     * 指定 key 策略
     * @return
     */
    private Map<String, RedisCacheConfiguration> getRedisCacheConfigurationMap() {
        Map<String, RedisCacheConfiguration> redisCacheConfigurationMap = new HashMap<>();
        redisCacheConfigurationMap.put("test-key", this.getRedisCacheConfigurationWithTtl(6));

        return redisCacheConfigurationMap;
    }

    private RedisCacheConfiguration getRedisCacheConfigurationWithTtl(Integer hours) {
        Jackson2JsonRedisSerializer<Object> jackson2JsonRedisSerializer = new Jackson2JsonRedisSerializer<>(Object.class);
        ObjectMapper om = new ObjectMapper();
        om.setVisibility(PropertyAccessor.ALL, JsonAutoDetect.Visibility.ANY);
        om.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL);
        jackson2JsonRedisSerializer.setObjectMapper(om);

        RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig();
        redisCacheConfiguration = redisCacheConfiguration.serializeValuesWith(
                RedisSerializationContext
                        .SerializationPair
                        .fromSerializer(jackson2JsonRedisSerializer)
        ).entryTtl(Duration.ofHours(hours));

        return redisCacheConfiguration;
    }


    /**
     * spring cache 注解,caffeine 缓存管理器配置
     */
    @Bean
    public CacheManager caffeineCacheManager() {
        Caffeine<Object, Object> caffeine = Caffeine.newBuilder()
                .initialCapacity(50)
                .maximumSize(1000)
                .expireAfterWrite(Duration.ofHours(3L));

        CaffeineCacheManager caffeineCacheManager = new CaffeineCacheManager();
        caffeineCacheManager.setCaffeine(caffeine);
        caffeineCacheManager.setAllowNullValues(false);

        return caffeineCacheManager;
    }
}

你可能感兴趣的:(redis)