我对 webflux 比较陌生,我想找到解决方案,以避免在有条件时出现嵌套的 flatMap:
我有 3 个简单的实体:
商品、品牌、类别。
Item基本包含:brandId和categoryId
public Mono<Item> patch(String itemId, PatchSpecs specs) {
return itemRepository.findById(itemId)
.switchIfEmpty(Mono.error(..)))
.flatMap(item -> {
final String brandId = specs.getBrandId();
if (brandId != null) {
return brandService.getById(brandId)
.switchIfEmpty(Mono.error(..)))
.flatMap(brand -> {
final String categoryId = specs.getCategoryId();
if (categoryId != null) {
return categoryService.getById(categoryId)... -> return updateAndSave(..)
}
return updateAndSave(specs, item, brand, null);
});
}
else {
final String categoryId = specs.getCategoryId();
if (categoryId != null) {
return categoryService.getById(categoryId)... -> return updateAndSave(..)
}
return …Run Code Online (Sandbox Code Playgroud) 在我的项目中,主数据库是 mongodb,为了缓存,我有 redis。现在,对于长且更复杂的查询,我使用 redis 来缓存它们显然更好。
但我想知道是否应该缓存简单的查询,例如按 id 查找,或按其他 mongodb 索引字段查找?使用 redis 进行这种索引查找有意义吗?
或者我应该不缓存这种查询,因为 mongodb 内部已经有很好的缓存机制?
查找 mongodb 索引字段更快还是查找 redis 更快?
这是我删除项目的控制器:
public Mono<ResponseEntity> delete(
@PathVariable(value = "id") String id) {
return itemService.delete(id)
.map(aVoid -> ResponseEntity.ok());
}
Run Code Online (Sandbox Code Playgroud)
itemService.delete(id) 退货 Mono<Void>
但是,当我成功删除一个项目时,它没有给我响应实体对象。它仅返回空白json。
我似乎未执行地图,因为delete方法返回 Mono<Void>
如何正确做到这一点?
我有一个List<List<String>>如何使用 Java 8 流迭代并将其存储到 Set 中?
我正在沿着这条线尝试一些东西,但我无法完全编译
List <List<String>> itemLists = ...
Set <String> codes = itemLists.stream()
.flatMap(itemList - > {
items.stream()
.collect(Collectors.toSet());
});
Run Code Online (Sandbox Code Playgroud)