优化配置文件缓存目录
Signed-off-by: zgf <zenggaofeng2@h-partners.com>
This commit is contained in:
parent
414e335a3a
commit
75495ccac9
|
@ -175,7 +175,7 @@ export class ImageKnifeDispatcher {
|
|||
requestSource: requestSource,
|
||||
isWatchProgress: isWatchProgress,
|
||||
memoryKey: memoryKey,
|
||||
fileCacheFolder: ImageKnife.getInstance().getFileCache().getCacheFolder()
|
||||
fileCacheFolder: ImageKnife.getInstance().getFileCache()?.getCacheFolder()
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -60,13 +60,17 @@ export class FileCache {
|
|||
if (this.isInited) {
|
||||
return
|
||||
}
|
||||
if (this.context && path.startsWith(this.context.cacheDir) === true) {
|
||||
this.path = path
|
||||
if( path.includes("/") ) {
|
||||
if ( await FileUtils.getInstance().createFolder(path+"/")) {
|
||||
throw Error("Failed to create the directory")
|
||||
} else {
|
||||
this.path = path
|
||||
}
|
||||
} else {
|
||||
FileCache.CACHE_FOLDER = path
|
||||
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)
|
||||
|
||||
|
@ -112,7 +116,11 @@ export class FileCache {
|
|||
}
|
||||
|
||||
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
|
||||
*/
|
||||
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()
|
||||
.writeFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key, value)
|
||||
.writeFileSync(path + FileUtils.SEPARATOR + key, value)
|
||||
return true
|
||||
}
|
||||
|
||||
|
@ -267,9 +277,11 @@ export class FileCache {
|
|||
* @returns
|
||||
*/
|
||||
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()
|
||||
.readFileSync(context.cacheDir + FileUtils.SEPARATOR + folder + FileUtils.SEPARATOR + key)
|
||||
.readFileSync(path + FileUtils.SEPARATOR + key)
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -15,7 +15,11 @@
|
|||
import { ImageKnife } from '@ohos/imageknife'
|
||||
import common from '@ohos.app.ability.common'
|
||||
export class InitImageKnife{
|
||||
static async init(entryContext:common.UIAbilityContext){
|
||||
await ImageKnife.getInstance().initFileCache(entryContext, 256, 256 * 1024 * 1024,"ImageKnifeCache")
|
||||
static async init(entryContext:common.UIAbilityContext,flag: boolean = true){
|
||||
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")
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue