Increased disk cache chunks amount to 10 to fix json string length error.

This commit is contained in:
softsimon 2021-01-15 23:26:32 +07:00
parent eff4d2c8cd
commit 1e81355e7d
No known key found for this signature in database
GPG key ID: 488D7DCFB5A430D7
2 changed files with 40 additions and 16 deletions

10
backend/.gitignore vendored
View file

@ -43,4 +43,12 @@ testem.log
Thumbs.db
cache.json
cache2.json
cache1.json
cache2.json
cache3.json
cache4.json
cache5.json
cache6.json
cache7.json
cache8.json
cache9.json

View file

@ -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);