From 915eded1abecfcdc6fdb6edc06d2ad9e1abb754e Mon Sep 17 00:00:00 2001 From: liuhaikang Date: Mon, 20 Jan 2025 15:34:07 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E6=96=87=E4=BB=B6=E7=BC=93?= =?UTF-8?q?=E5=AD=98=E5=88=9D=E5=A7=8B=E5=8C=96=E6=95=88=E7=8E=87?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: liuhaikang --- CHANGELOG.md | 1 + library/src/main/ets/cache/FileCache.ets | 26 ++++++++++++------------ 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d33e7f2..6f1962c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.2.1-rc.0 - Fix bug: CropTransformation is used to crop the original image - Fix bug: After calling the clear all file cache interfaces, the file cache becomes invalid +- Optimize the efficiency of file cache initialization. ## 3.2.0 - When successfully requesting the network, return the httpcode as well diff --git a/library/src/main/ets/cache/FileCache.ets b/library/src/main/ets/cache/FileCache.ets index 0cd1716..0759c0c 100644 --- a/library/src/main/ets/cache/FileCache.ets +++ b/library/src/main/ets/cache/FileCache.ets @@ -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();