SpringBoot 缓存清理导致应用阻塞问题

使用Spring Cache的 @CacheEvict,会触发Redis的Kesy命令,导致Redis服务短暂不可以用

dispatching command AsyncCommand [type=KEYS, output=KeyListOutput [output=[], error='null'], commandType=io.lettuce.core.protocol.Command]

解决方案:替换成Scan命令

    @Bean
    public RedisCacheManagerBuilderCustomizer RedisCacheManagerBuilderCustomizer(
        RedisConnectionFactory redisConnectionFactory) {
        return builder -> builder.cacheWriter(RedisCacheWriter.nonLockingRedisCacheWriter(redisConnectionFactory,
            // BatchStrategy 使用 SCAN 游标和多个 DEL 命令来删除所有匹配的键,配置批量大小,以优化扫描批处理。
            BatchStrategies.scan(100)));
    }

效果

dispatching command AsyncCommand [type=SCAN, output=KeyScanOutput [output=io.lettuce.core.KeyScanCursor@4b635b2c, error='null'], commandType=io.lettuce.core.protocol.Command]
posted @ 2025-06-15 15:08  一只盐桔鸡  阅读(12)  评论(0)    收藏  举报