From 95ffcb9e74f884973e2246df453e7ee2edcbec06 Mon Sep 17 00:00:00 2001 From: madixin Date: Wed, 6 Nov 2024 15:50:15 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E5=8D=95=E4=B8=AA=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E8=A7=A3=E7=A0=81=E5=90=8E=E5=86=85=E5=AD=98=E5=8D=A0?= =?UTF-8?q?=E7=94=A8=E8=B6=85=E8=BF=87=E5=86=85=E5=AD=98=E7=BC=93=E5=AD=98?= =?UTF-8?q?=E6=9C=80=E5=A4=A7=E5=80=BC=EF=BC=8C=E5=AF=BC=E8=87=B4=E6=97=A0?= =?UTF-8?q?=E6=B3=95=E6=98=BE=E7=A4=BA=E5=9B=BE=E7=89=87=E7=9A=84=E9=97=AE?= =?UTF-8?q?=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: madixin --- library/src/main/ets/cache/MemoryLruCache.ets | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/library/src/main/ets/cache/MemoryLruCache.ets b/library/src/main/ets/cache/MemoryLruCache.ets index 493fc11..aba330f 100644 --- a/library/src/main/ets/cache/MemoryLruCache.ets +++ b/library/src/main/ets/cache/MemoryLruCache.ets @@ -46,6 +46,11 @@ export class MemoryLruCache implements IMemoryCache { throw new Error('key or value is invalid '); } + let size = this.getImageKnifeDataSize(value) + if (size <= 0 && size >= this.maxMemory) { + return + } + // 如果size满了的话,需要按照LRU的方式删除第一个 if (this.lruCache.length == this.maxSize && !this.lruCache.contains(key)) { this.remove(this.lruCache.keys()[0]) @@ -53,12 +58,9 @@ export class MemoryLruCache implements IMemoryCache { this.remove(key) } - let pre: ImageKnifeData = this.lruCache.put(key, value) - this.addMemorySize(value) - // if (pre !== undefined) { // 当前返回不是key的之前value - // this.removeMemorySize(pre) - // } - this.trimToSize(); + this.lruCache.put(key, value) + this.currentMemory += size + this.trimToSize() } get(key: string): ImageKnifeData | undefined { @@ -119,19 +121,21 @@ export class MemoryLruCache implements IMemoryCache { } } - private addMemorySize(value: ImageKnifeData): void { + private getImageKnifeDataSize(value: ImageKnifeData): number { if (value.source != undefined) { if (typeof value.source === 'string' && value.source != "") { - this.currentMemory += value.source.length + return value.source.length } else if (value.source == "") { - for (let index = 0;index < value.imageAnimator!.length;index++) { + let size: number = 0 + for (let index = 0; index < value.imageAnimator!.length; index++) { let pixelMap = value.imageAnimator![index].src as PixelMap - this.currentMemory += pixelMap.getPixelBytesNumber() + size += pixelMap.getPixelBytesNumber() } + return size } else { - this.currentMemory += value.source.getPixelBytesNumber(); + return value.source.getPixelBytesNumber(); } - //LogUtil.log("MemoryCache addMemorySize: " + value.source.getPixelBytesNumber() + " currentMemory:" + this.currentMemory) } + return 0 } } \ No newline at end of file