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

71 lines
2.3 KiB
TypeScript
Raw Normal View History

2019-07-21 16:59:47 +02:00
import * as fs from 'fs';
const fsPromises = fs.promises;
import * as cluster from 'cluster';
import memPool from './mempool';
import blocks from './blocks';
import logger from '../logger';
2021-02-01 15:54:27 +01:00
import config from '../config';
2019-07-21 16:59:47 +02:00
class DiskCache {
2021-02-01 15:54:27 +01:00
private static FILE_NAME = config.MEMPOOL.CACHE_DIR + 'cache.json';
private static FILE_NAMES = config.MEMPOOL.CACHE_DIR + 'cache{number}.json';
private static CHUNK_SIZE = 10000;
constructor() { }
async $saveCacheToDisk(): Promise<void> {
if (!cluster.isMaster) {
return;
}
try {
2020-11-02 16:06:51 +01:00
logger.debug('Writing mempool and blocks data to disk cache (async)...');
const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).slice(0, DiskCache.CHUNK_SIZE));
await fsPromises.writeFile(DiskCache.FILE_NAME, JSON.stringify({
blocks: blocks.getBlocks(),
mempool: mempoolChunk_1
}), {flag: 'w'});
for (let i = 1; i < 10; i++) {
const mempoolChunk = Object.fromEntries(
Object.entries(memPool.getMempool()).slice(
DiskCache.CHUNK_SIZE * i, i === 9 ? undefined : DiskCache.CHUNK_SIZE * i + DiskCache.CHUNK_SIZE
)
);
await fsPromises.writeFile(DiskCache.FILE_NAMES.replace('{number}', i.toString()), JSON.stringify({
mempool: mempoolChunk
}), {flag: 'w'});
}
logger.debug('Mempool and blocks data saved to disk cache');
} catch (e) {
logger.warn('Error writing to cache file: ' + e.message || e);
}
}
loadMempoolCache() {
if (!fs.existsSync(DiskCache.FILE_NAME)) {
return;
}
try {
let data: any = {};
const cacheData = fs.readFileSync(DiskCache.FILE_NAME, 'utf8');
if (cacheData) {
logger.info('Restoring mempool and blocks data from disk cache');
data = JSON.parse(cacheData);
}
for (let i = 1; i < 10; i++) {
const fileName = DiskCache.FILE_NAMES.replace('{number}', i.toString());
if (fs.existsSync(fileName)) {
const cacheData2 = JSON.parse(fs.readFileSync(fileName, 'utf8'));
Object.assign(data.mempool, cacheData2.mempool);
}
}
2019-07-21 16:59:47 +02:00
memPool.setMempool(data.mempool);
blocks.setBlocks(data.blocks);
} catch (e) {
logger.warn('Failed to parse mempoool and blocks cache. Skipping...');
}
2019-07-21 16:59:47 +02:00
}
}
export default new DiskCache();