RedisTemplate 配置,自定义key生成策略,过期时间等


package com.test.common.config;

import java.time.Duration;

import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.cache.CacheManager;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.cache.interceptor.KeyGenerator;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.cache.RedisCacheConfiguration;
import org.springframework.data.redis.cache.RedisCacheManager;
import org.springframework.data.redis.cache.RedisCacheWriter;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
import org.springframework.data.redis.serializer.RedisSerializer;

import cn.hutool.json.JSONUtil;

/
 * RedisTemplate 配置
 *
 * @author 
 */
@EnableCaching
@Configuration
@AutoConfigureBefore(RedisAutoConfiguration.class)
public class RedisTemplateConfiguration {

  /**
   * value 值 序列化
   *
   * @return RedisSerializer
   */
  @Bean
  @ConditionalOnMissingBean(RedisSerializer.class)
  public RedisSerializer redisSerializer() {
    return new JdkSerializationRedisSerializer();
  }
  @Bean
    public KeyGenerator cacheKeyGenerator() {
        return (target, method, params) -> {
            StringBuilder sb = new StringBuilder();
            sb.append(target.getClass().getName());
            sb.append(method.getName());
            for (Object obj : params) {
              // 由于参数可能不同, hashCode肯定不一样, 缓存的key也需要不一样
                sb.append(JSONUtil.toJsonStr(obj).hashCode());
            }
            return sb.toString();
        };
    }
  @Bean(name = "redisTemplate")
  @ConditionalOnMissingBean(RedisTemplate.class)
  public RedisTemplate redisTemplate(RedisConnectionFactory redisConnectionFactory, RedisSerializer redisSerializer) {
    RedisTemplate redisTemplate = new RedisTemplate<>();
    RedisKeySerializer redisKeySerializer = new RedisKeySerializer();
    // key 序列化
    redisTemplate.setKeySerializer(redisKeySerializer);
    redisTemplate.setHashKeySerializer(redisKeySerializer);
    // value 序列化
    redisTemplate.setValueSerializer(redisSerializer);
    redisTemplate.setHashValueSerializer(redisSerializer);
    redisTemplate.setConnectionFactory(redisConnectionFactory);
    return redisTemplate;
  }

  @Bean
  public CacheManager cacheManager(RedisConnectionFactory redisConnectionFactory) {
    RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
      .entryTtl(Duration.ofSeconds(60*1));//过期时间,根据业务需要设置
    return RedisCacheManager
      .builder(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory))
      .cacheDefaults(redisCacheConfiguration).build();
  }

}
 

你可能感兴趣的:(redis)