content: package cn.hamm.airpower.auto;
  
  import jakarta.annotation.Resource;
  import org.springframework.cache.CacheManager;
  import org.springframework.cache.annotation.CachingConfigurer;
  import org.springframework.cache.annotation.EnableCaching;
  import org.springframework.cache.interceptor.CacheResolver;
  import org.springframework.cache.interceptor.KeyGenerator;
  import org.springframework.cache.interceptor.SimpleCacheResolver;
  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.connection.RedisConnectionFactory;
  import org.springframework.data.redis.serializer.GenericJackson2JsonRedisSerializer;
  import org.springframework.data.redis.serializer.RedisSerializationContext;
  import org.springframework.data.redis.serializer.StringRedisSerializer;
  
  import java.time.Duration;
  import java.util.Arrays;
  import java.util.Objects;
  
  /**
   * <h1>Redis 配置</h1>
   *
   * @author Hamm.cn
   */
  @Configuration
  @EnableCaching
  public class RedisConfigurer implements CachingConfigurer {
      /**
       * 连接工厂
       */
      @Resource
      private RedisConnectionFactory redisConnectionFactory;
  
      /**
       * 缓存管理器
       */
      @Bean
      @Override
      public CacheManager cacheManager() {
          RedisCacheConfiguration redisCacheConfiguration = RedisCacheConfiguration.defaultCacheConfig()
                  .entryTtl(Duration.ofMinutes(10))
                  .disableCachingNullValues()
                  .serializeKeysWith(
                          RedisSerializationContext.SerializationPair.fromSerializer(new StringRedisSerializer())
                  )
                  .serializeValuesWith(
                          RedisSerializationContext.SerializationPair.fromSerializer(
                                  new GenericJackson2JsonRedisSerializer()
                          )
                  );
          return RedisCacheManager.builder(redisConnectionFactory)
                  .cacheDefaults(redisCacheConfiguration)
                  .build();
      }
  
      /**
       * 缓存键生成器
       */
      @Bean
      @Override
      public KeyGenerator keyGenerator() {
          return (target, method, params) -> {
              StringBuilder builder = new StringBuilder()
                      .append(target.getClass().getName())
                      .append(method.getName());
              Arrays.stream(params).map(Object::toString).forEach(builder::append);
              return builder.toString();
          };
      }
  
      /**
       * 缓存解析器
       */
      @Bean
      @Override
      public CacheResolver cacheResolver() {
          return new SimpleCacheResolver(Objects.requireNonNull(cacheManager()));
      }
  }
  