优化getRequest方法中获取context和generateMemoryKey方法

Signed-off-by: zenggaofeng <zenggaofeng2@h-partners.com>
This commit is contained in:
zenggaofeng 2024-04-20 09:51:28 +08:00
parent f07c401dc2
commit 883357a8b0
3 changed files with 26 additions and 8 deletions

View File

@ -46,7 +46,7 @@ export class ImageKnifeDispatcher {
showFromMemomry(request: ImageKnifeRequest,imageSrc: string | PixelMap | Resource, requestSource: ImageKnifeRequestSource): boolean {
let memoryCache: ImageKnifeData | undefined = ImageKnife.getInstance()
.loadFromMemoryCache(Tools.generateKey(imageSrc))
.loadFromMemoryCache(Tools.generateMemoryKey(imageSrc))
if (memoryCache !== undefined) {
// 画主图
if (request.requestState === ImageKnifeRequestState.PROGRESS) {
@ -164,7 +164,7 @@ export class ImageKnifeDispatcher {
}
// 保存内存缓存
ImageKnife.getInstance().saveMemoryCache(key, ImageKnifeData)
ImageKnife.getInstance().saveMemoryCache(Tools.generateMemoryKey(imageSrc), ImageKnifeData)
if (requestList !== undefined) {

View File

@ -36,6 +36,7 @@ export struct ImageKnifeComponent {
private currentHeight: number = 0
private lastSrc: string | PixelMap | Resource = "";
private componentVersion: number = 0
private currentContext: common.UIAbilityContext | undefined = undefined
@State keyCanvas: KeyCanvas = {
keyId: util.generateRandomUUID()
}
@ -43,9 +44,9 @@ export struct ImageKnifeComponent {
aboutToAppear(): void {
let memoryCache: ImageKnifeData | undefined = ImageKnife.getInstance()
.loadFromMemoryCache(Tools.generateKey(this.ImageKnifeOption.loadSrc))
.loadFromMemoryCache(Tools.generateMemoryKey(this.ImageKnifeOption.loadSrc))
if (memoryCache !== undefined){
LogUtil.log("aboutToAppear load from memory cache for key = "+ Tools.generateKey(this.ImageKnifeOption.loadSrc))
LogUtil.log("aboutToAppear load from memory cache for key = "+ Tools.generateMemoryKey(this.ImageKnifeOption.loadSrc))
//画主图
this.pixelMap = memoryCache.source;
}else {
@ -82,13 +83,18 @@ export struct ImageKnifeComponent {
ImageKnife.getInstance().execute(this.getRequest(this.currentWidth, this.currentHeight))
}
}
getCurrentContext(): common.UIAbilityContext{
if(this.currentContext == undefined) {
this.currentContext = getContext(this) as common.UIAbilityContext
}
return this.currentContext
}
getRequest(width: number, height: number): ImageKnifeRequest {
if (this.request == undefined) {
this.lastSrc = this.ImageKnifeOption.loadSrc
this.request = new ImageKnifeRequest(
this.ImageKnifeOption,
this.ImageKnifeOption.context !== undefined ? this.ImageKnifeOption.context : getContext(this) as common.UIAbilityContext,
this.ImageKnifeOption.context !== undefined ? this.ImageKnifeOption.context : this.getCurrentContext(),
width,
height,
this.componentVersion,

View File

@ -13,11 +13,23 @@
* limitations under the License.
*/
import { SparkMD5 } from '../3rd_party/sparkmd5/spark-md5'
import util from '@ohos.util'
export class Tools {
private static keyCache: util.LRUCache<string,string> = new util.LRUCache(1024)
public static generateMemoryKey(key: string | PixelMap | Resource): string{
return typeof key == "string"? key : JSON.stringify(key)
}
// 生成唯一的key
public static generateKey(key: string | PixelMap | Resource): string{
return SparkMD5.hashBinary(JSON.stringify(key)) as string
let keyCache = typeof key == "string"? key : JSON.stringify(key)
let result = Tools.keyCache.get(keyCache)
if(result != undefined) {
return result
} else {
result = SparkMD5.hashBinary(keyCache)
Tools.keyCache.put(keyCache,result)
return result
}
}
}