鸿蒙清楚本地缓存
一.清除的话是需要清除4个文件夹的,如果没有清干净的话就是没有4个全清
·1.获取路径,并调用清除方法
async getPath() {// 获取原始密级let area = this.context.area;let appArea = this.context.getApplicationContext().area// 暂时替换密级this.context.area = contextConstant.AreaMode.EL2;this.context.getApplicationContext().area = 1let cacheDir01 = ''let cacheDir02 = getContext(this).cacheDirif (this.context.area === contextConstant.AreaMode.EL2) {cacheDir01 = cacheDir02.replace('el2', 'el1')} else if (this.context.area === contextConstant.AreaMode.EL1) {cacheDir01 = cacheDir02.replace('el1', 'el2')}let applicationContext = this.context.getApplicationContext(); // 获取应用上下文let cacheDirEL1 = ''let cacheDirEL2 = applicationContext.cacheDir;if (applicationContext.area === 1) { // 沙箱路径为1代表el2,0代表el1cacheDirEL1 = cacheDirEL2.replace('el2', 'el1')} else if (applicationContext.area === 0) {cacheDirEL1 = cacheDirEL2.replace('el1', 'el2')}console.log('cacheDirEL1', cacheDirEL1)console.log('cacheDirEL2', cacheDirEL2)// 还原密级this.context.area = area;this.context.getApplicationContext().area = appAreaawait fileIo.access(cacheDir01, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDir01)} else {console.info('cacheDir01', 'file not exists');}}});await fileIo.access(cacheDir02, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDir02)} else {console.info('cacheDir02', "file not exists");}}});await fileIo.access(cacheDirEL1, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDirEL1)} else {console.info('cacheDirEL1', "file not exists");}}});await fileIo.access(cacheDirEL2, (err: BusinessError, res: boolean) => {if (err) {console.error("access failed with error message: " + err.message + ", error code: " + err.code);} else {if (res) {this.clearCache(cacheDirEL2)} else {console.info('cacheDirEL2', "file not exists");}}});await this.getCache()promptAction.showToast({message: '清除缓存成功',duration: 2000,})}
2.清除方法
// 清理缓存clearCache(filepath: string) {fileIo.listFile(filepath).then((filenames) => {for (let i = 0; i < filenames.length; i++) {let dirPath = filepath + '/' + filenames[i];console.log('dirPath', dirPath);// 判断是否为文件夹let isDirectory: boolean = false;try {isDirectory = fileIo.statSync(dirPath).isDirectory();} catch (e) {console.error(JSON.stringify(e));}if (isDirectory) {fileIo.rmdirSync(dirPath);} else {fileIo.unlink(dirPath).then(() => {console.info('remove file succeed');}).catch((err: Error) => {console.error('remove file failed with error message: ' + err.message);});}}})}
3.页面调用
this.getPath()
4. 获取本地缓存
这里的单位转为M了
getCache() {storageStatistics.getCurrentBundleStats((err: BusinessError, bundleStats: storageStatistics.BundleStats) => {if (err) {console.error(`Invoke getCurrentBundleStats failed, code is ${err.code}, message is ${err.message}`);} else {console.info(`Invoke getCurrentBundleStats succeeded, appsize is ${bundleStats.cacheSize}`);this.mb = (bundleStats.cacheSize / (1024 * 1024)).toFixed(2); // 保留两位小数// 重新创建数组来触发UI更新}});}