From 1e81355e7daaabc5bc39ba241dad5d723e70b0ca Mon Sep 17 00:00:00 2001 From: softsimon Date: Fri, 15 Jan 2021 23:26:32 +0700 Subject: [PATCH] Increased disk cache chunks amount to 10 to fix json string length error. --- backend/.gitignore | 10 +++++++- backend/src/api/disk-cache.ts | 46 +++++++++++++++++++++++------------ 2 files changed, 40 insertions(+), 16 deletions(-) diff --git a/backend/.gitignore b/backend/.gitignore index 70c0d7e6c..32c205746 100644 --- a/backend/.gitignore +++ b/backend/.gitignore @@ -43,4 +43,12 @@ testem.log Thumbs.db cache.json -cache2.json \ No newline at end of file +cache1.json +cache2.json +cache3.json +cache4.json +cache5.json +cache6.json +cache7.json +cache8.json +cache9.json diff --git a/backend/src/api/disk-cache.ts b/backend/src/api/disk-cache.ts index e3d6491a5..201391d84 100644 --- a/backend/src/api/disk-cache.ts +++ b/backend/src/api/disk-cache.ts @@ -8,8 +8,8 @@ import logger from '../logger'; class DiskCache { private static FILE_NAME = './cache.json'; - private static FILE_NAME_2 = './cache2.json'; - private static CHUNK_SIZE = 50000; + private static FILE_NAMES = './cache{number}.json'; + private static CHUNK_SIZE = 10000; constructor() { if (!cluster.isMaster) { return; @@ -30,15 +30,21 @@ class DiskCache { } try { logger.debug('Writing mempool and blocks data to disk cache (async)...'); - const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(0, DiskCache.CHUNK_SIZE)); - const mempoolChunk_2 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(DiskCache.CHUNK_SIZE)); + 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'}); - await fsPromises.writeFile(DiskCache.FILE_NAME_2, JSON.stringify({ - mempool: mempoolChunk_2 - }), {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); @@ -48,15 +54,22 @@ class DiskCache { saveCacheToDiskSync(): void { try { logger.debug('Writing mempool and blocks data to disk cache...'); - const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(0, DiskCache.CHUNK_SIZE)); - const mempoolChunk_2 = Object.fromEntries(Object.entries(memPool.getMempool()).splice(DiskCache.CHUNK_SIZE)); + const mempoolChunk_1 = Object.fromEntries(Object.entries(memPool.getMempool()).slice(0, DiskCache.CHUNK_SIZE)); fs.writeFileSync(DiskCache.FILE_NAME, JSON.stringify({ blocks: blocks.getBlocks(), mempool: mempoolChunk_1 }), {flag: 'w'}); - fs.writeFileSync(DiskCache.FILE_NAME_2, JSON.stringify({ - mempool: mempoolChunk_2 - }), {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 + ) + ); + fs.writeFileSync(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); @@ -74,9 +87,12 @@ class DiskCache { data = JSON.parse(cacheData); } - if (fs.existsSync(DiskCache.FILE_NAME_2)) { - const cacheData2 = JSON.parse(fs.readFileSync(DiskCache.FILE_NAME_2, 'utf8')); - Object.assign(data.mempool, cacheData2.mempool); + 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); + } } memPool.setMempool(data.mempool);