From caebb3ed8d52cfeb7b8d3bae11d710b4bcc80d38 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E4=BC=9Fx?= Date: Wed, 15 May 2024 17:11:47 +0800 Subject: [PATCH 1/2] =?UTF-8?q?ImageKnife=E6=8F=90=E4=BE=9B=E5=9B=BE?= =?UTF-8?q?=E7=89=87=E5=8A=A0=E8=BD=BD=E6=88=90=E5=8A=9F=E5=A4=B1=E8=B4=A5?= =?UTF-8?q?=E7=9A=84=E4=BA=8B=E4=BB=B6=E5=8D=95=E5=85=83=E6=B5=8B=E8=AF=95?= =?UTF-8?q?=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 任伟x --- .../src/ohosTest/ets}/test/ImageKnifeOption.test.ets | 6 +++--- entry/src/ohosTest/ets/test/List.test.ets | 2 ++ library/src/test/List.test.ets | 2 -- 3 files changed, 5 insertions(+), 5 deletions(-) rename {library/src => entry/src/ohosTest/ets}/test/ImageKnifeOption.test.ets (94%) diff --git a/library/src/test/ImageKnifeOption.test.ets b/entry/src/ohosTest/ets/test/ImageKnifeOption.test.ets similarity index 94% rename from library/src/test/ImageKnifeOption.test.ets rename to entry/src/ohosTest/ets/test/ImageKnifeOption.test.ets index 0f157cc..391a510 100644 --- a/library/src/test/ImageKnifeOption.test.ets +++ b/entry/src/ohosTest/ets/test/ImageKnifeOption.test.ets @@ -13,10 +13,10 @@ * limitations under the License. */ import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; -import { ImageKnifeOption} from '../main/ets/ImageKnifeOption'; +import { ImageKnifeOption } from "@ohos/imageknife" -export default function imageKnifeOptionTest() { - describe('imageKnifeOptionTest',() => { +export default function ImageKnifeOptionTest() { + describe('ImageKnifeOptionTest',() => { // Defines a test suite. Two parameters are supported: test suite name and test suite function. beforeAll(() => { // Presets an action, which is performed only once before all test cases of the test suite start. diff --git a/entry/src/ohosTest/ets/test/List.test.ets b/entry/src/ohosTest/ets/test/List.test.ets index 878bde1..f290d64 100644 --- a/entry/src/ohosTest/ets/test/List.test.ets +++ b/entry/src/ohosTest/ets/test/List.test.ets @@ -14,10 +14,12 @@ */ import DefaultJobQueueTest from './DefaultJobQueueTest.test'; import FileLruCacheTest from './FileLruCache.test'; +import ImageKnifeOptionTest from './ImageKnifeOption.test'; import MemoryLruCacheTest from './MemoryLruCache.test'; export default function testsuite() { MemoryLruCacheTest(); FileLruCacheTest(); DefaultJobQueueTest(); + ImageKnifeOptionTest(); } \ No newline at end of file diff --git a/library/src/test/List.test.ets b/library/src/test/List.test.ets index 11c3bc2..bb5b5c3 100644 --- a/library/src/test/List.test.ets +++ b/library/src/test/List.test.ets @@ -1,7 +1,5 @@ -import imageKnifeOptionTest from './ImageKnifeOption.test'; import localUnitTest from './LocalUnit.test'; export default function testsuite() { localUnitTest(); - imageKnifeOptionTest(); } \ No newline at end of file From 8c15816eeebedc10b47025bbd17bb09ae96d248c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E4=BB=BB=E4=BC=9Fx?= Date: Mon, 20 May 2024 10:28:53 +0800 Subject: [PATCH 2/2] =?UTF-8?q?ImageKnife=E6=8F=90=E4=BE=9B=E6=B8=85?= =?UTF-8?q?=E7=90=86=E7=BC=93=E5=AD=98=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 任伟x --- CHANGELOG.md | 1 + README.md | 2 + entry/src/main/ets/pages/Index.ets | 6 + entry/src/main/ets/pages/TestRemoveCache.ets | 126 ++++++++++++++++++ .../resources/base/profile/main_pages.json | 3 +- .../src/ohosTest/ets/test/ImageKnife.test.ets | 67 ++++++++++ entry/src/ohosTest/ets/test/List.test.ets | 2 + library/src/main/ets/ImageKnife.ets | 30 +++++ 8 files changed, 236 insertions(+), 1 deletion(-) create mode 100644 entry/src/main/ets/pages/TestRemoveCache.ets create mode 100644 entry/src/ohosTest/ets/test/ImageKnife.test.ets diff --git a/CHANGELOG.md b/CHANGELOG.md index 3715d2f..d74fa1b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,6 +1,7 @@ ## 3.0.0-rc.5 - 图片加载事件增加请求开始的回调,以及修复有缓存时,没有回调的bug - 修复对已销毁组件不再下发请求的逻辑 +- 提供清理缓存能力 ## 3.0.0-rc.4 - 支持hsp多包图片资源 diff --git a/README.md b/README.md index 5edb702..64cd223 100644 --- a/README.md +++ b/README.md @@ -185,6 +185,8 @@ ImageKnifeComponent({ ImageKnifeOption: | deleteHeader | key: string | 全局删除http请求头 | | setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 | | putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 | +| removeMemoryCache| url: string | ImageKnifeOption | 清理指定内存缓存 | +| removeFileCache | url: string | ImageKnifeOption | 清理指定磁盘缓存 | ## 约束与限制 diff --git a/entry/src/main/ets/pages/Index.ets b/entry/src/main/ets/pages/Index.ets index 4593079..a799b6a 100644 --- a/entry/src/main/ets/pages/Index.ets +++ b/entry/src/main/ets/pages/Index.ets @@ -114,6 +114,12 @@ struct Index { }) }) + Button('测试移除图片缓存接口').margin({top:10}).onClick(()=>{ + router.push({ + uri: 'pages/TestRemoveCache', + }); + }) + } .width('100%') diff --git a/entry/src/main/ets/pages/TestRemoveCache.ets b/entry/src/main/ets/pages/TestRemoveCache.ets new file mode 100644 index 0000000..5b40e48 --- /dev/null +++ b/entry/src/main/ets/pages/TestRemoveCache.ets @@ -0,0 +1,126 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { ImageKnifeComponent, ImageKnife, ImageKnifeOption, CacheStrategy } from '@ohos/imageknife' + +@Entry +@Component +struct TestRemoveCache { + @State imageKnifeOption: ImageKnifeOption = { + loadSrc: $r('app.media.startIcon'), + placeholderSrc: $r('app.media.loading'), + errorholderSrc:$r('app.media.failed') + } + @State source: PixelMap | string | Resource = $r("app.media.startIcon"); + @State source1: PixelMap | string | Resource = $r("app.media.startIcon"); + @State url: string = ''; + + build() { + Column() { + Flex() { + Button("预加载gif图").onClick(() => { + this.imageKnifeOption.loadSrc = "https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658"; + this.url = "https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658"; + }) + .margin({left:10}) + Button("内存缓存获取gif").onClick(() => { + ImageKnife.getInstance() + .getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658", + CacheStrategy.Memory) + .then((data) => { + this.source = data !== undefined ? data.source : $r("app.media.startIcon"); + }) + }) + .margin({left:10}) + Button("文件缓存获取gif").onClick(() => { + ImageKnife.getInstance() + .getCacheImage("https://gd-hbimg.huaban.com/e0a25a7cab0d7c2431978726971d61720732728a315ae-57EskW_fw658", + CacheStrategy.File) + .then((data) => { + this.source1 = data !== undefined ? data.source : $r("app.media.startIcon"); + }) + }) + .margin({left:10}) + } + + Flex() { + Button("预加载静态图").onClick(() => { + this.imageKnifeOption.loadSrc = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp'; + this.url = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp'; + }) + .margin({left:10}) + Button("内存缓存获取").onClick(() => { + ImageKnife.getInstance() + .getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp', + CacheStrategy.Memory) + .then((data) => { + this.source = data!.source; + }) + }) + .margin({left:10}) + Button("文件缓存获取").onClick(() => { + ImageKnife.getInstance() + .getCacheImage('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp', + CacheStrategy.File) + .then((data) => { + this.source1 = data!.source; + }) + }) + .margin({left:10}) + }.margin({ top: 10 }) + + Flex() { + Button("删除全部缓存").onClick(() => { + ImageKnife.getInstance() + .removeAllMemoryCache() + ImageKnife.getInstance() + .removeAllFileCache() + }) + .margin({left:5}) + Button("删除全部内存缓存").onClick(() => { + ImageKnife.getInstance() + .removeAllMemoryCache() + }) + .margin({left:5}) + Button("删除全部文件缓存").onClick(() => { + ImageKnife.getInstance() + .removeAllFileCache() + }) + .margin({left:5}) + }.margin({ top: 10 }) + + Flex() { + Button("删除自定义内存缓存").onClick(() => { + ImageKnife.getInstance() + .removeMemoryCache(this.url) + }) + .margin({left:20}) + Button("删除自定义文件缓存").onClick(() => { + ImageKnife.getInstance() + .removeFileCache(this.url) + }) + .margin({left:20}) + }.margin({ top: 10 }) + + ImageKnifeComponent({ + imageKnifeOption: this.imageKnifeOption + }).width(200).height(200).margin({ top: 10 }) + Image(this.source).margin({ top: 10 }) + .width(200).height(200) + Image(this.source1).margin({ top: 10 }) + .width(200).height(200) + } + .height('100%').width('100%') + } +} \ No newline at end of file diff --git a/entry/src/main/resources/base/profile/main_pages.json b/entry/src/main/resources/base/profile/main_pages.json index bfefb27..4d74d0d 100644 --- a/entry/src/main/resources/base/profile/main_pages.json +++ b/entry/src/main/resources/base/profile/main_pages.json @@ -15,6 +15,7 @@ "pages/ImageTransformation", "pages/ObjectFitPage", "pages/TestWriteCacheStage", - "pages/LoadStatePage" + "pages/LoadStatePage", + "pages/TestRemoveCache" ] } \ No newline at end of file diff --git a/entry/src/ohosTest/ets/test/ImageKnife.test.ets b/entry/src/ohosTest/ets/test/ImageKnife.test.ets new file mode 100644 index 0000000..8c6811f --- /dev/null +++ b/entry/src/ohosTest/ets/test/ImageKnife.test.ets @@ -0,0 +1,67 @@ +/* + * Copyright (C) 2024 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the 'License'); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an 'AS IS' BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium'; +import { ImageKnife, ImageKnifeOption } from '@ohos/imageknife'; +import { ImageKnifeRequestSource } from '@ohos/imageknife/src/main/ets/model/ImageKnifeData'; + +export default function ImageKnifeTest() { + describe('ImageKnifeTest', () => { + // Defines a test suite. Two parameters are supported: test suite name and test suite function. + beforeAll(() => { + // Presets an action, which is performed only once before all test cases of the test suite start. + // This API supports only one parameter: preset action function. + }); + beforeEach(() => { + // Presets an action, which is performed before each unit test case starts. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: preset action function. + }); + afterEach(() => { + // Presets a clear action, which is performed after each unit test case ends. + // The number of execution times is the same as the number of test cases defined by **it**. + // This API supports only one parameter: clear action function. + }); + afterAll(() => { + // Presets a clear action, which is performed after all test cases of the test suite end. + // This API supports only one parameter: clear action function. + }); + + it('removeMemoryCache', 0, async () => { + let a = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp'; + let option: ImageKnifeOption = { + loadSrc: 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp', + signature: '' + } + let key = ImageKnife.getInstance() + .getEngineKeyImpl() + .generateMemoryKey('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp', ImageKnifeRequestSource.SRC, option) + ImageKnife.getInstance() + .removeMemoryCache(a) + expect(ImageKnife.getInstance() + .loadFromMemoryCache(key)).assertEqual(undefined) + }); + + it('removeFileCache', 0, async () => { + let a = 'https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp'; + let key = ImageKnife.getInstance() + .getEngineKeyImpl() + .generateFileKey('https://hbimg.huabanimg.com/95a6d37a39aa0b70d48fa18dc7df8309e2e0e8e85571e-x4hhks_fw658/format/webp'); + ImageKnife.getInstance() + .removeFileCache(a) + expect(ImageKnife.getInstance() + .loadFromFileCache(key)).assertEqual(undefined) + }); + }); +} diff --git a/entry/src/ohosTest/ets/test/List.test.ets b/entry/src/ohosTest/ets/test/List.test.ets index f290d64..a730b72 100644 --- a/entry/src/ohosTest/ets/test/List.test.ets +++ b/entry/src/ohosTest/ets/test/List.test.ets @@ -16,10 +16,12 @@ import DefaultJobQueueTest from './DefaultJobQueueTest.test'; import FileLruCacheTest from './FileLruCache.test'; import ImageKnifeOptionTest from './ImageKnifeOption.test'; import MemoryLruCacheTest from './MemoryLruCache.test'; +import ImageKnifeTest from './ImageKnife.test'; export default function testsuite() { MemoryLruCacheTest(); FileLruCacheTest(); DefaultJobQueueTest(); ImageKnifeOptionTest(); + ImageKnifeTest(); } \ No newline at end of file diff --git a/library/src/main/ets/ImageKnife.ets b/library/src/main/ets/ImageKnife.ets index 6e01ae1..c43b172 100644 --- a/library/src/main/ets/ImageKnife.ets +++ b/library/src/main/ets/ImageKnife.ets @@ -106,6 +106,19 @@ export class ImageKnife { removeAllMemoryCache(): void { this.memoryCache.removeAll() } + /* + * 清除指定内存缓存 + * */ + removeMemoryCache(url: string | ImageKnifeOption) { + let imageKnifeOption = new ImageKnifeOption(); + if (typeof url == 'string') { + imageKnifeOption.loadSrc = url; + } else { + imageKnifeOption = url; + } + let key = this.getEngineKeyImpl().generateMemoryKey(imageKnifeOption.loadSrc, ImageKnifeRequestSource.SRC, imageKnifeOption); + this.memoryCache.remove(key); + } loadFromMemoryCache(key: string): ImageKnifeData | undefined { if (key !== "") { @@ -279,6 +292,23 @@ export class ImageKnife { await this.fileCache.removeAll() } } + /* + * 清除指定文件缓存 + * */ + removeFileCache(url: string | ImageKnifeOption) { + let imageKnifeOption: ImageKnifeOption; + if (url instanceof ImageKnifeOption) { + imageKnifeOption = url; + } else { + imageKnifeOption = { + loadSrc: url + }; + } + let key = this.getEngineKeyImpl().generateFileKey(imageKnifeOption.loadSrc, imageKnifeOption.signature); + if (this.fileCache !== undefined) { + this.fileCache.remove(key); + } + } saveWithoutWriteFile(key: string, bufferSize: number): void { this.fileCache?.putWithoutWriteFile(key, bufferSize)