2017-12-18 242 views
0

我有一个REST API,它使用基于RxScala的反应式scala驱动程序来调用mongodb。如何缓存RxJava/RxScala中的observable

在我的API控制器或服务层,我需要用缓存来避免通话使用hazelcast(或任何其他缓存工具)

我所有的服务都是异步,只返回观察到的,任何想法如何,我可以给的MongoDB用可观察的方式实现缓存?

回答

0

Cache in .doOnNext(),retrieve as .myFromCacheObservable()。 switchIfEmpty(serviceCallOrDbOrWhatever。doOnNext(mySaveToCache))

-1
Observable<String> stringResponse = response 
.flatMap(resp -> resp.getContent() 
.map(bytes -> new String(bytes))) 
.retry(5) 
.cast(String.class) 
.map(String::trim) 
.cache(); //remember the sequence of items emitted by the Observable and emit the same sequence to future Subscribers 

试试这个。应该帮助你。