!101 moduleContext新增缓存策略,缓存上限5,缓存策略Lru
Merge pull request !101 from 明月清风/master
This commit is contained in:
commit
a44045dc0b
|
@ -1,6 +1,8 @@
|
||||||
## 2.1.2-rc.5
|
## 2.1.2-rc.5
|
||||||
|
- moduleContext新增缓存策略,缓存上限5,缓存策略Lru
|
||||||
- 适配DevEco Studio 4.1(4.1.3.415)--SDK:API11( 4.1.0.56)
|
- 适配DevEco Studio 4.1(4.1.3.415)--SDK:API11( 4.1.0.56)
|
||||||
|
|
||||||
|
|
||||||
## 2.1.2-rc.4
|
## 2.1.2-rc.4
|
||||||
- canvas新增抗锯齿
|
- canvas新增抗锯齿
|
||||||
- 修复图片缩放时出现重影
|
- 修复图片缩放时出现重影
|
||||||
|
|
|
@ -62,6 +62,11 @@ export class LruCache <K, V> {
|
||||||
return preValue
|
return preValue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// 是否存在key
|
||||||
|
has(key: K): boolean {
|
||||||
|
return this.map.hasKey(key);
|
||||||
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* 替换或删除对应key键的数据
|
* 替换或删除对应key键的数据
|
||||||
* evicted:是否删除
|
* evicted:是否删除
|
||||||
|
@ -135,5 +140,4 @@ export class LruCache <K, V> {
|
||||||
foreachLruCache(fn: (value: V, key: K, map: Map<K, V>) => void) {
|
foreachLruCache(fn: (value: V, key: K, map: Map<K, V>) => void) {
|
||||||
this.map.each(fn);
|
this.map.each(fn);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -20,6 +20,7 @@ import { ImageKnifeGlobal } from '../../ImageKnifeGlobal';
|
||||||
import resourceManager from '@ohos.resourceManager';
|
import resourceManager from '@ohos.resourceManager';
|
||||||
import { BusinessError } from '@ohos.base'
|
import { BusinessError } from '@ohos.base'
|
||||||
import common from '@ohos.app.ability.common';
|
import common from '@ohos.app.ability.common';
|
||||||
|
import { ContextCacheProxy } from '../../requestmanage/ContextCacheProxy';
|
||||||
|
|
||||||
export class RecourseProvider extends CompressAdapter {
|
export class RecourseProvider extends CompressAdapter {
|
||||||
private static CHARS: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
private static CHARS: string = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
|
||||||
|
@ -46,8 +47,11 @@ export class RecourseProvider extends CompressAdapter {
|
||||||
if (!this._mResourceData) {
|
if (!this._mResourceData) {
|
||||||
throw Error("compress resource is empty");
|
throw Error("compress resource is empty");
|
||||||
}
|
}
|
||||||
((ImageKnifeGlobal.getInstance()
|
let context = ContextCacheProxy.getInstance()
|
||||||
.getHapContext() as common.UIAbilityContext).createModuleContext(this._mResourceData.moduleName).resourceManager as resourceManager.ResourceManager)
|
.contextGetValue(this._mResourceData.moduleName,
|
||||||
|
ImageKnifeGlobal.getInstance().getHapContext() as common.UIAbilityContext);
|
||||||
|
if (context != undefined) {
|
||||||
|
(context.resourceManager as resourceManager.ResourceManager)
|
||||||
.getMediaContent(this._mResourceData.id)
|
.getMediaContent(this._mResourceData.id)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
let buffer = this.uint8ArrayToBuffer(data);
|
let buffer = this.uint8ArrayToBuffer(data);
|
||||||
|
@ -61,7 +65,7 @@ export class RecourseProvider extends CompressAdapter {
|
||||||
.catch((err: BusinessError) => {
|
.catch((err: BusinessError) => {
|
||||||
console.log("RecourseProvider openInternal err" + JSON.stringify(err as BusinessError));
|
console.log("RecourseProvider openInternal err" + JSON.stringify(err as BusinessError));
|
||||||
})
|
})
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
getPixelMapFormat(): PixelMapFormat | undefined {
|
getPixelMapFormat(): PixelMapFormat | undefined {
|
||||||
|
@ -70,6 +74,7 @@ export class RecourseProvider extends CompressAdapter {
|
||||||
}
|
}
|
||||||
return this.getFormat(this._mPixelMapHeader);
|
return this.getFormat(this._mPixelMapHeader);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* data decode
|
* data decode
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -0,0 +1,80 @@
|
||||||
|
/*
|
||||||
|
* 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 { ICache } from "../requestmanage/ICache"
|
||||||
|
import { LruCache } from "../../cache/LruCache"
|
||||||
|
|
||||||
|
export class ContextCacheProxy implements ICache<string, Context> {
|
||||||
|
private mLruCache: LruCache<string, Context>;
|
||||||
|
private static cache: ContextCacheProxy = new ContextCacheProxy(new LruCache(5));
|
||||||
|
|
||||||
|
private constructor(lruCache: LruCache<string, Context>) {
|
||||||
|
this.mLruCache = lruCache;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static getInstance(): ContextCacheProxy {
|
||||||
|
return ContextCacheProxy.cache;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 缓存类型
|
||||||
|
getName() {
|
||||||
|
return "ContextCacheProxy"
|
||||||
|
}
|
||||||
|
|
||||||
|
contextGetValue(key: string, context: Context): Context | undefined{
|
||||||
|
if (this.hasValue(key)) {
|
||||||
|
return this.getValue(key);
|
||||||
|
} else {
|
||||||
|
let moduleContext = context.createModuleContext(key);
|
||||||
|
this.putValue(key, moduleContext)
|
||||||
|
return moduleContext;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
getValue(key: string): Context | undefined {
|
||||||
|
return this.mLruCache.get(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
hasValue(key: string): boolean {
|
||||||
|
return this.mLruCache.has(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
putValue(key: string, value: Context) {
|
||||||
|
this.mLruCache.put(key, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
removeValue(key: string): Context | undefined {
|
||||||
|
return this.mLruCache.remove(key);
|
||||||
|
}
|
||||||
|
|
||||||
|
clear() {
|
||||||
|
this.mLruCache.evicAll();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
// 外界调用
|
||||||
|
loadMemoryCache(key: string, isMemoryCacheable: boolean): Context | null {
|
||||||
|
// 是否开启内存缓存
|
||||||
|
if (!isMemoryCacheable) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
let cached = this.getValue(key)
|
||||||
|
if (cached != null) {
|
||||||
|
return cached;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,12 +19,17 @@ import resourceManager from '@ohos.resourceManager';
|
||||||
import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
|
import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
|
||||||
import { BusinessError } from '@ohos.base'
|
import { BusinessError } from '@ohos.base'
|
||||||
import common from '@ohos.app.ability.common';
|
import common from '@ohos.app.ability.common';
|
||||||
|
import { ContextCacheProxy } from '../requestmanage/ContextCacheProxy';
|
||||||
|
|
||||||
export class ParseResClient implements IResourceFetch<ArrayBuffer> {
|
export class ParseResClient implements IResourceFetch<ArrayBuffer> {
|
||||||
loadResource(context: common.UIAbilityContext, res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike<ArrayBuffer>, onErrorFunction: (reason?: BusinessError | string) => void) {
|
loadResource(context: common.UIAbilityContext, res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike<ArrayBuffer>, onErrorFunction: (reason?: BusinessError | string) => void) {
|
||||||
let resId = res.id;
|
let resId = res.id;
|
||||||
let resType = res.type;
|
let resType = res.type;
|
||||||
if (resType == ResourceTypeEts.MEDIA) {
|
if (resType == ResourceTypeEts.MEDIA) {
|
||||||
(context.createModuleContext(res.moduleName).resourceManager as resourceManager.ResourceManager)
|
let moduleContext = ContextCacheProxy.getInstance()
|
||||||
|
.contextGetValue(res.moduleName, context);
|
||||||
|
if (moduleContext != undefined) {
|
||||||
|
(moduleContext.resourceManager as resourceManager.ResourceManager)
|
||||||
.getMediaContent(resId)
|
.getMediaContent(resId)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
let arrayBuffer = this.typedArrayToBuffer(data);
|
let arrayBuffer = this.typedArrayToBuffer(data);
|
||||||
|
@ -34,6 +39,8 @@ export class ParseResClient implements IResourceFetch<ArrayBuffer> {
|
||||||
onErrorFunction(err)
|
onErrorFunction(err)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
else if (resType == ResourceTypeEts.RAWFILE) {
|
else if (resType == ResourceTypeEts.RAWFILE) {
|
||||||
onErrorFunction('ParseResClient 本地资源是rawfile暂时无法解析出错')
|
onErrorFunction('ParseResClient 本地资源是rawfile暂时无法解析出错')
|
||||||
} else {
|
} else {
|
||||||
|
|
|
@ -20,13 +20,16 @@ import { BusinessError } from '@ohos.base'
|
||||||
import resourceManager from '@ohos.resourceManager';
|
import resourceManager from '@ohos.resourceManager';
|
||||||
import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
|
import { ImageKnifeGlobal } from '../ImageKnifeGlobal';
|
||||||
import common from '@ohos.app.ability.common'
|
import common from '@ohos.app.ability.common'
|
||||||
|
import { ContextCacheProxy } from '../requestmanage/ContextCacheProxy'
|
||||||
|
|
||||||
export class ParseResClientBase64 implements IResourceFetch<ArrayBuffer> {
|
export class ParseResClientBase64 implements IResourceFetch<ArrayBuffer> {
|
||||||
loadResource(context: common.UIAbilityContext, res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike<ArrayBuffer>, onErrorFunction: (reason?: BusinessError | string) => void) {
|
loadResource(context: common.UIAbilityContext, res: Resource, onCompleteFunction: (value: ArrayBuffer) => void | PromiseLike<ArrayBuffer>, onErrorFunction: (reason?: BusinessError | string) => void) {
|
||||||
let resId = res.id;
|
let resId = res.id;
|
||||||
let resType = res.type;
|
let resType = res.type;
|
||||||
if (resType == ResourceTypeEts.MEDIA) {
|
if (resType == ResourceTypeEts.MEDIA) {
|
||||||
(context.createModuleContext(res.moduleName).resourceManager as resourceManager.ResourceManager)
|
let moduleContext = ContextCacheProxy.getInstance()
|
||||||
|
.contextGetValue(res.moduleName, context);
|
||||||
|
(moduleContext.resourceManager as resourceManager.ResourceManager)
|
||||||
.getMediaContentBase64(resId)
|
.getMediaContentBase64(resId)
|
||||||
.then(data => {
|
.then(data => {
|
||||||
let matchReg = ';base64,';
|
let matchReg = ';base64,';
|
||||||
|
|
|
@ -26,6 +26,7 @@ import resourceManager from '@ohos.resourceManager'
|
||||||
import { BusinessError } from '@ohos.base'
|
import { BusinessError } from '@ohos.base'
|
||||||
import {Size} from '../../imageknife/RequestOption'
|
import {Size} from '../../imageknife/RequestOption'
|
||||||
import common from '@ohos.app.ability.common'
|
import common from '@ohos.app.ability.common'
|
||||||
|
import { ContextCacheProxy } from '../requestmanage/ContextCacheProxy'
|
||||||
|
|
||||||
export class MaskTransformation implements BaseTransform<PixelMap> {
|
export class MaskTransformation implements BaseTransform<PixelMap> {
|
||||||
private _mResourceData:Resource|undefined = undefined;
|
private _mResourceData:Resource|undefined = undefined;
|
||||||
|
@ -96,7 +97,10 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
|
||||||
let context = (request.getModuleContext() as common.UIAbilityContext)
|
let context = (request.getModuleContext() as common.UIAbilityContext)
|
||||||
if(context != undefined){
|
if(context != undefined){
|
||||||
if(this._mResourceData != undefined){
|
if(this._mResourceData != undefined){
|
||||||
let resourceManager = context.createModuleContext(this._mResourceData.moduleName).resourceManager as resourceManager.ResourceManager
|
let moduleContext = ContextCacheProxy.getInstance()
|
||||||
|
.contextGetValue(this._mResourceData.moduleName, context);
|
||||||
|
if (moduleContext != undefined) {
|
||||||
|
let resourceManager = moduleContext.resourceManager as resourceManager.ResourceManager
|
||||||
if(resourceManager != undefined && this._mResourceData != undefined)
|
if(resourceManager != undefined && this._mResourceData != undefined)
|
||||||
resourceManager.getMediaContent(this._mResourceData?.id)
|
resourceManager.getMediaContent(this._mResourceData?.id)
|
||||||
.then(array => {
|
.then(array => {
|
||||||
|
@ -118,6 +122,8 @@ export class MaskTransformation implements BaseTransform<PixelMap> {
|
||||||
func?.asyncTransform("MaskTransformation openInternal error" + err, null);
|
func?.asyncTransform("MaskTransformation openInternal error" + err, null);
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue