优化配置文件缓存目录

Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
zgf 2024-06-28 10:34:28 +08:00
parent 414e335a3a
commit 75495ccac9
3 changed files with 25 additions and 9 deletions

View File

@ -175,7 +175,7 @@ export class ImageKnifeDispatcher {
requestSource: requestSource, requestSource: requestSource,
isWatchProgress: isWatchProgress, isWatchProgress: isWatchProgress,
memoryKey: memoryKey, memoryKey: memoryKey,
fileCacheFolder: ImageKnife.getInstance().getFileCache().getCacheFolder() fileCacheFolder: ImageKnife.getInstance().getFileCache()?.getCacheFolder()
} }

View File

@ -60,13 +60,17 @@ export class FileCache {
if (this.isInited) { if (this.isInited) {
return return
} }
if (this.context && path.startsWith(this.context.cacheDir) === true) { if( path.includes("/") ) {
this.path = path if ( await FileUtils.getInstance().createFolder(path+"/")) {
throw Error("Failed to create the directory")
} else {
this.path = path
}
} else { } else {
FileCache.CACHE_FOLDER = path FileCache.CACHE_FOLDER = path
this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR this.path = this.context?.cacheDir + FileUtils.SEPARATOR + FileCache.CACHE_FOLDER + FileUtils.SEPARATOR
await FileUtils.getInstance().createFolder(this.path)
} }
await FileUtils.getInstance().createFolder(this.path)
// 遍历缓存目录下的文件,按照时间顺序加入缓存 // 遍历缓存目录下的文件,按照时间顺序加入缓存
let filenames: string[] = await FileUtils.getInstance().ListFile(this.path) let filenames: string[] = await FileUtils.getInstance().ListFile(this.path)
@ -112,7 +116,11 @@ export class FileCache {
} }
public getCacheFolder(): string { public getCacheFolder(): string {
return FileCache.CACHE_FOLDER if(this.context && this.path.startsWith(this.context.cacheDir) == true) {
return FileCache.CACHE_FOLDER
} else {
return this.path
}
} }
// 添加缓存键值对,同时写文件 // 添加缓存键值对,同时写文件
@ -254,9 +262,11 @@ export class FileCache {
* @param value * @param value
*/ */
static saveFileCacheOnlyFile(context: Context, key: string, value: ArrayBuffer, folder: string = FileCache.CACHE_FOLDER): boolean { static saveFileCacheOnlyFile(context: Context, key: string, value: ArrayBuffer, folder: string = FileCache.CACHE_FOLDER): boolean {
let path = context.cacheDir + FileUtils.SEPARATOR + folder
path = folder.includes("/") ? folder : path
// 写文件 // 写文件
FileUtils.getInstance() FileUtils.getInstance()
.writeFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key, value) .writeFileSync(path + FileUtils.SEPARATOR + key, value)
return true return true
} }
@ -267,9 +277,11 @@ export class FileCache {
* @returns * @returns
*/ */
static getFileCacheByFile(context: Context, key: string, folder: string = FileCache.CACHE_FOLDER): ArrayBuffer | undefined { static getFileCacheByFile(context: Context, key: string, folder: string = FileCache.CACHE_FOLDER): ArrayBuffer | undefined {
let path = context.cacheDir + FileUtils.SEPARATOR + folder
path = folder.includes("/") ? folder : path
// 从文件获取查看是否有缓存 // 从文件获取查看是否有缓存
return FileUtils.getInstance() return FileUtils.getInstance()
.readFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key) .readFileSync(path + FileUtils.SEPARATOR + key)
} }
/** /**

View File

@ -15,7 +15,11 @@
import { ImageKnife } from '@ohos/imageknife' import { ImageKnife } from '@ohos/imageknife'
import common from '@ohos.app.ability.common' import common from '@ohos.app.ability.common'
export class InitImageKnife{ export class InitImageKnife{
static async init(entryContext:common.UIAbilityContext){ static async init(entryContext:common.UIAbilityContext,flag: boolean = true){
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache") if ( flag ) {
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache")
} else {
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"/data/storage/el2/100/hap/files/ImageKnife")
}
} }
} }