Merge pull request #4501 from takuabonn/bugfix/4475
update to use cachedRequest
This commit is contained in:
commit
acff9782cb
3
contributors/takuabonn.txt
Normal file
3
contributors/takuabonn.txt
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
I hereby accept the terms of the Contributor License Agreement in the CONTRIBUTING.md file of the mempool/mempool git repository as of December 13, 2023.
|
||||||
|
|
||||||
|
Signed: takuabonn
|
@ -1,6 +1,6 @@
|
|||||||
import { Injectable } from '@angular/core';
|
import { Injectable } from '@angular/core';
|
||||||
import { HttpClient, HttpParams } from '@angular/common/http';
|
import { HttpClient, HttpParams } from '@angular/common/http';
|
||||||
import { Observable } from 'rxjs';
|
import { BehaviorSubject, Observable, catchError, filter, of, shareReplay, take, tap } from 'rxjs';
|
||||||
import { StateService } from '../services/state.service';
|
import { StateService } from '../services/state.service';
|
||||||
import { IChannel, INodesRanking, IOldestNodes, ITopNodesPerCapacity, ITopNodesPerChannels } from '../interfaces/node-api.interface';
|
import { IChannel, INodesRanking, IOldestNodes, ITopNodesPerCapacity, ITopNodesPerChannels } from '../interfaces/node-api.interface';
|
||||||
|
|
||||||
@ -10,6 +10,8 @@ import { IChannel, INodesRanking, IOldestNodes, ITopNodesPerCapacity, ITopNodesP
|
|||||||
export class LightningApiService {
|
export class LightningApiService {
|
||||||
private apiBasePath = ''; // network path is /testnet, etc. or '' for mainnet
|
private apiBasePath = ''; // network path is /testnet, etc. or '' for mainnet
|
||||||
|
|
||||||
|
private requestCache = new Map<string, { subject: BehaviorSubject<any>, expiry: number }>;
|
||||||
|
|
||||||
constructor(
|
constructor(
|
||||||
private httpClient: HttpClient,
|
private httpClient: HttpClient,
|
||||||
private stateService: StateService,
|
private stateService: StateService,
|
||||||
@ -23,6 +25,46 @@ export class LightningApiService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private generateCacheKey(functionName: string, params: any[]): string {
|
||||||
|
return functionName + JSON.stringify(params);
|
||||||
|
}
|
||||||
|
|
||||||
|
// delete expired cache entries
|
||||||
|
private cleanExpiredCache(): void {
|
||||||
|
this.requestCache.forEach((value, key) => {
|
||||||
|
if (value.expiry < Date.now()) {
|
||||||
|
this.requestCache.delete(key);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
cachedRequest<T, F extends (...args: any[]) => Observable<T>>(
|
||||||
|
apiFunction: F,
|
||||||
|
expireAfter: number, // in ms
|
||||||
|
...params: Parameters<F>
|
||||||
|
): Observable<T> {
|
||||||
|
this.cleanExpiredCache();
|
||||||
|
|
||||||
|
const cacheKey = this.generateCacheKey(apiFunction.name, params);
|
||||||
|
if (!this.requestCache.has(cacheKey)) {
|
||||||
|
const subject = new BehaviorSubject<T | null>(null);
|
||||||
|
this.requestCache.set(cacheKey, { subject, expiry: Date.now() + expireAfter });
|
||||||
|
|
||||||
|
apiFunction.bind(this)(...params).pipe(
|
||||||
|
tap(data => {
|
||||||
|
subject.next(data as T);
|
||||||
|
}),
|
||||||
|
catchError((error) => {
|
||||||
|
subject.error(error);
|
||||||
|
return of(null);
|
||||||
|
}),
|
||||||
|
shareReplay(1),
|
||||||
|
).subscribe();
|
||||||
|
}
|
||||||
|
|
||||||
|
return this.requestCache.get(cacheKey).subject.asObservable().pipe(filter(val => val !== null), take(1));
|
||||||
|
}
|
||||||
|
|
||||||
getNode$(publicKey: string): Observable<any> {
|
getNode$(publicKey: string): Observable<any> {
|
||||||
return this.httpClient.get<any>(this.apiBasePath + '/api/v1/lightning/nodes/' + publicKey);
|
return this.httpClient.get<any>(this.apiBasePath + '/api/v1/lightning/nodes/' + publicKey);
|
||||||
}
|
}
|
||||||
|
@ -82,9 +82,9 @@ export class NodesNetworksChartComponent implements OnInit {
|
|||||||
firstRun = false;
|
firstRun = false;
|
||||||
this.miningWindowPreference = timespan;
|
this.miningWindowPreference = timespan;
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
return this.lightningApiService.listStatistics$(timespan)
|
return this.lightningApiService.cachedRequest(this.lightningApiService.listStatistics$, 250, timespan)
|
||||||
.pipe(
|
.pipe(
|
||||||
tap((response) => {
|
tap((response:any) => {
|
||||||
const data = response.body;
|
const data = response.body;
|
||||||
const chartData = {
|
const chartData = {
|
||||||
tor_nodes: data.map(val => [val.added * 1000, val.tor_nodes]),
|
tor_nodes: data.map(val => [val.added * 1000, val.tor_nodes]),
|
||||||
|
@ -81,9 +81,9 @@ export class LightningStatisticsChartComponent implements OnInit {
|
|||||||
firstRun = false;
|
firstRun = false;
|
||||||
this.miningWindowPreference = timespan;
|
this.miningWindowPreference = timespan;
|
||||||
this.isLoading = true;
|
this.isLoading = true;
|
||||||
return this.lightningApiService.listStatistics$(timespan)
|
return this.lightningApiService.cachedRequest(this.lightningApiService.listStatistics$, 250, timespan)
|
||||||
.pipe(
|
.pipe(
|
||||||
tap((response) => {
|
tap((response:any) => {
|
||||||
const data = response.body;
|
const data = response.body;
|
||||||
this.prepareChartOptions({
|
this.prepareChartOptions({
|
||||||
channel_count: data.map(val => [val.added * 1000, val.channel_count]),
|
channel_count: data.map(val => [val.added * 1000, val.channel_count]),
|
||||||
|
Loading…
x
Reference in New Issue
Block a user