Skip to content

Commit

Permalink
Fix Issue#2: Removing unnecessary rewrites of the backup file
Browse files Browse the repository at this point in the history
  • Loading branch information
kwertop committed Mar 27, 2024
1 parent 33dfde4 commit 9c75610
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/persistentNodeCache.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export default class PersistentNodeCache extends NodeCache {
private flushingToDisk;
private appendFileDescriptor;
private serializer;
private changesSinceLastBackup;
constructor(cacheName: string, period?: number, dir?: string, opts?: any, serializer?: CacheSerializer);
set<T>(key: Key, value: T, ttl?: number | string): boolean;
mset<T>(keyValueSet: ValueSetItem<T>[]): boolean;
Expand Down
6 changes: 6 additions & 0 deletions lib/persistentNodeCache.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ class PersistentNodeCache extends node_cache_1.default {
};
this.serializer = customSerializer;
}
this.changesSinceLastBackup = false;
super.on("expired", (key, _) => { this.appendExpiredEvent(key); });
}
set(key, value, ttl) {
Expand Down Expand Up @@ -189,6 +190,9 @@ class PersistentNodeCache extends node_cache_1.default {
this.appendToFile(this.appendFilePath, bf);
}
saveToDisk() {
if (!this.changesSinceLastBackup) {
return;
}
this.flushingToDisk = true;
try {
let data = new Array();
Expand All @@ -212,10 +216,12 @@ class PersistentNodeCache extends node_cache_1.default {
}
finally {
this.flushingToDisk = false;
this.changesSinceLastBackup = false;
this.emitter.emit('done');
}
}
appendToFile(fileName, data) {
this.changesSinceLastBackup = true;
const flags = fs_1.default.constants.O_WRONLY | fs_1.default.constants.O_DIRECT | fs_1.default.constants.O_APPEND;
const mode = 0o666;
if (this.appendFileDescriptor) {
Expand Down
8 changes: 8 additions & 0 deletions src/persistentNodeCache.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ export default class PersistentNodeCache extends NodeCache {
private flushingToDisk: boolean;
private appendFileDescriptor: any;
private serializer: CacheSerializer;
private changesSinceLastBackup: boolean;

constructor(cacheName: string, period?: number, dir?: string, opts?: any, serializer?: CacheSerializer) {
super(opts);
Expand Down Expand Up @@ -58,6 +59,7 @@ export default class PersistentNodeCache extends NodeCache {
}
this.serializer = customSerializer;
}
this.changesSinceLastBackup = false;
super.on("expired", (key, _) => { this.appendExpiredEvent(key) });
}

Expand Down Expand Up @@ -179,6 +181,10 @@ export default class PersistentNodeCache extends NodeCache {
}

private saveToDisk(): void {
if(!this.changesSinceLastBackup) {
return;
}

this.flushingToDisk = true;
try {
let data: Array<ValueSetItem> = new Array<ValueSetItem>();
Expand All @@ -202,11 +208,13 @@ export default class PersistentNodeCache extends NodeCache {
}
finally {
this.flushingToDisk = false;
this.changesSinceLastBackup = false;
this.emitter.emit('done');
}
}

private appendToFile(fileName: string, data: Buffer): void {
this.changesSinceLastBackup = true;
const flags = fs.constants.O_WRONLY | fs.constants.O_DIRECT | fs.constants.O_APPEND;
const mode = 0o666;

Expand Down

0 comments on commit 9c75610

Please sign in to comment.