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

87 lines
2.2 KiB
TypeScript
Raw Normal View History

import config from '../config';
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 {
private static FILE_NAME = './cache.json';
private enabled = true;
2019-07-21 16:59:47 +02:00
constructor() {
if (process.env.workerId === '0' || !config.MEMPOOL.SPAWN_CLUSTER_PROCS) {
if (!fs.existsSync(DiskCache.FILE_NAME)) {
fs.closeSync(fs.openSync(DiskCache.FILE_NAME, 'w'));
logger.info('Disk cache file created');
}
2020-09-29 19:25:43 +02:00
process.on('SIGINT', () => {
this.saveCacheToDisk();
process.exit(2);
});
process.on('SIGTERM', () => {
this.saveCacheToDisk();
process.exit(2);
});
} else {
this.enabled = false;
2020-09-29 19:25:43 +02:00
}
}
async $saveCacheToDiskAsync(): Promise<void> {
if (!this.enabled) {
return;
}
try {
await this.$saveDataAsync(JSON.stringify({
mempool: memPool.getMempool(),
blocks: blocks.getBlocks(),
}));
logger.info('Mempool and blocks data saved to disk cache');
} catch (e) {
logger.warn('Error writing to cache file asynchronously');
}
}
loadMempoolCache() {
const cacheData = this.loadDataSync();
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 saveCacheToDisk() {
this.saveDataSync(JSON.stringify({
mempool: memPool.getMempool(),
blocks: blocks.getBlocks(),
}));
logger.info('Mempool and blocks data saved to disk cache');
}
private $saveDataAsync(dataBlob: string): Promise<void> {
return new Promise((resolve, reject) => {
fs.writeFile(DiskCache.FILE_NAME, dataBlob, (err) => {
if (err) {
return reject(err);
}
resolve();
});
});
}
private saveDataSync(dataBlob: string) {
2019-07-21 16:59:47 +02:00
fs.writeFileSync(DiskCache.FILE_NAME, dataBlob, 'utf8');
}
private loadDataSync(): string {
2019-07-21 16:59:47 +02:00
return fs.readFileSync(DiskCache.FILE_NAME, 'utf8');
}
}
export default new DiskCache();