1
0
mirror of https://github.com/ragestudio/comty.git synced 2025-06-19 23:44:15 +00:00
2023-05-30 12:49:08 +00:00

61 lines
1.7 KiB
JavaScript

import fs from "fs"
export default class CacheService {
intervalMaps = new Map()
static deletionInterval = 1000 * 60 * 5
checkDeletionFilepath(filepath) {
try {
const stats = fs.statSync(filepath)
stats.atime = new Date(stats.atime)
if (stats.atime.getTime() + CacheService.deletionInterval < Date.now()) {
fs.promises.unlink(filepath)
} else {
return false
}
return true
} catch (error) {
console.error(error)
fs.promises.unlink(filepath)
return true
}
}
appendToDeletion(filepath) {
// create a interval of 5 minutes to delete the file
// check the last time the file was accessed and if it was accessed in the last 5 minutes
// reset the interval until the file is not accessed for 5 minutes and then delete it
try {
const createInterval = () => {
let interval = setInterval(async () => {
try {
await this.checkDeletionFilepath(filepath)
this.intervalMaps.delete(filepath)
if (!results) {
this.appendToDeletion(filepath)
}
} catch (error) {
return clearInterval(interval)
}
})
return interval
}
this.intervalMaps.set(filepath, createInterval())
} catch (error) {
console.error(error)
return fs.promises.unlink(filepath)
}
}
}