2017-12-18 244 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 

試試這個。應該幫助你。