add get cache data

Signed-off-by: tyBrave <tianyong21@h-partners.com>
This commit is contained in:
tyBrave 2024-10-12 09:57:11 +08:00
parent 78770a430c
commit 2a81312ed5
4 changed files with 168 additions and 1 deletions

View File

@ -159,6 +159,11 @@ struct Index {
uri: 'pages/TestTaskResourcePage', uri: 'pages/TestTaskResourcePage',
}); });
}) })
Button("测试缓存数据").margin({top:10}).onClick(()=>{
router.push({
uri: 'pages/TestCacheDataPage',
});
})
} }
} .width('100%') } .width('100%')
.height('100%') .height('100%')

View File

@ -0,0 +1,100 @@
import { ImageKnife, CacheStrategy, ImageKnifeComponent, ImageKnifeOption } from '@ohos/imageknife';
@Entry
@ComponentV2
struct TestCacheDataPage {
@Local cacheUpLimit: string = '当前缓存上限: 0M';
@Local currentNum: string = '当前缓存图片数量: 0';
@Local currentSize: string = '当前缓存的大小: 0M';
@Local currentWidth: number = 200
@Local currentHeight: number = 200
@Local ImageKnifeOption: ImageKnifeOption = new ImageKnifeOption({
loadSrc: "",
objectFit: ImageFit.Contain,
onLoadListener: {
onLoadFailed: (err) => {
console.error("Load Failed Reason: " + err);
},
onLoadSuccess: (data) => {
return data;
},
},
border: { radius: 50 }
})
aboutToAppear(): void {
ImageKnife.getInstance().initFileCache(getContext(this), 256, 256 * 1024 * 1024,"ImageKnifeCache1")
}
build() {
Column() {
ImageKnifeComponent(
{ imageKnifeOption: this.ImageKnifeOption })
.height(this.currentHeight)
.width(this.currentWidth)
.margin({ top: 10 })
Button('内存加载图片')
.onClick(() => {
this.ImageKnifeOption = new ImageKnifeOption({
loadSrc: "https://img0.baidu.com/it/u=1530797181,174436037&fm=253&fmt=auto&app=120&f=JPEG?w=1422&h=800",
objectFit: ImageFit.Contain,
writeCacheStrategy:CacheStrategy.Memory,
border: { radius: 50 },
})
})
Button('磁盘缓存加载图片')
.onClick(() => {
this.ImageKnifeOption = new ImageKnifeOption({
loadSrc: "https://q7.itc.cn/images01/20240223/ce80229bf9934dff97cdf2ad7be1dcb8.jpeg",
objectFit: ImageFit.Contain,
writeCacheStrategy:CacheStrategy.File,
border: { radius: 50 },
})
})
Text(this.cacheUpLimit).fontSize(20).margin({bottom:8});
Text(this.currentNum).fontSize(20).margin({bottom:8});
Text(this.currentSize).fontSize(20).margin({bottom:20});
Button("获取当前内存缓存上限").onClick(() => {
let result = ImageKnife.getInstance().getCacheUpperLimit(CacheStrategy.Memory);
if (result) {
this.cacheUpLimit = "当前缓存上限:" + (result/(1024*1024))+"M";
}
}).margin({bottom:8});
Button("获取当前内存缓存图片数量").onClick(() => {
let result = ImageKnife.getInstance().getCurrentPicturesNum(CacheStrategy.Memory);
if (result) {
this.currentNum = "当前缓存图片数量:" + result;
}
}).margin({bottom:8});
Button("获取当前缓存的大小").onClick(() => {
let result = ImageKnife.getInstance().getCurrentCacheSize(CacheStrategy.Memory);
if (result) {
this.currentSize = "当前缓存的大小:" + (result/(1024*1024))+"M";
}
}).margin({bottom:8});
Button("获取当前磁盘缓存上限").onClick(() => {
let result = ImageKnife.getInstance().getCacheUpperLimit(CacheStrategy.File);
if (result) {
this.cacheUpLimit = "当前缓存上限:" + (result/(1024*1024))+"M";
}
}).margin({bottom:8});
Button("获取当前磁盘缓存图片数量").onClick(() => {
let result = ImageKnife.getInstance().getCurrentPicturesNum(CacheStrategy.File);
if (result) {
this.currentNum = "当前缓存图片数量:" + result;
}
}).margin({bottom:8});
Button("获取当前磁盘的大小").onClick(() => {
let result = ImageKnife.getInstance().getCurrentCacheSize(CacheStrategy.File);
if (result) {
this.currentSize = "当前缓存的大小:" + (result/(1024*1024))+"M";
}
}).margin({bottom:8});
}
.height('100%').width('100%').margin({top:50})
}
}

View File

@ -22,6 +22,7 @@
"pages/ImageAnimatorPage", "pages/ImageAnimatorPage",
"pages/TestSetCustomImagePage", "pages/TestSetCustomImagePage",
"pages/TestErrorHolderPage", "pages/TestErrorHolderPage",
"pages/TestTaskResourcePage" "pages/TestTaskResourcePage",
"pages/TestCacheDataPage"
] ]
} }

View File

@ -302,6 +302,67 @@ export class ImageKnife {
return this.fileCache as FileCache return this.fileCache as FileCache
} }
/**
* get cache upper limit
* @param cacheType
* @returns
*/
getCacheUpperLimit(cacheType?: CacheStrategy): number | undefined {
if (cacheType == undefined || cacheType == CacheStrategy.Default) {
cacheType = CacheStrategy.Memory;
}
if (cacheType == CacheStrategy.Memory) {
return (this.memoryCache as MemoryLruCache).maxMemory;
} else {
console.log("sss---->isFileCacheInit:"+this.fileCache)
if (this.isFileCacheInit()) {
return this.fileCache?.maxMemory;
} else {
throw new Error("the disk cache not init");
}
}
}
/**
* gets the number of images currently cached
* @param cacheType
* @returns
*/
getCurrentPicturesNum(cacheType: CacheStrategy): number | undefined {
if (cacheType == undefined || cacheType == CacheStrategy.Default) {
cacheType = CacheStrategy.Memory;
}
if (cacheType == CacheStrategy.Memory) {
return (this.memoryCache as MemoryLruCache).size();
} else {
if (this.isFileCacheInit()) {
return this.fileCache?.size();
} else {
throw new Error("the disk cache not init");
}
}
}
/**
* gets the current cache size
* @param cacheType
* @returns
*/
getCurrentCacheSize(cacheType: CacheStrategy): number | undefined {
if (cacheType == undefined || cacheType == CacheStrategy.Default) {
cacheType = CacheStrategy.Memory;
}
if (cacheType == CacheStrategy.Memory) {
return (this.memoryCache as MemoryLruCache).currentMemory;
} else {
if (this.isFileCacheInit()) {
return this.fileCache?.currentMemory;
} else {
throw new Error("the disk cache not init");
}
}
}
private pixelMapToArrayBuffer(pixelMap: PixelMap): ArrayBuffer { private pixelMapToArrayBuffer(pixelMap: PixelMap): ArrayBuffer {
let imageInfo = pixelMap.getImageInfoSync(); let imageInfo = pixelMap.getImageInfoSync();