在SpringDataRedis中存储对象列表的正确方法
我需要在Redis中存储一个对象列表。列表中的每个元素都应该访问一个唯一的键。为此,我认为对象列表应该存储为Redis中对象的映射(在存储它之前,我将其转换为字符串): 我在用RedisTemplate(命令式方式)实现它时使用了这种方法。现在,我正在使用反应式Redis,我想知道正确的方法是什么。我看到SpringRedis中存在在SpringDataRedis中存储对象列表的正确方法,redis,spring-data-redis,spring-data-redis-reactive,Redis,Spring Data Redis,Spring Data Redis Reactive,我需要在Redis中存储一个对象列表。列表中的每个元素都应该访问一个唯一的键。为此,我认为对象列表应该存储为Redis中对象的映射(在存储它之前,我将其转换为字符串): 我在用RedisTemplate(命令式方式)实现它时使用了这种方法。现在,我正在使用反应式Redis,我想知道正确的方法是什么。我看到SpringRedis中存在ReactiveListOperations接口 目前我有以下代码,但当我执行时,Redis没有返回任何值。我想知道我是否使用了正确的方法来实现它 @Service
ReactiveListOperations
接口
目前我有以下代码,但当我执行时,Redis没有返回任何值。我想知道我是否使用了正确的方法来实现它
@Service
@RequiredArgsConstructor
public class Service {
private final Client client;
private final ReactiveRedisTemplate<String, Map> reactiveRedisTemplate;
@Override
public Mono<Element> getElement(String name) {
return reactiveRedisTemplate.opsForValue().get(Constants.KEY_LIST)
.switchIfEmpty(Mono.defer(() -> {
//I get a map from the service when info is not stored in Redis
log.debug("Redis does not contain key: {}", Constants.KEY_LIST);
return this.client.getMap()
.doOnSuccess(mapFromService -> redisTemplate.opsForValue()
.set(Constants.KEY_LIST, mapFromService));
}))
.map(map -> {
//I suppose that flow continues here when Redis find the key in Redis
log.debug("Map from Redis: {}", map);
if (!map.containsKey(name)) {
return this.client.getMap()
.doOnSuccess(mapFromService -> reactiveRedisTemplate.opsForValue()
.set(Constants.KEY_LIST, mapFromService));
}
return map;
}).map(map -> {
//I get the specific element from the map
return map.get(name);
});
}
}
@服务
@所需参数构造函数
公务舱服务{
私人最终客户;
私人最终反应者模板反应者模板;
@凌驾
公共元素(字符串名称){
返回reactiveEdistemplate.opsForValue().get(常量.KEY\u列表)
.switchIfEmpty(单声道延迟)(()->{
//当信息未存储在Redis中时,我会从服务中获取地图
debug(“Redis不包含key:{}”、Constants.key_LIST);
返回这个.client.getMap()
.doOnSuccess(mapFromService->redisTemplate.opsfovervalue()
.set(Constants.KEY_LIST,mapFromService));
}))
.地图(地图->{
//我想,当Redis在Redis中找到密钥时,流在这里继续
debug(“来自Redis的映射:{}”,Map);
如果(!map.containsKey(名称)){
返回这个.client.getMap()
.doOnSuccess(mapFromService->reactiveedistemplate.opsfovervalue()
.set(Constants.KEY_LIST,mapFromService));
}
返回图;
}).地图(地图->{
//我从地图上得到了具体的元素
返回map.get(name);
});
}
}
提前谢谢
@Service
@RequiredArgsConstructor
public class Service {
private final Client client;
private final ReactiveRedisTemplate<String, Map> reactiveRedisTemplate;
@Override
public Mono<Element> getElement(String name) {
return reactiveRedisTemplate.opsForValue().get(Constants.KEY_LIST)
.switchIfEmpty(Mono.defer(() -> {
//I get a map from the service when info is not stored in Redis
log.debug("Redis does not contain key: {}", Constants.KEY_LIST);
return this.client.getMap()
.doOnSuccess(mapFromService -> redisTemplate.opsForValue()
.set(Constants.KEY_LIST, mapFromService));
}))
.map(map -> {
//I suppose that flow continues here when Redis find the key in Redis
log.debug("Map from Redis: {}", map);
if (!map.containsKey(name)) {
return this.client.getMap()
.doOnSuccess(mapFromService -> reactiveRedisTemplate.opsForValue()
.set(Constants.KEY_LIST, mapFromService));
}
return map;
}).map(map -> {
//I get the specific element from the map
return map.get(name);
});
}
}