Here i try to upgrade my spring-data-redis RedisTemplate
using reactive-redis ReactiveRedisTemplate
which is returning Publisher. In this case, i want to change the method findCache
to Mono
. The problem is old findCache
function that using spring-data-redis is accepting generic data, like this:
@Autowired
ReactiveRedisTemplate redisTemplate;
public <T> T findCache(String key, Class<T> clazz) {
Object content = this.redisTemplate.opsForValue().get(key);
if (content != null) {
return clazz.cast(content);
}
return null;
}
of course i will get error
Cannot cast reactor.core.publisher.MonoDefer to Person
then, since i want to get it to work reactively i update this code to return publisher, like this:
if (content != null) {
return ((Mono) content).flatMap(o -> clazz.cast(o));
}
but it also won't work since my findCache
accepting generic. what supposed i need to do, please help.
It would be better to specify ReactiveRedisTemplate
parameters. But if you can't, you should change your content type to Mono<Object>
. Something like this:
public <T> Mono<T> findCache(String key, Class<T> clazz) {
@SuppressWarnings("unchecked")
Mono<Object> contentMono = redisTemplate.opsForValue().get(key);
return contentMono.map(clazz::cast);
}
And if cache doesn't contain value for given key, it returns empty Mono, not null.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.