|
|
@@ -0,0 +1,71 @@
|
|
|
+package cn.tr.plugin.cache;
|
|
|
+
|
|
|
+import org.springframework.beans.factory.ObjectProvider;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.boot.autoconfigure.cache.CacheProperties;
|
|
|
+import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
|
|
+import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
|
|
+import org.springframework.cache.CacheManager;
|
|
|
+import org.springframework.cache.annotation.EnableCaching;
|
|
|
+import org.springframework.context.annotation.Bean;
|
|
|
+import org.springframework.context.annotation.Configuration;
|
|
|
+import org.springframework.core.io.ResourceLoader;
|
|
|
+import org.springframework.data.redis.cache.RedisCacheConfiguration;
|
|
|
+import org.springframework.data.redis.cache.RedisCacheWriter;
|
|
|
+import org.springframework.data.redis.connection.RedisConnectionFactory;
|
|
|
+import org.springframework.data.redis.serializer.JdkSerializationRedisSerializer;
|
|
|
+import org.springframework.data.redis.serializer.RedisSerializationContext;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @ClassName : CustomerRedisCacheManagerAutoConfiguration
|
|
|
+ * @Description :
|
|
|
+ * @Author : LF
|
|
|
+ * @Date: 2023年04月19日
|
|
|
+ */
|
|
|
+@Configuration
|
|
|
+@EnableConfigurationProperties(CacheProperties.class)
|
|
|
+@ConditionalOnProperty(prefix = "spring.cache",name = "type",havingValue = "redis")
|
|
|
+public class CustomizeRedisCacheManagerAutoConfiguration {
|
|
|
+ /**
|
|
|
+ * 配置cacheManage 使用 RedisTemplate 的配置信息
|
|
|
+ */
|
|
|
+ @Bean
|
|
|
+ public CacheManager customizeCacheManager( CacheProperties cacheProperties,
|
|
|
+ ObjectProvider<org.springframework.data.redis.cache.RedisCacheConfiguration> redisCacheConfiguration,
|
|
|
+ RedisConnectionFactory redisConnectionFactory,
|
|
|
+ ResourceLoader resourceLoader) {
|
|
|
+ RedisCacheConfiguration defaultCacheConfiguration = determineConfiguration(cacheProperties, redisCacheConfiguration, resourceLoader.getClassLoader());
|
|
|
+
|
|
|
+ return new CustomerRedisCacheManager(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory), defaultCacheConfiguration);
|
|
|
+ }
|
|
|
+
|
|
|
+ private org.springframework.data.redis.cache.RedisCacheConfiguration determineConfiguration(
|
|
|
+ CacheProperties cacheProperties,
|
|
|
+ ObjectProvider<RedisCacheConfiguration> redisCacheConfiguration,
|
|
|
+ ClassLoader classLoader) {
|
|
|
+ return redisCacheConfiguration.getIfAvailable(() -> createConfiguration(cacheProperties, classLoader));
|
|
|
+ }
|
|
|
+
|
|
|
+ private org.springframework.data.redis.cache.RedisCacheConfiguration createConfiguration(
|
|
|
+ CacheProperties cacheProperties, ClassLoader classLoader) {
|
|
|
+ CacheProperties.Redis redisProperties = cacheProperties.getRedis();
|
|
|
+ org.springframework.data.redis.cache.RedisCacheConfiguration config = org.springframework.data.redis.cache.RedisCacheConfiguration
|
|
|
+ .defaultCacheConfig();
|
|
|
+ config = config.serializeValuesWith(
|
|
|
+ RedisSerializationContext.SerializationPair.fromSerializer(new JdkSerializationRedisSerializer(classLoader)));
|
|
|
+ if (redisProperties.getTimeToLive() != null) {
|
|
|
+ config = config.entryTtl(redisProperties.getTimeToLive());
|
|
|
+ }
|
|
|
+ if (redisProperties.getKeyPrefix() != null) {
|
|
|
+ config = config.prefixCacheNameWith(redisProperties.getKeyPrefix());
|
|
|
+ }
|
|
|
+ if (!redisProperties.isCacheNullValues()) {
|
|
|
+ config = config.disableCachingNullValues();
|
|
|
+ }
|
|
|
+ if (!redisProperties.isUseKeyPrefix()) {
|
|
|
+ config = config.disableKeyPrefix();
|
|
|
+ }
|
|
|
+ return config;
|
|
|
+ }
|
|
|
+
|
|
|
+}
|