Angular 4个缓存

Angular 4 caching

问题:

  1. 我要显示大量数据

  2. 使用可以改变计数的分页(用户可以select每页显示10 / 20 / 50)

  3. 在 link https://blog.thoughtram.io/angular/2018/03/05/advanced-caching-with-rxjs.html

  4. 之后尝试使用 rxjs
  5. 但是我使用这个有两个问题....

    4.1。这提供了最新数据,但我需要显示该特定页面的数据,并在我再次回来时显示相同的数据

    4.2。我在顶部使用搜索,这需要我使用整个数据,但由于此缓存分步获取数据,因此当用户在从后端服务提取完整数据之前进行搜索时,我会遇到问题...

请提出解决此问题的最佳方法....

RXJS 主题有 3 种不同类型的提要,就

 1) if you miss it you miss it = Subject   
 2) give me the last value =  BehaviorSubject   
 3) give me all the last values = ReplaySubject

听起来您在寻找 #3 对吗? 如果是这样,请查看 ReplaySubjects。

Subject - 订阅者只会获得订阅后发出的已发布值。 A

BehaviorSubject - 最后一个值被缓存。订阅者将在初始订阅时获得最新值。

ReplaySubject - 它最多可以缓存指定数量的发射。任何订阅者都将在订阅时获得所有缓存值。

import * as Rx from "rxjs";

const subject = new Rx.ReplaySubject(2, 100);

// subscriber 1
subject.subscribe((data) => {
    console.log('Subscriber A:', data);
});

setInterval(() => subject.next(Math.random()), 200);

// subscriber 2
setTimeout(() => {
  subject.subscribe((data) => {
    console.log('Subscriber B:', data);
  });
}, 1000)

We create the ReplaySubject and specify that we only want to store the last 2 values, but no longer than a 100 ms We start emiting Subject values every 200 ms. Subscriber A will pick this up and log every value that’s being emited by the Subject. We start subscribing with Subscriber B, but we do that after 1000 ms. This means that 5 values have already been emitted by the Subject before we start subscribing. When we created the Subject we specified that we wanted to store max 2 values, but no longer then 100ms. This means that after a 1000 ms, when Subscriber B starts subscribing, it will only receive 1 value as the subject emits values every 200ms.