mempool/backend/src/api/disk-cache.ts

53 lines
1.3 KiB
TypeScript
Raw Normal View History

2020-09-29 19:25:43 +02:00
const config = require('../../mempool-config.json');
2019-07-21 16:59:47 +02:00
import * as fs from 'fs';
2020-09-29 19:25:43 +02:00
import * as process from 'process';
import memPool from './mempool';
import blocks from './blocks';
import logger from '../logger';
2019-07-21 16:59:47 +02:00
class DiskCache {
static FILE_NAME = './cache.json';
constructor() {
2020-09-29 19:25:43 +02:00
if (process.env.workerId === '0' || !config.CLUSTER_NUM_CORES || config.CLUSTER_NUM_CORES === 1) {
process.on('SIGINT', () => {
this.saveCacheToDisk();
process.exit(2);
});
process.on('SIGTERM', () => {
this.saveCacheToDisk();
process.exit(2);
});
}
}
saveCacheToDisk() {
this.saveData(JSON.stringify({
mempool: memPool.getMempool(),
blocks: blocks.getBlocks(),
}));
logger.info('Mempool and blocks data saved to disk cache');
}
loadMempoolCache() {
const cacheData = this.loadData();
if (cacheData) {
logger.info('Restoring mempool and blocks data from disk cache');
const data = JSON.parse(cacheData);
memPool.setMempool(data.mempool);
blocks.setBlocks(data.blocks);
}
}
private saveData(dataBlob: string) {
2019-07-21 16:59:47 +02:00
fs.writeFileSync(DiskCache.FILE_NAME, dataBlob, 'utf8');
}
private loadData(): string {
2019-07-21 16:59:47 +02:00
return fs.readFileSync(DiskCache.FILE_NAME, 'utf8');
}
}
export default new DiskCache();