Why does Spring-data-redis use keys command when cache evict

Viewed 678

I use redis as my cache server when i use springboot. But i read the spring-data-redis source, when evict cache, the code is

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

Redis recommends replacing the keys command with the scan command. why does the spring team not do this.

1 Answers

This functionality isn't available in the current released version (2.5.5) and more likely will be included in the next release (2.6.*)

The cache implementation defaults to use KEYS and DEL to clear the cache. KEYS can cause performance issues with large keyspaces. Therefore, the default RedisCacheWriter can be created with a BatchStrategy to switch to a SCAN-based batch strategy. The SCAN strategy requires a batch size to avoid excessive Redis command roundtrips:

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

Note: The KEYS batch strategy is fully supported using any driver and Redis operation mode (Standalone, Clustered). SCAN is fully supported when using the Lettuce driver. Jedis supports SCAN only in non-clustered modes.

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

Related