Skip to content

A modern, self-hosted backup solution for secure, encrypted backups across local and cloud storage.

feat: add caching for snapshot data loading

towfiqiac286fc654d5
diff --git a/backend/src/jobs/tasks/CleanDownloadsTask.ts b/backend/src/jobs/tasks/CleanDownloadsTask.ts
index 0e7d29e..8ec5116 100644
--- a/backend/src/jobs/tasks/CleanDownloadsTask.ts
+++ b/backend/src/jobs/tasks/CleanDownloadsTask.ts
@@ -16,6 +16,7 @@ export class CleanDownloadsTask extends Task {
appPaths.getDownloadsDir(),
appPaths.getRestoresDir(),
appPaths.getProgressDir(),
+ appPaths.getCacheDir(),
];
for (const dir of dirsToClean) {
diff --git a/backend/src/managers/BaseSnapshotManager.ts b/backend/src/managers/BaseSnapshotManager.ts
index 92908e8..dadfb8f 100644
--- a/backend/src/managers/BaseSnapshotManager.ts
+++ b/backend/src/managers/BaseSnapshotManager.ts
@@ -4,6 +4,8 @@ import { generateResticRepoPath, resticPathToWindows } from '../utils/restic/hel
import { DownloadHandler } from './handlers/DownloadHandler';
import { configService } from '../services/ConfigService';
import { SnapShotFile } from '../types/restic';
+import { appPaths } from '../utils/AppPaths';
+import { readFile, writeFile } from 'fs/promises';
export class BaseSnapshotManager extends EventEmitter {
private downloadHandler: DownloadHandler;
@@ -116,6 +118,15 @@ export class BaseSnapshotManager extends EventEmitter {
backupId: string,
options: { storagePath: string; storageName: string; encryption: boolean }
): Promise<{ success: boolean; result: SnapShotFile[] | string }> {
+ const filename = `backup-${backupId}-snapshot.json`;
+ const snapshotFilePath = `${appPaths.getCacheDir()}/${filename}`;
+
+ try {
+ const snapshotCache = await readFile(snapshotFilePath, 'utf8');
+ const files = JSON.parse(snapshotCache);
+ return { success: true, result: files };
+ } catch (error: any) {}
+
try {
let snapshotId: string | undefined;
const { success, result } = await getSnapshotByTag('backup-' + backupId, options);
@@ -157,6 +168,9 @@ export class BaseSnapshotManager extends EventEmitter {
isAvailable: true,
}));
+ // write the cache
+ await writeFile(snapshotFilePath, JSON.stringify(files), 'utf8');
+
return { success: true, result: files };
} catch (error: any) {
console.log('getSnapshotFiles error :', error);
diff --git a/backend/src/utils/AppPaths.ts b/backend/src/utils/AppPaths.ts
index 6cf423f..ecbb4bc 100644
--- a/backend/src/utils/AppPaths.ts
+++ b/backend/src/utils/AppPaths.ts
@@ -16,6 +16,7 @@ interface IAppPaths {
progress: string;
stats: string;
temp: string;
+ cache: string;
downloads: string;
restores: string;
sync: string;
@@ -118,6 +119,7 @@ class AppPaths {
progress: path.join(baseDir, 'progress'),
stats: path.join(baseDir, 'stats'),
temp: path.join(tempBase, 'pluton'),
+ cache: path.join(tempBase, 'pluton', 'cache'),
downloads: path.join(tempBase, 'pluton', 'downloads'),
restores: path.join(tempBase, 'pluton', 'restores'),
sync: path.join(baseDir, 'sync'),
@@ -132,6 +134,7 @@ class AppPaths {
this.paths.progress,
this.paths.stats,
this.paths.temp,
+ this.paths.cache,
this.paths.downloads,
this.paths.restores,
this.paths.sync,
@@ -210,6 +213,10 @@ class AppPaths {
this.checkInitialized();
return this.paths.downloads;
}
+ public getCacheDir(): string {
+ this.checkInitialized();
+ return this.paths.cache;
+ }
public getRestoresDir(): string {
this.checkInitialized();
return this.paths.restores;