首页 > 解决方案 > Angular 8 - 使用 PublishReplay 缓存 HTTP 请求

问题描述

我现在可以在同一个组件中同时缓存 1 到 2 个 http 请求,并在缓存过期后重新缓存它,但是当我尝试在服务中再添加 1 个 publishReplay() 时,当我导航到其他组件并返回时,它冻结了几秒钟,在它返回之前。

getAllUsers(): Observable<any> {
if (this.reuse) {
  return this.reuse;
} else {
  return this.reuse = this.http.get(`${environment.url.userProfile}account-info`, this.userprofileKey()).pipe(
    publishReplay(1, 20000),
    refCount(),
    take(1)
  );
}

}

enter getUserInterests(): Observable<any> {
if (this.reuse3) {
  return this.reuse3;
} else {
  return this.reuse3 = this.http.get(`${environment.url.userProfile}interest`, this.userprofileKey()).pipe(
    publishReplay(1, 20000),
    refCount(),
    take(1)
  );
}

}

getEntitlementPlan(): Observable<any> {
if (this.reuse2) {
  return this.reuse2;
} else {
  return this.reuse2 = this.http.get(`${environment.url.entitlement}bau/subscription/current`, this.paymentKey()).pipe(
    publishReplay(1, 20000),
    refCount(),
    take(1)
  );
}

}

如果我从上面删除 1 publishReplay()。它工作顺利。

标签: javascriptangularrxjsrxjs6

解决方案


推荐阅读