Skip to content

Commit

Permalink
perf: add code cache cleaner
Browse files Browse the repository at this point in the history
Signed-off-by: Innei <tukon479@gmail.com>
  • Loading branch information
Innei committed Nov 11, 2024
1 parent faae74b commit a0fd15f
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
3 changes: 2 additions & 1 deletion apps/main/src/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -61,6 +61,7 @@ export const initializeAppStage1 = () => {

registerPushNotifications()
clearCacheCronJob()
checkAndCleanCodeCache()
}

let contextMenuDisposer: () => void
Expand Down
22 changes: 22 additions & 0 deletions apps/main/src/lib/cleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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}`)
})
}
}

0 comments on commit a0fd15f

Please sign in to comment.