我有一个组件调用这样的服务
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$;
}
如您所见,我正在缓存结果并将缓存的数据返回到每个后续请求。
有没有办法将此缓存逻辑移动到装饰器中?除了这个包之外,我似乎找不到任何在线示例
有一个我从未尝试过的 npm 包,但我听说过。
检查 github : https://github.com/angelnikolov/ngx-cacheable
使用 : npm install ngx-cacheable
安装它