add clearDiskCache API

This commit is contained in:
Abhinav 2022-08-11 09:56:56 +05:30
parent 8e03f5d899
commit c797dca835
2 changed files with 10 additions and 4 deletions

View file

@ -12,7 +12,7 @@ import { logError } from './utils/logging';
import { ElectronFile } from './types';
import { getEncryptionKey, setEncryptionKey } from './utils/safeStorage';
import { clearElectronStore } from './utils/electronStore';
import { openLocalCache } from './utils/cache';
import { openDiskCache, clearDiskCache } from './utils/cache';
// Patch the global WebSocket constructor to use the correct DevServer url
const fixHotReloadNext12 = () => {
@ -194,5 +194,6 @@ windowObject['ElectronAPIs'] = {
getEncryptionKey,
setEncryptionKey,
clearElectronStore,
openLocalCache,
openDiskCache,
clearDiskCache,
};

View file

@ -1,6 +1,6 @@
import { ipcRenderer } from 'electron/renderer';
import path from 'path';
import { readFile, writeFile, existsSync, mkdir } from 'promise-fs';
import { readFile, writeFile, existsSync, mkdir, rmSync } from 'promise-fs';
import crypto from 'crypto';
import DiskLRUService from './diskLRU';
@ -12,7 +12,7 @@ const getCacheDir = async () => {
return path.join(systemCacheDir, CACHE_DIR);
};
export async function openLocalCache(cacheName: string) {
export async function openDiskCache(cacheName: string) {
const cacheDir = await getCacheDir();
const cacheBucketDir = path.join(cacheDir, cacheName);
if (!existsSync(cacheBucketDir)) {
@ -21,6 +21,11 @@ export async function openLocalCache(cacheName: string) {
return new DiskCache(cacheBucketDir);
}
export async function clearDiskCache() {
const cacheDir = await getCacheDir();
rmSync(cacheDir, { recursive: true, force: true });
}
class DiskCache {
constructor(private cacheBucketDir: string) {}