dengzedong
2024-12-03 22d6c70a50235fb46bd6db500c99406b42d454e6
提交 | 用户 | 时间
e7c126 1 package com.iailab.framework.redis.config;
H 2
3 import cn.hutool.core.util.StrUtil;
4 import com.iailab.framework.redis.core.TimeoutRedisCacheManager;
5 import org.springframework.boot.autoconfigure.AutoConfiguration;
6 import org.springframework.boot.autoconfigure.cache.CacheProperties;
7 import org.springframework.boot.context.properties.EnableConfigurationProperties;
8 import org.springframework.cache.annotation.EnableCaching;
9 import org.springframework.context.annotation.Bean;
10 import org.springframework.context.annotation.Primary;
11 import org.springframework.data.redis.cache.BatchStrategies;
12 import org.springframework.data.redis.cache.RedisCacheConfiguration;
13 import org.springframework.data.redis.cache.RedisCacheManager;
14 import org.springframework.data.redis.cache.RedisCacheWriter;
15 import org.springframework.data.redis.connection.RedisConnectionFactory;
16 import org.springframework.data.redis.core.RedisTemplate;
17 import org.springframework.data.redis.serializer.RedisSerializationContext;
18 import org.springframework.util.StringUtils;
19
20 import java.util.Objects;
21
22 import static com.iailab.framework.redis.config.IailabRedisAutoConfiguration.buildRedisSerializer;
23
24 /**
25  * Cache 配置类,基于 Redis 实现
26  */
27 @AutoConfiguration
28 @EnableConfigurationProperties({CacheProperties.class, IailabCacheProperties.class})
29 @EnableCaching
30 public class IailabCacheAutoConfiguration {
31
32     /**
33      * RedisCacheConfiguration Bean
34      * <p>
35      * 参考 org.springframework.boot.autoconfigure.cache.RedisCacheConfiguration 的 createConfiguration 方法
36      */
37     @Bean
38     @Primary
39     public RedisCacheConfiguration redisCacheConfiguration(CacheProperties cacheProperties) {
40         RedisCacheConfiguration config = RedisCacheConfiguration.defaultCacheConfig();
41         // 设置使用 : 单冒号,而不是双 :: 冒号,避免 Redis Desktop Manager 多余空格
42         // 详细可见 https://blog.csdn.net/chuixue24/article/details/103928965 博客
43         // 再次修复单冒号,而不是双 :: 冒号问题,Issues 详情:https://gitee.com/zhijiantianya/iailab-cloud/issues/I86VY2
44         config = config.computePrefixWith(cacheName -> {
45             String keyPrefix = cacheProperties.getRedis().getKeyPrefix();
46             if (StringUtils.hasText(keyPrefix)) {
47                 keyPrefix = keyPrefix.lastIndexOf(StrUtil.COLON) == -1 ? keyPrefix + StrUtil.COLON : keyPrefix;
48                 return keyPrefix + cacheName + StrUtil.COLON;
49             }
50             return cacheName + StrUtil.COLON;
51         });
52         // 设置使用 JSON 序列化方式
53         config = config.serializeValuesWith(
54                 RedisSerializationContext.SerializationPair.fromSerializer(buildRedisSerializer()));
55
56         // 设置 CacheProperties.Redis 的属性
57         CacheProperties.Redis redisProperties = cacheProperties.getRedis();
58         if (redisProperties.getTimeToLive() != null) {
59             config = config.entryTtl(redisProperties.getTimeToLive());
60         }
61         if (!redisProperties.isCacheNullValues()) {
62             config = config.disableCachingNullValues();
63         }
64         if (!redisProperties.isUseKeyPrefix()) {
65             config = config.disableKeyPrefix();
66         }
67         return config;
68     }
69
70     @Bean
71     public RedisCacheManager redisCacheManager(RedisTemplate<String, Object> redisTemplate,
72                                                RedisCacheConfiguration redisCacheConfiguration,
73                                                IailabCacheProperties iailabCacheProperties) {
74         // 创建 RedisCacheWriter 对象
75         RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory());
76         RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory,
77                 BatchStrategies.scan(iailabCacheProperties.getRedisScanBatchSize()));
78         // 创建 TenantRedisCacheManager 对象
79         return new TimeoutRedisCacheManager(cacheWriter, redisCacheConfiguration);
80     }
81
82 }