我有一个组件调用这样的服务
this.service.GetCountries()
.subscribe((pCountries: ICountry[]) => {
this.mCountries = pCountries;
});`
我的服务看起来像这样
private countriesCache$ = new ReplaySubject(1);
GetCountries(pForceRefresh?: boolean) {
if (!this.countriesCache$.observers.length || pForceRefresh) {
this.http.get<ICountry[]>(path + 'GetCountries')
.subscribe(
countries => {
this.countriesCache$.next(countries)
},
error => {
this.countriesCache$.error(error);
// Recreate the Observable as after Error we cannot emit data anymore
this.countriesCache$ = new ReplaySubject(1);
}
);
}
return this.countriesCache$;
}
如您所见,我正在缓存结果并将缓存的数据返回到每个后续请求。
是否可以将这种缓存逻辑移至Decorator中?除此package
之外,我似乎找不到其他在线示例。答案 0 :(得分:0)
有一个我从未尝试过的 npm包,但我听说过。
检查github:https://github.com/angelnikolov/ngx-cacheable
安装:npm install ngx-cacheable