flux unity 流体_请教一下 Webflux Reactor 编程

public People getPeople(String name) {

People people = getCache(name);

if (people != null) {

return people;

}

//Mono people = httpService.monoRequest("http://www.xxx.com/path", People.class);

people = httpService.syncRequest("http://www.xxx.com/path/info?name=" + name, People.class);

setCache(msisdn, operatorInfo);

return people;

}

private People getCache(String name) {

String str = redisTemplate.opsForValue().get(name);

if (str == null) {

return null;

}

return deserialize(str);

}

private void setCache(String name, People people) {

String str = serialize(people);

redisTemplate.opsForValue().set(name, str);

}

以上是一个非常简单的带有缓存的获取信息的方法,我尝试使用 Spring Webflux 来做,但是写起来发现举步维艰,尤其对于 null 值的流式条件判断根本想不出有什么办法去写

以下是我自己的理解写的,看起来非常不优雅,而且没有 null 判断,请问哪位大佬可以给个优雅的样例吗?网上的例子少得可怜

public Mono getPeople(String name) {

Mono peopleMono = httpService

.monoRequest("http://www.xxx.com/path/info?name=" + name, People.class);

Mono setCacheMono = peopleMono.flatMap(people -> setCache(name, people));

return peopleMono

.and(setCacheMono)

.then(peopleMono);

}

private Mono getnCache(String name) {

return reactiveRedisTemplate.opsForValue()

.get(name)

.map(this::deserialize);

}

private Mono setCache(String name, People people) {

return reactiveRedisTemplate.opsForValue()

.set(name, serialize(people));

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值