From 9a8e17908aa77b01addd81727b074bd9550688ce Mon Sep 17 00:00:00 2001 From: Konv Suu <2583695112@qq.com> Date: Tue, 13 May 2025 15:55:38 +0800 Subject: [PATCH] feat: add cache size retrieval functionality and integrate with UI (#5689) * feat: add cache size retrieval functionality and integrate with UI * chore: clean * update * update --- package.json | 1 + packages/shared/IpcChannel.ts | 1 + src/main/ipc.ts | 17 ++++++++++++++++- src/main/utils/file.ts | 4 ++++ src/preload/index.ts | 1 + .../settings/DataSettings/DataSettings.tsx | 18 +++++++++++++++++- yarn.lock | 14 +++++++++++++- 7 files changed, 53 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 2b3483a952..3cc85ed935 100644 --- a/package.json +++ b/package.json @@ -86,6 +86,7 @@ "fast-xml-parser": "^5.2.0", "fetch-socks": "^1.3.2", "fs-extra": "^11.2.0", + "go-get-folder-size": "^0.5.5", "got-scraping": "^4.1.1", "jsdom": "^26.0.0", "markdown-it": "^14.1.0", diff --git a/packages/shared/IpcChannel.ts b/packages/shared/IpcChannel.ts index 1dd61c6364..dcb4f18656 100644 --- a/packages/shared/IpcChannel.ts +++ b/packages/shared/IpcChannel.ts @@ -1,4 +1,5 @@ export enum IpcChannel { + App_GetCacheSize = 'app:get-cache-size', App_ClearCache = 'app:clear-cache', App_SetLaunchOnBoot = 'app:set-launch-on-boot', App_SetLanguage = 'app:set-language', diff --git a/src/main/ipc.ts b/src/main/ipc.ts index ecb74a57b4..dd7a5ca3b3 100644 --- a/src/main/ipc.ts +++ b/src/main/ipc.ts @@ -8,6 +8,7 @@ import { IpcChannel } from '@shared/IpcChannel' import { Shortcut, ThemeMode } from '@types' import { BrowserWindow, ipcMain, nativeTheme, session, shell } from 'electron' import log from 'electron-log' +import { getFolderSizeBin } from 'go-get-folder-size' import { titleBarOverlayDark, titleBarOverlayLight } from './config' import AppUpdater from './services/AppUpdater' @@ -31,8 +32,9 @@ import { setOpenLinkExternal } from './services/WebviewService' import { windowService } from './services/WindowService' import { getResourcePath } from './utils' import { decrypt, encrypt } from './utils/aes' -import { getConfigDir, getFilesDir } from './utils/file' +import { getCacheDir, getConfigDir, getFilesDir } from './utils/file' import { compress, decompress } from './utils/zip' + const fileManager = new FileStorage() const backupManager = new BackupManager() const exportService = new ExportService(fileManager) @@ -179,6 +181,19 @@ export function registerIpc(mainWindow: BrowserWindow, app: Electron.App) { } }) + // get cache size + ipcMain.handle(IpcChannel.App_GetCacheSize, async () => { + const cachePath = getCacheDir() + const size = await getFolderSizeBin(cachePath, true, { + // ignore files that we can't access + loose: true + }).catch((err) => { + log.error('Failed to get cache size:', err) + }) + + return size || '0MB' + }) + // check for update ipcMain.handle(IpcChannel.App_CheckForUpdate, async () => { await appUpdater.checkForUpdates() diff --git a/src/main/utils/file.ts b/src/main/utils/file.ts index 25a4ed7323..f01a6d47bf 100644 --- a/src/main/utils/file.ts +++ b/src/main/utils/file.ts @@ -81,6 +81,10 @@ export function getConfigDir() { return path.join(os.homedir(), '.cherrystudio', 'config') } +export function getCacheDir() { + return path.join(app.getPath('userData'), 'Cache') +} + export function getAppConfigDir(name: string) { return path.join(getConfigDir(), name) } diff --git a/src/preload/index.ts b/src/preload/index.ts index eeea6ec3de..373cad4a24 100644 --- a/src/preload/index.ts +++ b/src/preload/index.ts @@ -23,6 +23,7 @@ const api = { ipcRenderer.invoke(IpcChannel.App_HandleZoomFactor, delta, reset), setAutoUpdate: (isActive: boolean) => ipcRenderer.invoke(IpcChannel.App_SetAutoUpdate, isActive), openWebsite: (url: string) => ipcRenderer.invoke(IpcChannel.Open_Website, url), + getCacheSize: () => ipcRenderer.invoke(IpcChannel.App_GetCacheSize), clearCache: () => ipcRenderer.invoke(IpcChannel.App_ClearCache), system: { getDeviceType: () => ipcRenderer.invoke(IpcChannel.System_GetDeviceType), diff --git a/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx b/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx index 4178ac9df7..32d6e0e926 100644 --- a/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx +++ b/src/renderer/src/pages/settings/DataSettings/DataSettings.tsx @@ -36,6 +36,7 @@ import YuqueSettings from './YuqueSettings' const DataSettings: FC = () => { const { t } = useTranslation() const [appInfo, setAppInfo] = useState() + const [cacheSize, setCacheSize] = useState('') const { size, removeAllFiles } = useKnowledgeFiles() const { theme } = useTheme() const [menu, setMenu] = useState('data') @@ -106,6 +107,7 @@ const DataSettings: FC = () => { useEffect(() => { window.api.getAppInfo().then(setAppInfo) + window.api.getCacheSize().then(setCacheSize) }, []) const handleOpenPath = (path?: string) => { @@ -130,6 +132,7 @@ const DataSettings: FC = () => { onOk: async () => { try { await window.api.clearCache() + await window.api.getCacheSize().then(setCacheSize) window.message.success(t('settings.data.clear_cache.success')) } catch (error) { window.message.error(t('settings.data.clear_cache.error')) @@ -228,7 +231,10 @@ const DataSettings: FC = () => { - {t('settings.data.clear_cache.title')} + + {t('settings.data.clear_cache.title')} + ({cacheSize}) +