public People getPeople(String name) {
People people = getCache(name);
if (people != null) {
return people;
}
//Mono<People> 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<People> getPeople(String name) {
Mono<People> peopleMono = httpService
.monoRequest("http://www.xxx.com/path/info?name=" + name, People.class);
Mono<Boolean> setCacheMono = peopleMono.flatMap(people -> setCache(name, people));
return peopleMono
.and(setCacheMono)
.then(peopleMono);
}
private Mono<People> getnCache(String name) {
return reactiveRedisTemplate.opsForValue()
.get(name)
.map(this::deserialize);
}
private Mono<Boolean> setCache(String name, People people) {
return reactiveRedisTemplate.opsForValue()
.set(name, serialize(people));
}
这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。
V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。
V2EX is a community of developers, designers and creative people.