1

How do you customize RedisCacheManager instances in Spring 2.0 specifically to set the cache names and the expirations.

Here's the code I used to have working in 1.5.x, but fails on 2.x

public class RedisCacheManagerCustomizer implements CacheManagerCustomizer<RedisCacheManager> {
...
    @Override
    public void customize(final RedisCacheManager cacheManager) {
        final Map<String, Long> expiresMap = new HashMap<>();

        expiresMap.put(CacheNames.ACCESS_TOKEN_TO_ENTRY, accessTokenExpirationInSeconds);
        expiresMap.put(CacheNames.REFRESH_TOKEN_TO_ENTRY, jwtMaximumLifetimeInSeconds);


        // these two no longer work
        cacheManager.setCacheNames(expiresMap.keySet());
        cacheManager.setExpires(expiresMap);
    }
}
Archimedes Trajano
  • 35,625
  • 19
  • 175
  • 265

1 Answers1

0

You should be able to do this:

public class RedisCacheManagerCustomizer implements CacheManagerCustomizer<RedisCacheManager> {
    ...
    @Override
    public void customize(final RedisCacheManager cacheManager) {
        setCacheExpiry(cacheManager, CacheNames.ACCESS_TOKEN_TO_ENTRY, accessTokenExpirationInSeconds);
        setCacheExpiry(cacheManager, CacheNames.REFRESH_TOKEN_TO_ENTRY, jwtMaximumLifetimeInSeconds);
    }

    private void setCacheExpiry(RedisCacheManager cacheManager, String name, long expiry) {
        ((RedisCache) Objects.requireNonNull(cacheManager.getCache(name)))
                .getCacheConfiguration().entryTtl(Duration.ofSeconds(expiry));
    }
}

IMHO, the new builder pattern isn't so conducive to the way the customizers work. Looks like that might have been an oversight to the new builder pattern.

Dovmo
  • 8,121
  • 3
  • 30
  • 44