add electron cache service
This commit is contained in:
parent
239e243ac3
commit
7d033bbe19
|
@ -12,6 +12,7 @@ import { logError } from './utils/logging';
|
||||||
import { ElectronFile } from './types';
|
import { ElectronFile } from './types';
|
||||||
import { getEncryptionKey, setEncryptionKey } from './utils/safeStorage';
|
import { getEncryptionKey, setEncryptionKey } from './utils/safeStorage';
|
||||||
import { clearElectronStore } from './utils/electronStore';
|
import { clearElectronStore } from './utils/electronStore';
|
||||||
|
import { openLocalCache } from './utils/cache';
|
||||||
|
|
||||||
// Patch the global WebSocket constructor to use the correct DevServer url
|
// Patch the global WebSocket constructor to use the correct DevServer url
|
||||||
const fixHotReloadNext12 = () => {
|
const fixHotReloadNext12 = () => {
|
||||||
|
@ -193,4 +194,5 @@ windowObject['ElectronAPIs'] = {
|
||||||
getEncryptionKey,
|
getEncryptionKey,
|
||||||
setEncryptionKey,
|
setEncryptionKey,
|
||||||
clearElectronStore,
|
clearElectronStore,
|
||||||
|
openLocalCache,
|
||||||
};
|
};
|
||||||
|
|
35
main/utils/cache.ts
Normal file
35
main/utils/cache.ts
Normal file
|
@ -0,0 +1,35 @@
|
||||||
|
import { ipcRenderer } from 'electron/renderer';
|
||||||
|
import path from 'path';
|
||||||
|
import { readFile, writeFile, existsSync, mkdir } from 'promise-fs';
|
||||||
|
|
||||||
|
const getCacheDir = async () => await ipcRenderer.invoke('get-path', 'cache');
|
||||||
|
|
||||||
|
export async function openLocalCache(cacheName: string) {
|
||||||
|
const cacheDir = await getCacheDir();
|
||||||
|
const cacheBucketDir = path.join(cacheDir, cacheName);
|
||||||
|
if (!existsSync(cacheBucketDir)) {
|
||||||
|
await mkdir(cacheBucketDir, { recursive: true });
|
||||||
|
}
|
||||||
|
return new DiskCache(cacheBucketDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
class DiskCache {
|
||||||
|
constructor(private cacheBucketDir: string) {}
|
||||||
|
|
||||||
|
async put(cacheKey: string, response: Response): Promise<void> {
|
||||||
|
const cachePath = path.join(this.cacheBucketDir, cacheKey);
|
||||||
|
await writeFile(
|
||||||
|
cachePath,
|
||||||
|
new Uint8Array(await response.arrayBuffer())
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
async match(cacheKey: string): Promise<Response> {
|
||||||
|
const cachePath = path.join(this.cacheBucketDir, cacheKey);
|
||||||
|
if (existsSync(cachePath)) {
|
||||||
|
return new Response(await readFile(cachePath));
|
||||||
|
} else {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -5,6 +5,7 @@ import {
|
||||||
Tray,
|
Tray,
|
||||||
Notification,
|
Notification,
|
||||||
safeStorage,
|
safeStorage,
|
||||||
|
app,
|
||||||
} from 'electron';
|
} from 'electron';
|
||||||
import { createWindow } from './createWindow';
|
import { createWindow } from './createWindow';
|
||||||
import { buildContextMenu } from './menuUtil';
|
import { buildContextMenu } from './menuUtil';
|
||||||
|
@ -82,4 +83,8 @@ export default function setupIpcComs(
|
||||||
ipcMain.handle('safeStorage-decrypt', (_, message) => {
|
ipcMain.handle('safeStorage-decrypt', (_, message) => {
|
||||||
return safeStorage.decryptString(message);
|
return safeStorage.decryptString(message);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
ipcMain.handle('get-path', (_, message) => {
|
||||||
|
return app.getPath(message);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue