Commit e266a0fc4aae49b310109db2f59d985ff7e5a073

Authored by YunaiV
1 parent f03143c4

♻️ refactor(cache): 开启事务感知以优化缓存管理

yudao-framework/yudao-spring-boot-starter-biz-tenant/src/main/java/cn/iocoder/yudao/framework/tenant/config/YudaoTenantAutoConfiguration.java
@@ -192,7 +192,12 @@ public class YudaoTenantAutoConfiguration { @@ -192,7 +192,12 @@ public class YudaoTenantAutoConfiguration {
192 RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory, 192 RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory,
193 BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize())); 193 BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize()));
194 // 创建 TenantRedisCacheManager 对象 194 // 创建 TenantRedisCacheManager 对象
195 - return new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration, tenantProperties.getIgnoreCaches()); 195 + TenantRedisCacheManager cacheManager = new TenantRedisCacheManager(cacheWriter, redisCacheConfiguration,
  196 + tenantProperties.getIgnoreCaches());
  197 + // 开启事务感知:@Transactional 方法内的 @CacheEvict / @CachePut 自动延迟到 afterCommit,
  198 + // 避免事务未提交就清缓存被并发读穿写脏值;无事务时立即生效,行为不变
  199 + cacheManager.setTransactionAware(true);
  200 + return cacheManager;
196 } 201 }
197 202
198 } 203 }
yudao-framework/yudao-spring-boot-starter-redis/src/main/java/cn/iocoder/yudao/framework/redis/config/YudaoCacheAutoConfiguration.java
@@ -75,8 +75,12 @@ public class YudaoCacheAutoConfiguration { @@ -75,8 +75,12 @@ public class YudaoCacheAutoConfiguration {
75 RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory()); 75 RedisConnectionFactory connectionFactory = Objects.requireNonNull(redisTemplate.getConnectionFactory());
76 RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory, 76 RedisCacheWriter cacheWriter = RedisCacheWriter.nonLockingRedisCacheWriter(connectionFactory,
77 BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize())); 77 BatchStrategies.scan(yudaoCacheProperties.getRedisScanBatchSize()));
78 - // 创建 TenantRedisCacheManager 对象  
79 - return new TimeoutRedisCacheManager(cacheWriter, redisCacheConfiguration); 78 + // 创建 TimeoutRedisCacheManager 对象
  79 + TimeoutRedisCacheManager cacheManager = new TimeoutRedisCacheManager(cacheWriter, redisCacheConfiguration);
  80 + // 开启事务感知:@Transactional 方法内的 @CacheEvict / @CachePut 自动延迟到 afterCommit,
  81 + // 避免事务未提交就清缓存被并发读穿写脏值;无事务时立即生效,行为不变
  82 + cacheManager.setTransactionAware(true);
  83 + return cacheManager;
80 } 84 }
81 85
82 } 86 }