我如何使用RXjs缓存请求?

问题描述 投票:0回答:1

情况如下:有文字。每个文本都会更改,我会提交一个请求。响应文本分为多个部分。然后,我将每个部分再次发送到服务器。

示例:

请求-响应1234567 => [1],[2],[3,4],[5、6、7]请求队列[1] => [2] => [3,4] => [5,6,7]

更改文字

请求-响应12334567 => [1],[2],[3,3,4],[5、6、7]

这里我想将上一个答案与新答案进行比较,仅发送[3,3,4]

我决定将其全部作为流,最后处理RxJS。其实是在寻找RxJS的方式

rxjs rxjs5 rxjs6
1个回答
0
投票

我会在这里建议一个替代方案。代替缓存响应数组,而是缓存使用Interceptor发出的HTTP请求的响应。对于缓存,简单的map将是一个不错的选择。如果您使用的是Angular,请参见下面的代码,它提供了现成的拦截功能。

例如,如果对[1]]发出HTTP请求,例如将其保存在缓存中

intercept(req: HttpRequest<any>, next: HttpHandler) {

   const response = this.cache.get(req); // get the cached response object.

   return response? Observable.of(response) : makeHttpRequest(req,next)
}

makeRequest(req, next){
 //make http request and save it in cache as well,
 const response = httpREquest();
 this.cache.put(req, response);
}

从您的示例中,当请求[3,3,4]

时,在缓存中找不到输出,发出一个http请求并保存。这样,您将避免数组比较。
© www.soinside.com 2019 - 2024. All rights reserved.