admin管理员组文章数量:1310290
I want to validate if my Redis Spring Cache is set up correctly. Below is the configuration and the function whose results are being cached. After I call the function, I see no new keys created in Redis and in the logs, I see the function being executed every time I call it with the same parameters instead of using the result from the cache. What am I missing and how do I fix this?
This is my CacheConfiguration
@Getter
@Setter
@Configuration
@ConfigurationProperties(prefix = "my-service.redis")
@ConditionalOnProperty(value = "spring.cache.type", havingValue = "redis")
public class MyServiceCacheConfiguration {
private static final Logger LOG = LoggerFactory.getLogger(MyServiceCacheConfiguration.class);
private String hostname;
private int port;
private String username;
private String password;
private int connectionPoolSize;
private int retryAttempts;
private int minimumIdleConnections;
private int connectionTimeout;
private int idleTimeout;
protected static final Map<String, Object> CACHE_CONFIG = new HashMap<>();
@PostConstruct
public void setupCacheConfig() {
LOG.info("Getting redis config");
CACHE_CONFIG.put(HOST, hostname);
CACHE_CONFIG.put(PORT, port);
CACHE_CONFIG.put(USERNAME, username);
CACHE_CONFIG.put(PASSWORD, password);
CACHE_CONFIG.put(CONNECTION_POOL_SIZE, connectionPoolSize);
CACHE_CONFIG.put(RETRY_ATTEMPTS, retryAttempts);
CACHE_CONFIG.put(MINIMUM_IDLE_CONNECTIONS, minimumIdleConnections);
CACHE_CONFIG.put(CONNECTION_TIMEOUT, connectionTimeout);
CACHE_CONFIG.put(IDLE_TIMEOUT, idleTimeout);
}
@Bean("redissonCacheClient")
RedissonClient initRedissonClient() {
try {
Config config = new Config();
config.setCodec(JsonJacksonCodec.INSTANCE);
SingleServerConfig singleServerConfig = config.useSingleServer();
singleServerConfig.setAddress(getAddress())
.setUsername((String) CACHE_CONFIG.get(USERNAME))
// .setPassword((String) CACHE_CONFIG.get(PASSWORD))
.setConnectionPoolSize((Integer) CACHE_CONFIG.get(CONNECTION_POOL_SIZE))
.setRetryAttempts((Integer) CACHE_CONFIG.get(RETRY_ATTEMPTS))
.setConnectionMinimumIdleSize((Integer) CACHE_CONFIG.get(MINIMUM_IDLE_CONNECTIONS))
.setConnectTimeout((Integer) CACHE_CONFIG.get(CONNECTION_TIMEOUT))
.setIdleConnectionTimeout((Integer) CACHE_CONFIG.get(IDLE_TIMEOUT));
LOG.info("Creating RedissonClient client");
if (CACHE_CONFIG.get(PASSWORD) != null && !((String) CACHE_CONFIG.get(PASSWORD)).isEmpty()){
singleServerConfig.setPassword((String) CACHE_CONFIG.get(PASSWORD));
}
return Redisson.create(config);
} catch (Exception e) {
LOG.error("Exception while creating initRedissonClient client", e);
}
return null;
}
@Bean
CacheManager cacheManager(
@Qualifier("redissonCacheClient")
RedissonClient redissonClient
) {
LOG.info("Creating cache manager");
Map<String, CacheConfig> config = new HashMap<>();
return new RedissonSpringCacheManager(redissonClient, config);
}
private static String getAddress() {
return String.format("%s%s%s%s", "redis://", CACHE_CONFIG.get(HOST), ":", CACHE_CONFIG.get(PORT));
}
}
This is the function whose result I'm caching
@Cacheable(key = "#application.concat('::').concat(#label).concat('::').concat(#locale)")
public Keyword findKeyword(String application, String label, LocaleEnum locale){
Optional<Keyword> optionalKeyword = keywordRepository.findByApplicationAndLabelAndLocale(application, label, locale);
return optionalKeyword.orElse(null);
}
本文标签: How to verify if spring redis cache is workingStack Overflow
版权声明:本文标题:How to verify if spring redis cache is working - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741834609a2400138.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论