perf: add code cache cleaner

Signed-off-by: Innei <tukon479@gmail.com>
This commit is contained in:
Innei 2024-11-11 23:31:32 +08:00
parent faae74b8ba
commit a0fd15f4ab
No known key found for this signature in database
GPG Key ID: 0F62D33977F021F7
2 changed files with 24 additions and 1 deletions

View File

@ -9,7 +9,7 @@ import { app, nativeTheme, Notification, shell } from "electron"
import contextMenu from "electron-context-menu"
import { getIconPath } from "./helper"
import { clearCacheCronJob } from "./lib/cleaner"
import { checkAndCleanCodeCache, clearCacheCronJob } from "./lib/cleaner"
import { t } from "./lib/i18n"
import { store } from "./lib/store"
import { updateNotificationsToken } from "./lib/user"
@ -61,6 +61,7 @@ export const initializeAppStage1 = () => {
registerPushNotifications()
clearCacheCronJob()
checkAndCleanCodeCache()
}
let contextMenuDisposer: () => void

View File

@ -144,3 +144,25 @@ export const clearCacheCronJob = () => {
timer = clearInterval(timer)
}
}
export const checkAndCleanCodeCache = async () => {
const cachePath = path.join(app.getPath("userData"), "Code Cache")
const size = await fastFolderSizeAsync(cachePath).catch((error) => {
logger.error(error)
})
if (!size) return
const threshold = 1024 * 1024 * 100 // 100MB
if (size > threshold) {
await fsp
.rm(cachePath, { force: true, recursive: true })
.then(() => {
logger.info(`Cleaned ${size} bytes code cache`)
})
.catch((error) => {
logger.error(`clean code cache failed: ${error.message}`)
})
}
}