mirror of
https://github.com/mempool/mempool.git
synced 2025-01-18 05:12:35 +01:00
Cache electrum address history fetch a couple of seconds to prevent double requests on address page load.
This commit is contained in:
parent
9a23d2c6b0
commit
c4d1fad853
@ -11,6 +11,7 @@ import * as ElectrumClient from '@mempool/electrum-client';
|
|||||||
import * as sha256 from 'crypto-js/sha256';
|
import * as sha256 from 'crypto-js/sha256';
|
||||||
import * as hexEnc from 'crypto-js/enc-hex';
|
import * as hexEnc from 'crypto-js/enc-hex';
|
||||||
import loadingIndicators from '../loading-indicators';
|
import loadingIndicators from '../loading-indicators';
|
||||||
|
import memoryCache from '../memory-cache';
|
||||||
|
|
||||||
class BitcoindElectrsApi extends BitcoinApi implements AbstractBitcoinApi {
|
class BitcoindElectrsApi extends BitcoinApi implements AbstractBitcoinApi {
|
||||||
private electrumClient: any;
|
private electrumClient: any;
|
||||||
@ -158,7 +159,15 @@ class BitcoindElectrsApi extends BitcoinApi implements AbstractBitcoinApi {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private $getScriptHashHistory(scriptHash: string): Promise<IElectrumApi.ScriptHashHistory[]> {
|
private $getScriptHashHistory(scriptHash: string): Promise<IElectrumApi.ScriptHashHistory[]> {
|
||||||
return this.electrumClient.blockchainScripthash_getHistory(this.encodeScriptHash(scriptHash));
|
const fromCache = memoryCache.get<IElectrumApi.ScriptHashHistory[]>('Scripthash_getHistory', scriptHash);
|
||||||
|
if (fromCache) {
|
||||||
|
return Promise.resolve(fromCache);
|
||||||
|
}
|
||||||
|
return this.electrumClient.blockchainScripthash_getHistory(this.encodeScriptHash(scriptHash))
|
||||||
|
.then((history) => {
|
||||||
|
memoryCache.set('Scripthash_getHistory', scriptHash, history, 2);
|
||||||
|
return history;
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private encodeScriptHash(scriptPubKey: string): string {
|
private encodeScriptHash(scriptPubKey: string): string {
|
||||||
|
38
backend/src/api/memory-cache.ts
Normal file
38
backend/src/api/memory-cache.ts
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
interface ICache {
|
||||||
|
type: string;
|
||||||
|
id: string;
|
||||||
|
expires: Date;
|
||||||
|
data: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
class MemoryCache {
|
||||||
|
private cache: ICache[] = [];
|
||||||
|
constructor() {
|
||||||
|
setInterval(this.cleanup.bind(this), 1000);
|
||||||
|
}
|
||||||
|
|
||||||
|
public set(type: string, id: string, data: any, secondsExpiry: number) {
|
||||||
|
const expiry = new Date();
|
||||||
|
expiry.setSeconds(expiry.getSeconds() + secondsExpiry);
|
||||||
|
this.cache.push({
|
||||||
|
type: type,
|
||||||
|
id: id,
|
||||||
|
data: data,
|
||||||
|
expires: expiry,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public get<T>(type: string, id: string): T | null {
|
||||||
|
const found = this.cache.find((cache) => cache.type === type && cache.id === id);
|
||||||
|
if (found) {
|
||||||
|
return found.data;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
private cleanup() {
|
||||||
|
this.cache = this.cache.filter((cache) => cache.expires < (new Date()));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export default new MemoryCache();
|
Loading…
Reference in New Issue
Block a user