Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RedisCacheConfiguration.prefixKeysWith(…) does not consider cache name [DATAREDIS-1041] #1614

Closed
spring-projects-issues opened this issue Oct 2, 2019 · 5 comments
Assignees
Labels
in: cache RedisCache and CacheManager type: bug A general bug

Comments

@spring-projects-issues
Copy link

Mykyta Bezverkhyi opened DATAREDIS-1041 and commented

Assuming I have spring-data-redis application with the following method:

@Cacheable(
    cacheNames = "cacheName",
    key = "T(java.lang.String).format('tiles:%s', #categories.hashCode())")
public List<Tile> findAll(Set<String> categories) {
    return findAllByCategories(categories);
}

The entries are saved to Redis as following:

cacheName::tiles:253903999

Now I can evict this cache using another method, for example:

@CacheEvict(
    cacheNames = "cacheName", 
    allEntries = true)
public Optional<Tile> save(Tile entity) {
    return super.save(entity);
}

And everything works as expected.

But setting property

spring.cache.redis.key-prefix=prefix:

gives me the following entry in Redis:

prefix:tiles:253903999

while

cacheName::prefix:tiles:253903999

is expected.

Actual result: cacheNames parameter don't work when key-prefix is set. 

Expected result: cacheNames parameter works as expected and all entries can be evicted


Affects: 2.2 GA (Moore)

Reference URL: spring-projects/spring-boot#16676

Issue Links:

  • DATAREDIS-975 RedisCacheConfiguration prefixKeysWith override the cacheName
    ("is duplicated by")
  • DATAREDIS-1006 Consider pre-prefix with spring.cache.redis.key-prefix for CacheKeyPrefix#simple()
    ("supersedes")

Referenced from: pull request #507

1 votes, 5 watchers

@spring-projects-issues
Copy link
Author

Christoph Strobl commented

The Spring Boot o.s.b.autoconfigure.cache.RedisCacheConfiguration translates the key-prefix to o.s.data.redis.cache.RedisCacheConfiguration#prefixKeysWith(String).
You can override the default by registering your very own o.s.data.redis.cache.RedisCacheConfiguration using computePrefixWith(CacheKeyPrefix) to generate the desired key.

@Bean
RedisCacheConfiguration cacheConfig(CacheProperties cacheProperties) {
	
	return RedisCacheConfiguration.defaultCacheConfig()
		.computePrefixWith(cacheName -> cacheName + "::" + cacheProperties.getKeyPrefix() + ":"));
}

You may also open a ticket for boot to discuss if it makes sense to maybe switch to computePrefixWith

@spring-projects-issues
Copy link
Author

James Howe commented

I would expect the prefix to be the other way around, and for the implementation to be

 public RedisCacheConfiguration prefixKeysWith(String prefix) {

   Assert.notNull(prefix, "Prefix must not be null!");

   return computePrefixWith((cacheName) -> prefix + cacheName + "::");
}

@spring-projects-issues
Copy link
Author

Stéphane Nicoll commented

Spring Boot is delegating to RedisCacheConfiguration#prefixKeysWith. That current arrangement causes issues to users so I am not keen to fix that in Spring Boot. Perhaps the implementation in Spring Data can be changed for all users? Or this settings can be deprecated with a migration path that Spring Boot could use?

@spring-projects-issues
Copy link
Author

James Howe commented

Duplicate: DATAREDIS-975

@spring-projects-issues
Copy link
Author

Mark Paluch commented

That issue is addressed now by introducing a proper method prefixCacheNameWith(…) to configure a prefix for the entire entry instead of dropping the cache name. The Javadoc of the existing prefixKeysWith(…) now reflects what's happening. 

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
in: cache RedisCache and CacheManager type: bug A general bug
Projects
None yet
Development

No branches or pull requests

2 participants