mirror of
https://github.com/ragestudio/comty.git
synced 2025-06-11 03:24:16 +00:00
improve method
This commit is contained in:
parent
7613f67fef
commit
5c4d3ddd84
@ -1,42 +1,57 @@
|
|||||||
import fs from "fs"
|
import fs from "fs"
|
||||||
import path from "path"
|
|
||||||
|
|
||||||
export default class CacheService {
|
export default class CacheService {
|
||||||
watchingFiles = new Set()
|
intervalMaps = new Map()
|
||||||
|
|
||||||
static deletionInterval = 1000 * 60 * 5
|
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) {
|
appendToDeletion(filepath) {
|
||||||
// create a interval of 5 minutes to delete the file
|
// 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
|
// 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
|
// reset the interval until the file is not accessed for 5 minutes and then delete it
|
||||||
try {
|
try {
|
||||||
const createInterval = () => {
|
const createInterval = () => {
|
||||||
return setInterval(() => {
|
let interval = setInterval(async () => {
|
||||||
const stats = fs.statSync(filepath)
|
try {
|
||||||
|
await this.checkDeletionFilepath(filepath)
|
||||||
|
|
||||||
stats.atime = new Date(stats.atime)
|
this.intervalMaps.delete(filepath)
|
||||||
|
|
||||||
if (stats.atime.getTime() + CacheService.deletionInterval < Date.now()) {
|
if (!results) {
|
||||||
clearInterval(this.watchingFiles.get(filepath).interval)
|
this.appendToDeletion(filepath)
|
||||||
|
}
|
||||||
this.watchingFiles.delete(filepath)
|
} catch (error) {
|
||||||
|
return clearInterval(interval)
|
||||||
fs.promises.unlink(filepath)
|
|
||||||
} else {
|
|
||||||
console.log(`[${filepath}] was accessed in the last 5 minutes, resetting deletion interval`)
|
|
||||||
|
|
||||||
clearInterval(this.watchingFiles.get(filepath).interval)
|
|
||||||
|
|
||||||
this.watchingFiles.get(filepath).interval = createInterval()
|
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
|
return interval
|
||||||
}
|
}
|
||||||
|
|
||||||
this.watchingFiles.add({
|
this.intervalMaps.set(filepath, createInterval())
|
||||||
filepath,
|
|
||||||
interval: createInterval()
|
|
||||||
})
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error)
|
console.error(error)
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user