【问题标题】:Why does Spring-data-redis use keys command when cache evict为什么Spring-data-redis在缓存驱逐时使用keys命令
【发布时间】:2020-10-02 01:40:09
【问题描述】:

当我使用 springboot 时,我使用 redis 作为我的缓存服务器。 但是我看了spring-data-redis源码,当evict cache时,代码是

byte[][] keys = Optional.ofNullable(connection.keys(pattern)).orElse(Collections.emptySet())
                    .toArray(new byte[0][]);

Redis 建议将 keys 命令替换为 scan 命令。为什么春天的团队不这样做。

【问题讨论】:

  • 非常好的问题。 KEYS 命令无法扩展,在我们的应用程序中,缓存驱逐已成为瓶颈,KEYS 需要 160 毫秒,这会阻塞所有其他 redis 命令,因此缓存驱逐实质上会阻塞整个分布式应用程序集群。

标签: spring-boot redis spring-data-redis


【解决方案1】:

此功能在当前发布的版本 (2.5.5) 中不可用,很有可能将包含在下一个版本中 (2.6.*)

缓存实现默认使用 KEYS 和 DEL 来清除 缓存。 KEYS 可能会导致大键空间出现性能问题。 因此,默认的 RedisCacheWriter 可以使用 BatchStrategy 切换到基于 SCAN 的批处理策略。扫描 策略需要批量大小以避免过多的 Redis 命令 往返:

RedisCacheManager cm = RedisCacheManager.build(RedisCacheWriter.nonLockingRedisCacheWriter(
  connectionFactory, 
  BatchStrategies.scan(1000))
).cacheDefaults(defaultCacheConfig())

注意:使用任何驱动程序都完全支持 KEYS 批处理策略,并且 Redis 操作模式(独立、集群)。完全支持扫描 使用生菜驱动程序时。 Jedis 仅支持 SCAN 在 非集群模式。

https://github.com/spring-projects/spring-data-redis/blob/main/src/main/asciidoc/reference/redis-cache.adoc

【讨论】:

    猜你喜欢
    • 2018-03-23
    • 1970-01-01
    • 2022-08-03
    • 2014-03-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-02-20
    相关资源
    最近更新 更多