!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新增抗锯齿
|
||||||
- 修复图片缩放时出现重影
|
- 修复图片缩放时出现重影
|
||||||
|
|
|
@ -12,9 +12,9 @@
|
||||||
* See the License for the specific language governing permissions and
|
* See the License for the specific language governing permissions and
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
import {CustomMap} from './CustomMap'
|
import { CustomMap } from './CustomMap'
|
||||||
|
|
||||||
export class LruCache <K, V> {
|
export class LruCache<K, V> {
|
||||||
maxsize: number = 0
|
maxsize: number = 0
|
||||||
size: number = 0;
|
size: number = 0;
|
||||||
map: CustomMap<K, V> = new CustomMap<K, V>();
|
map: CustomMap<K, V> = new CustomMap<K, V>();
|
||||||
|
@ -39,7 +39,7 @@ export class LruCache <K, V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 移除键为key的缓存
|
// 移除键为key的缓存
|
||||||
remove(key: K): V | undefined{
|
remove(key: K): V | undefined {
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
throw new Error('key is null,checking the parameter');
|
throw new Error('key is null,checking the parameter');
|
||||||
}
|
}
|
||||||
|
@ -51,7 +51,7 @@ export class LruCache <K, V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 获取键为key的value
|
// 获取键为key的value
|
||||||
get(key: K): V|undefined {
|
get(key: K): V | undefined {
|
||||||
if (key == null) {
|
if (key == null) {
|
||||||
throw new Error('key is null,checking the parameter');
|
throw new Error('key is null,checking the parameter');
|
||||||
}
|
}
|
||||||
|
@ -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:是否删除
|
||||||
|
@ -96,12 +101,12 @@ export class LruCache <K, V> {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存数据数量
|
// 缓存数据数量
|
||||||
sizeLength(): number{
|
sizeLength(): number {
|
||||||
return this.size
|
return this.size
|
||||||
}
|
}
|
||||||
|
|
||||||
// 缓存数据最大值
|
// 缓存数据最大值
|
||||||
maxSize(): number{
|
maxSize(): number {
|
||||||
return this.maxsize
|
return this.maxsize
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,21 +124,20 @@ export class LruCache <K, V> {
|
||||||
this.trimToSize(-1)
|
this.trimToSize(-1)
|
||||||
}
|
}
|
||||||
|
|
||||||
print():string {
|
print(): string {
|
||||||
let printResult = '';
|
let printResult = '';
|
||||||
if (this.map.isEmpty()) {
|
if (this.map.isEmpty()) {
|
||||||
return printResult;
|
return printResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
this.map.each( (value, key, index) => {
|
this.map.each((value, key, index) => {
|
||||||
printResult +='LruCache:key=' + key + 'value= ' + value;
|
printResult += 'LruCache:key=' + key + 'value= ' + value;
|
||||||
})
|
})
|
||||||
return printResult;
|
return printResult;
|
||||||
}
|
}
|
||||||
|
|
||||||
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,14 +47,17 @@ 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);
|
||||||
let fileTypeUtil = new FileTypeUtil()
|
let fileTypeUtil = new FileTypeUtil()
|
||||||
let fileType = fileTypeUtil.getFileType(buffer);
|
let fileType = fileTypeUtil.getFileType(buffer);
|
||||||
if(fileType != null) {
|
if (fileType != null) {
|
||||||
this._mPixelMapHeader = fileType;
|
this._mPixelMapHeader = fileType;
|
||||||
}
|
}
|
||||||
callback.compressDataListener(buffer);
|
callback.compressDataListener(buffer);
|
||||||
|
@ -61,15 +65,16 @@ 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 {
|
||||||
if (!this._mPixelMapHeader) {
|
if (!this._mPixelMapHeader) {
|
||||||
return PixelMapFormat.NONE;
|
return PixelMapFormat.NONE;
|
||||||
}
|
}
|
||||||
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;
|
||||||
|
}
|
||||||
|
}
|
|
@ -13,27 +13,34 @@
|
||||||
* limitations under the License.
|
* limitations under the License.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import {IResourceFetch} from '../resourcemanage/IResourceFetch'
|
import { IResourceFetch } from '../resourcemanage/IResourceFetch'
|
||||||
import {ResourceTypeEts} from '../../imageknife/constants/ResourceTypeEts'
|
import { ResourceTypeEts } from '../../imageknife/constants/ResourceTypeEts'
|
||||||
import resourceManager from '@ohos.resourceManager';
|
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);
|
||||||
onCompleteFunction(arrayBuffer)
|
onCompleteFunction(arrayBuffer)
|
||||||
})
|
})
|
||||||
.catch( (err:BusinessError) => {
|
.catch((err: BusinessError) => {
|
||||||
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