ImageKnife提供清理缓存能力
Signed-off-by: 任伟x <renwei79@h-partners.com>
This commit is contained in:
parent
caebb3ed8d
commit
8c15816eee
|
@ -1,6 +1,7 @@
|
||||||
## 3.0.0-rc.5
|
## 3.0.0-rc.5
|
||||||
- 图片加载事件增加请求开始的回调,以及修复有缓存时,没有回调的bug
|
- 图片加载事件增加请求开始的回调,以及修复有缓存时,没有回调的bug
|
||||||
- 修复对已销毁组件不再下发请求的逻辑
|
- 修复对已销毁组件不再下发请求的逻辑
|
||||||
|
- 提供清理缓存能力
|
||||||
|
|
||||||
## 3.0.0-rc.4
|
## 3.0.0-rc.4
|
||||||
- 支持hsp多包图片资源
|
- 支持hsp多包图片资源
|
||||||
|
|
|
@ -185,6 +185,8 @@ ImageKnifeComponent({ ImageKnifeOption:
|
||||||
| deleteHeader | key: string | 全局删除http请求头 |
|
| deleteHeader | key: string | 全局删除http请求头 |
|
||||||
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
|
| setEngineKeyImpl | IEngineKey | 全局配置缓存key生成策略 |
|
||||||
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
|
| putCacheImage | url: string, pixelMap: PixelMap, cacheType: CacheStrategy = CacheStrategy.Default, signature?: string | 写入内存磁盘缓存 |
|
||||||
|
| removeMemoryCache| url: string | ImageKnifeOption | 清理指定内存缓存 |
|
||||||
|
| removeFileCache | url: string | ImageKnifeOption | 清理指定磁盘缓存 |
|
||||||
|
|
||||||
## 约束与限制
|
## 约束与限制
|
||||||
|
|
||||||
|
|
|
@ -114,6 +114,12 @@ struct Index {
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
Button('测试移除图片缓存接口').margin({top:10}).onClick(()=>{
|
||||||
|
router.push({
|
||||||
|
uri: 'pages/TestRemoveCache',
|
||||||
|
});
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
.width('100%')
|
.width('100%')
|
||||||
|
|
||||||
|
|
|
@ -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%')
|
||||||
|
}
|
||||||
|
}
|
|
@ -15,6 +15,7 @@
|
||||||
"pages/ImageTransformation",
|
"pages/ImageTransformation",
|
||||||
"pages/ObjectFitPage",
|
"pages/ObjectFitPage",
|
||||||
"pages/TestWriteCacheStage",
|
"pages/TestWriteCacheStage",
|
||||||
"pages/LoadStatePage"
|
"pages/LoadStatePage",
|
||||||
|
"pages/TestRemoveCache"
|
||||||
]
|
]
|
||||||
}
|
}
|
|
@ -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)
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -16,10 +16,12 @@ import DefaultJobQueueTest from './DefaultJobQueueTest.test';
|
||||||
import FileLruCacheTest from './FileLruCache.test';
|
import FileLruCacheTest from './FileLruCache.test';
|
||||||
import ImageKnifeOptionTest from './ImageKnifeOption.test';
|
import ImageKnifeOptionTest from './ImageKnifeOption.test';
|
||||||
import MemoryLruCacheTest from './MemoryLruCache.test';
|
import MemoryLruCacheTest from './MemoryLruCache.test';
|
||||||
|
import ImageKnifeTest from './ImageKnife.test';
|
||||||
|
|
||||||
export default function testsuite() {
|
export default function testsuite() {
|
||||||
MemoryLruCacheTest();
|
MemoryLruCacheTest();
|
||||||
FileLruCacheTest();
|
FileLruCacheTest();
|
||||||
DefaultJobQueueTest();
|
DefaultJobQueueTest();
|
||||||
ImageKnifeOptionTest();
|
ImageKnifeOptionTest();
|
||||||
|
ImageKnifeTest();
|
||||||
}
|
}
|
|
@ -106,6 +106,19 @@ export class ImageKnife {
|
||||||
removeAllMemoryCache(): void {
|
removeAllMemoryCache(): void {
|
||||||
this.memoryCache.removeAll()
|
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 {
|
loadFromMemoryCache(key: string): ImageKnifeData | undefined {
|
||||||
if (key !== "") {
|
if (key !== "") {
|
||||||
|
@ -279,6 +292,23 @@ export class ImageKnife {
|
||||||
await this.fileCache.removeAll()
|
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 {
|
saveWithoutWriteFile(key: string, bufferSize: number): void {
|
||||||
this.fileCache?.putWithoutWriteFile(key, bufferSize)
|
this.fileCache?.putWithoutWriteFile(key, bufferSize)
|
||||||
|
|
Loading…
Reference in New Issue