admin管理员组文章数量:1401199
I am working on a reactive Spring Boot project using the spring-boot-starter-data-redis-reactive
library to integrate Redis as a caching mechanism.
Before I used .springframework.cache.CacheManager
, but as I use this within a spring-cloud-gateway
project and this should be always non-blocking I try to switch to a reactive implementation of redis.
I want to configure a default Time-to-Live (TTL) for cache entries in Redis, but it seems that the property spring.cache.redis.time-to-live does not work with the reactive Redis configuration (which worked before).
Here are some details about my setup:
Spring Boot version: 3.4.3
Redis library: spring-boot-starter-data-redis-reactive
I tried the following steps:
Set the TTL property in application.yaml
:
spring:
cache:
redis:
time-to-live: 60000 # TTL in milliseconds
and write some key, value like:
@Component
@Slf4j
public class UserContextReactiveRedisCacheClient implements UserContextCachePort {
public static final String CACHE_PREFIX = "core-gateway::cacheUserContext::";
private final ReactiveRedisOperations<String, String> userContextOperations;
public UserContextReactiveRedisCacheClient(
final ReactiveRedisOperations<String, String> userContextOperations
) {
this.userContextOperations = userContextOperations;
}
/**
* Load a context of a user.
*
* @param ciamId Load user context based on provided ciamId.
* @return The User Context.
*/
public Mono<String> loadUserContext(final String ciamId) {
log.debug("Loading from cache user context for ciamId {}.", ciamId);
return userContextOperations.opsForValue().get(CACHE_PREFIX + ciamId);
}
/**
* Puts as encoded user context as String in cache if absent.
*
* @param ciamId The ciamId for the user.
* @param encodedUserContext The context as a String to put.
* @return A Mono of Boolean representing success of the operation.
*/
public Mono<Boolean> putUserContextIfAbsent(
final String ciamId,
final String encodedUserContext
) {
return userContextOperations.keys(CACHE_PREFIX + ciamId)
.collectList()
.flatMap(keys -> {
if (keys.isEmpty()) {
log.debug("Putting into cache user context for ciamId {}.", ciamId);
return userContextOperations.opsForValue().set(
CACHE_PREFIX + ciamId,
encodedUserContext
);
}
return Mono.just(false);
});
}
}
This will provide in a key entry in redis:
127.0.0.1:6379> TTL "core-gateway::cacheUserContext::f3ac883e-f73f-40d7-b4a0-cd900755ed7d"
(integer) -1
So the configuration is not working properly which was before.
I need to set the expiration manually each time I write a key with value to take TTL into account like:
@Component
@Slf4j
public class UserContextReactiveRedisCacheClient implements UserContextCachePort {
public static final String CACHE_PREFIX = "core-gateway::cacheUserContext::";
private final ReactiveRedisOperations<String, String> userContextOperations;
private Duration expiration;
public UserContextReactiveRedisCacheClient(
final ReactiveRedisOperations<String, String> userContextOperations,
@Value("${spring.cache.redis.time-to-live}") final Duration expiration
) {
this.userContextOperations = userContextOperations;
this.expiration = expiration;
}
/**
* Load a context of a user.
*
* @param ciamId Load user context based on provided ciamId.
* @return The User Context.
*/
public Mono<String> loadUserContext(final String ciamId) {
log.debug("Loading from cache user context for ciamId {}.", ciamId);
return userContextOperations.opsForValue().get(CACHE_PREFIX + ciamId);
}
/**
* Puts as encoded user context as String in cache if absent.
*
* @param ciamId The ciamId for the user.
* @param encodedUserContext The context as a String to put.
* @return A Mono of Boolean representing success of the operation.
*/
public Mono<Boolean> putUserContextIfAbsent(
final String ciamId,
final String encodedUserContext
) {
return userContextOperations.keys(CACHE_PREFIX + ciamId)
.collectList()
.flatMap(keys -> {
if (keys.isEmpty()) {
log.debug("Putting into cache user context for ciamId {}.", ciamId);
return userContextOperations.opsForValue().set(
CACHE_PREFIX + ciamId,
encodedUserContext,
expiration
);
}
return Mono.just(false);
});
}
}
Is there a way to configure this so it only works with the application.yaml
configuration?
Thanks in advance for your help
本文标签:
版权声明:本文标题:java - Spring Redis Reactive Implementation with Time To Live TTL set by `spring.cache.redis.time-to-live` property - Stack Over 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744244511a2596945.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论