HarmonyOS 鸿蒙Next 缓存清理不彻底
HarmonyOS 鸿蒙Next 缓存清理不彻底
使用鸿蒙的清理缓存,但是无法清理到0,代码如下
clearCache() {
let cacheDir = getContext(this).cacheDir;
console.info(cacheDir);
fileIo.listFile(cacheDir).then((filenames) => {
for (let i = 0; i < filenames.length; i++) {
let dirPath = cacheDir + '/' + filenames[i];
console.log(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);
});
}
}
完成后再次调用获取缓存方法
getCache(): Promise<number | null> {
return new Promise((resolve, reject) => {
storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
if (error) {
console.log(TAG, 'getCurrentBundleStats failed with error:' + JSON.stringify(error));
reject(null)
} else {
console.log(TAG, 'getCurrentBundleStats successfully:' + JSON.stringify(bundleStats));
console.log(TAG, 'appsize :' + bundleStats.appSize);
console.log(TAG, 'cacheSize :' + bundleStats.cacheSize);
console.log(TAG, 'dataSize :' + bundleStats.dataSize);
resolve(bundleStats.cacheSize)
}
});
})
}
显示cachesize还有几十兆,清理缓存代码中没有 fileIo.unlink(dirPath)调用后的日志,说明没有文件夹缓存对象吗还是代码错误了?
更多关于HarmonyOS 鸿蒙Next 缓存清理不彻底的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
可以参考:
import { fileIo, storageStatistics } from '@kit.CoreFileKit';
import { BusinessError } from '@kit.BasicServicesKit';
import common from '@ohos.app.ability.common';
import contextConstant from '@ohos.app.ability.contextConstant';
@Entry
@Component
struct ClearCache {
private context = this.getContext(this) as common.UIAbilityContext;
// 获取应用数据空间大小
getCache() {
storageStatistics.getCurrentBundleStats((error: BusinessError, bundleStats: storageStatistics.BundleStats) => {
if (error) {
console.error('getCurrentBundleStats failed with error:' + JSON.stringify(error));
} else {
console.info('getCurrentBundleStats successfully:' + JSON.stringify(bundleStats));
console.info('appsize :' + bundleStats.appSize);
console.info('cacheSize :' + bundleStats.cacheSize);
console.info('dataSize :' + bundleStats.dataSize);
}
});
}
// 清理缓存
clearCache(filepath: string) {
fileIo.listFiles(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);
});
}
}
})
}
// 获取缓存路径
getPath() {
// 获取原始密级
let area = this.context.area;
console.log('area', area)
let appArea = this.context.getApplicationContext().area
console.log('appArea', appArea)
// 暂时替换密级
this.context.area = contextConstant.AreaMode.EL2;
this.context.getApplicationContext().area = 1
let cacheDir01 = ''
let cacheDir02 = this.getContext(this).cacheDir
if (this.context.area === contextConstant.AreaMode.EL2) {
cacheDir01 = cacheDir02.replace('el2', 'el1')
} else if (this.context.area === contextConstant.AreaMode.EL1){
cacheDir01 = cacheDir02.replace('el1', 'el2')
}
console.log('cacheDir01', cacheDir01)
console.log('cacheDir02', cacheDir02)
let applicationContext = this.context.getApplicationContext(); // 获取应用上下文
let cacheDirEL1 = ''
let cacheDirEL2 = applicationContext.cacheDir;
if (applicationContext.area === 1) { // 沙箱路径为1代表el2,0代表el1
cacheDirEL1 = 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 = appArea
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');
}
}
});
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");
}
}
});
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");
}
}
});
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");
}
}
});
}
build() {
Column() {
Button('获取系统缓存大小')
.onClick(() => {
this.getCache();
})
Button('点击清理缓存')
.onClick(() => {
this.getPath()
})
}
}
}
更多关于HarmonyOS 鸿蒙Next 缓存清理不彻底的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html
我咋使用以上代码后,缓存值还剩0.5M,并没有完全清理到0呢?
另外,请问,如何在清除完缓存后,立即获取缓存值,而不是点击获取缓存按钮时才获取
看错了,所有的清理缓存的日志都走了,结果就是还剩几十兆,cachesize
针对HarmonyOS 鸿蒙Next缓存清理不彻底的问题,以下是专业解答:
HarmonyOS 鸿蒙Next系统在设计上优化了缓存管理机制,以提高设备的运行效率和用户体验。然而,如果出现缓存清理不彻底的情况,可能是由于系统缓存策略、应用缓存机制或用户操作习惯等多种因素导致。
首先,确保您使用的是系统自带的缓存清理工具或第三方经过官方认证的工具进行清理。这些工具通常能够识别并清除大部分系统缓存和应用缓存。
其次,检查是否有特定应用缓存清理不彻底。如果是这种情况,尝试在应用的设置中找到缓存清理选项,手动进行深度清理。
另外,重启设备也是解决缓存问题的一种有效方法。重启后,系统会重新分配内存和缓存资源,有助于解决缓存清理不彻底的问题。
如果以上方法均无法彻底清理缓存,可能是由于系统或应用存在bug。此时,建议您关注华为官方发布的系统更新和应用更新,及时升级以修复可能存在的问题。
如果问题依旧没法解决请联系官网客服,官网地址是:https://www.itying.com/category-93-b0.html。在这里,您可以获得更专业的技术支持和解决方案。