优化主线程文件缓存里的读取文件操作,减少主线程耗时
Signed-off-by: madixin <madixin@huawei.com>
This commit is contained in:
parent
883357a8b0
commit
5d21da6dac
|
@ -1,5 +0,0 @@
|
|||
import MemoryLruCacheTest from './MemoryLruCache.test';
|
||||
|
||||
export default function testsuite() {
|
||||
MemoryLruCacheTest();
|
||||
}
|
|
@ -1,98 +0,0 @@
|
|||
import { describe, beforeAll, beforeEach, afterEach, afterAll, it, expect } from '@ohos/hypium';
|
||||
import image from '@ohos.multimedia.image';
|
||||
|
||||
import { ImageKnifeData } from '@ohos/imageword/src/main/ets/model/ImageKnifeData';
|
||||
import { MemoryLruCache } from '@ohos/imageword/src/main/ets/utils/MemoryLruCache';
|
||||
|
||||
export default function MemoryLruCacheTest() {
|
||||
describe('MemoryLruCacheTest',() => {
|
||||
// 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.
|
||||
});
|
||||
|
||||
// 测试基础put,get以及size功能
|
||||
it('assertBasicFunction', 0, async () => {
|
||||
let memoryCache: MemoryLruCache = new MemoryLruCache(3, 3 * 1024 * 1024);
|
||||
|
||||
let data: ImageKnifeData = await getNewImageKnifeData()
|
||||
memoryCache.put("aaa", data)
|
||||
memoryCache.put("bbb", data)
|
||||
memoryCache.put("ccc", data)
|
||||
|
||||
console.info(JSON.stringify(memoryCache.get("aaa")))
|
||||
console.info(memoryCache.size() + "")
|
||||
// expect(memoryCache.size()).assertEqual(3)
|
||||
console.info("1111111")
|
||||
// expect(memoryCache.get("aaa")).assertEqual(data)
|
||||
// expect(memoryCache.get("bbb")).assertEqual(data)
|
||||
// expect(memoryCache.get("ccc")).assertEqual(data)
|
||||
// expect(memoryCache.size()).assertEqual(3)
|
||||
//
|
||||
// memoryCache.remove("ccc")
|
||||
// memoryCache.remove("ddd")
|
||||
// expect(memoryCache.size()).assertEqual(2)
|
||||
//
|
||||
// memoryCache.removeAll()
|
||||
// expect(memoryCache.size()).assertEqual(0)
|
||||
});
|
||||
|
||||
// it('assertSizeLruFuction', 0, async () => {
|
||||
// let memoryCache: MemoryLruCache = new MemoryLruCache(3, 3 * 1024 * 1024);
|
||||
//
|
||||
// let data: ImageKnifeData = await getNewImageKnifeData()
|
||||
// memoryCache.put("aaa", data)
|
||||
// memoryCache.put("bbb", data)
|
||||
// memoryCache.put("ccc", data)
|
||||
// memoryCache.put("ddd", data)
|
||||
//
|
||||
// expect(memoryCache.get("aaa")).assertUndefined()
|
||||
// expect(memoryCache.get("bbb")).assertEqual(data)
|
||||
// memoryCache.put("eee", data)
|
||||
// expect(memoryCache.get("ccc")).assertUndefined()
|
||||
// expect(memoryCache.get("bbb")).assertEqual(data)
|
||||
// expect(memoryCache.get("ddd")).assertEqual(data)
|
||||
// expect(memoryCache.get("eee")).assertEqual(data)
|
||||
// });
|
||||
//
|
||||
// it('assertMemorySizeLruFuction', 0, () => {
|
||||
// // Defines a test case. This API supports three parameters: test case name, filter parameter, and test case function.
|
||||
// let a = 'abc';
|
||||
// let b = 'b';
|
||||
// // Defines a variety of assertion methods, which are used to declare expected boolean conditions.
|
||||
// expect(a).assertContain(b);
|
||||
// expect(a).assertEqual(a);
|
||||
// });
|
||||
});
|
||||
}
|
||||
|
||||
async function getNewImageKnifeData(): Promise<ImageKnifeData> {
|
||||
const color: ArrayBuffer = new ArrayBuffer(96); //96为需要创建的像素buffer大小,取值为:height * width *4
|
||||
let opts: image.InitializationOptions = {
|
||||
editable: true, pixelFormat: 3, size: {
|
||||
height: 4, width: 6
|
||||
}
|
||||
}
|
||||
let pixelmap: PixelMap = await image.createPixelMap(color, opts)
|
||||
let data: ImageKnifeData = {
|
||||
source: pixelmap,
|
||||
imageWidth: 5,
|
||||
imageHeight: 5
|
||||
}
|
||||
return data
|
||||
}
|
|
@ -29,7 +29,7 @@ export class FileCache {
|
|||
currentMemory: number = 0
|
||||
maxSize: number = 0
|
||||
path: string = ""
|
||||
private lruCache: util.LRUCache<string, string>
|
||||
private lruCache: util.LRUCache<string, number>
|
||||
private isInited: boolean = false
|
||||
private context?: Context
|
||||
readonly defaultMaxSize: number = 512;
|
||||
|
@ -86,17 +86,14 @@ export class FileCache {
|
|||
if (buf !== undefined) {
|
||||
// 处理数量超过size的场景,移除即将排除的文件
|
||||
if (this.lruCache.length == this.maxSize && !this.lruCache.contains(sortedCachefiles[i].file)) {
|
||||
let remove: string | undefined = this.lruCache.remove(this.lruCache.keys()[0])
|
||||
let remove: number | undefined = this.lruCache.remove(this.lruCache.keys()[0])
|
||||
if (remove !== undefined) {
|
||||
let buf: ArrayBuffer | undefined = FileUtils.getInstance().readFileSync(this.path + this.lruCache.keys()[0])
|
||||
if (buf !== undefined) {
|
||||
FileUtils.getInstance().deleteFile(this.path + this.lruCache.keys()[0])
|
||||
this.removeMemorySize(buf)
|
||||
}
|
||||
FileUtils.getInstance().deleteFile(this.path + this.lruCache.keys()[0])
|
||||
this.removeMemorySize(buf)
|
||||
}
|
||||
}
|
||||
|
||||
this.lruCache.put(sortedCachefiles[i].file, "")
|
||||
this.lruCache.put(sortedCachefiles[i].file, buf.byteLength)
|
||||
this.addMemorySize(buf)
|
||||
}
|
||||
}
|
||||
|
@ -121,7 +118,7 @@ export class FileCache {
|
|||
this.remove(key)
|
||||
}
|
||||
|
||||
let pre = this.lruCache.put(key, "")
|
||||
let pre = this.lruCache.put(key, value.byteLength)
|
||||
FileUtils.getInstance().writeDataSync(this.path + key, value)
|
||||
if (pre !== undefined) {
|
||||
this.addMemorySize(value)
|
||||
|
@ -143,11 +140,11 @@ export class FileCache {
|
|||
this.remove(this.lruCache.keys()[0])
|
||||
} else if (this.lruCache.contains(key)) {
|
||||
this.lruCache.remove(key)
|
||||
this.lruCache.put(key, "")
|
||||
this.lruCache.put(key, typeof value == "number" ? value : value.byteLength)
|
||||
return
|
||||
}
|
||||
|
||||
this.lruCache.put(key, "")
|
||||
this.lruCache.put(key, typeof value == "number" ? value : value.byteLength)
|
||||
this.addMemorySize(value)
|
||||
this.trimToSize()
|
||||
}
|
||||
|
@ -173,13 +170,10 @@ export class FileCache {
|
|||
return
|
||||
}
|
||||
|
||||
let remove: string | undefined = this.lruCache.remove(key)
|
||||
let remove: number | undefined = this.lruCache.remove(key)
|
||||
if (remove !== undefined) {
|
||||
let buf: ArrayBuffer | undefined = FileUtils.getInstance().readFileSync(this.path + key)
|
||||
if (buf !== undefined) {
|
||||
FileUtils.getInstance().deleteFile(this.path + key)
|
||||
this.removeMemorySize(buf)
|
||||
}
|
||||
this.removeMemorySize(remove)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -212,15 +206,18 @@ export class FileCache {
|
|||
let delkey = this.lruCache.keys()[0]
|
||||
let data: ArrayBuffer | undefined = FileUtils.getInstance().readFileSync(this.path + delkey)
|
||||
if (data !== undefined) {
|
||||
FileUtils.getInstance().deleteFileSync(this.path + delkey)
|
||||
FileUtils.getInstance().deleteFile(this.path + delkey)
|
||||
this.removeMemorySize(data)
|
||||
}
|
||||
this.lruCache.remove(delkey)
|
||||
}
|
||||
}
|
||||
|
||||
private removeMemorySize(value: ArrayBuffer): void {
|
||||
if (value != undefined) {
|
||||
private removeMemorySize(value: ArrayBuffer | number): void {
|
||||
if (typeof value == "number") {
|
||||
this.currentMemory -= value
|
||||
}
|
||||
else if (value != undefined) {
|
||||
this.currentMemory -= value.byteLength
|
||||
LogUtil.info("FileCache removeMemorySize: " + value.byteLength + " currentMemory:" + this.currentMemory)
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue