在春季启动2.0 RedisCacheManager.setCacheNames

问题描述 投票:1回答:1

你如何在Spring 2.0定制RedisCacheManager实例专门设置缓存名称和到期日。

这是我过去在1.5.x的工作代码,但未能在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);
    }
}
java spring-boot redis
1个回答
0
投票

你应该能够做到这一点:

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));
    }
}

恕我直言,新生成器模式是不那么有利于定制的工作方式。看起来可能已经监督新生成器模式。

热门问题
推荐问题
最新问题