|
|
|
@ -70,6 +70,7 @@ export class FileCache {
|
|
|
|
|
interface CacheFileInfo {
|
|
|
|
|
file: string;
|
|
|
|
|
ctime: number;
|
|
|
|
|
size: number;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按照上次访问该文件的时间排序
|
|
|
|
@ -78,26 +79,25 @@ export class FileCache {
|
|
|
|
|
let stat: fs.Stat | undefined = await FileUtils.getInstance().Stat(this.path + filenames[i])
|
|
|
|
|
cachefiles.push({
|
|
|
|
|
file: filenames[i],
|
|
|
|
|
ctime: stat === undefined ? 0 : stat.ctime
|
|
|
|
|
ctime: stat === undefined ? 0 : stat.ctime,
|
|
|
|
|
size: stat?.size ?? 0
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
let sortedCachefiles: CacheFileInfo[] = cachefiles.sort((a, b) => a.ctime - b.ctime)
|
|
|
|
|
|
|
|
|
|
for (let i = 0; i < sortedCachefiles.length; i++) {
|
|
|
|
|
let buf: ArrayBuffer | undefined = await FileUtils.getInstance().readFile(this.path + sortedCachefiles[i].file)
|
|
|
|
|
if (buf !== undefined) {
|
|
|
|
|
// 处理数量超过size的场景,移除即将排除的文件
|
|
|
|
|
if (this.lruCache.length == this.maxSize && !this.lruCache.contains(sortedCachefiles[i].file)) {
|
|
|
|
|
let remove: number | undefined = this.lruCache.remove(this.lruCache.keys()[0])
|
|
|
|
|
if (remove !== undefined) {
|
|
|
|
|
FileUtils.getInstance().deleteFile(this.path + this.lruCache.keys()[0])
|
|
|
|
|
this.removeMemorySize(buf)
|
|
|
|
|
}
|
|
|
|
|
const fileSize: number = sortedCachefiles[i].size;
|
|
|
|
|
// 处理数量超过size的场景,移除即将排除的文件
|
|
|
|
|
if (this.lruCache.length == this.maxSize && !this.lruCache.contains(sortedCachefiles[i].file)) {
|
|
|
|
|
let remove: number | undefined = this.lruCache.remove(this.lruCache.keys()[0])
|
|
|
|
|
if (remove !== undefined) {
|
|
|
|
|
FileUtils.getInstance().deleteFile(this.path + this.lruCache.keys()[0])
|
|
|
|
|
this.removeMemorySize(fileSize)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.lruCache.put(sortedCachefiles[i].file, buf.byteLength)
|
|
|
|
|
this.addMemorySize(buf)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.lruCache.put(sortedCachefiles[i].file, fileSize)
|
|
|
|
|
this.addMemorySize(fileSize)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.trimToSize();
|
|
|
|
|