1、获取本机缓存
// 获取缓存
formatSize() {
plus.cache.calculate((size) => {
let sizeCache = parseInt(size);
if (sizeCache == 0) {
this.fileSizeString = "0B";
} else if (sizeCache < 1024) {
this.fileSizeString = sizeCache + "B";
} else if (sizeCache < 1048576) {
this.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
} else if (sizeCache < 1073741824) {
this.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
} else {
this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
}
});
},
2、清除缓存
clearStorage() {
uni.showModal({
title: '清除缓存',
content: '您确定要清除缓存吗?',
success: (res) => {
if (res.confirm) {
console.log('用户点击确定');
this.clearCache();
} else if (res.cancel) {
console.log('用户点击取消');
}
}
});
},
3、清理缓存
clearCache() {
let os = plus.os.name;
if (os == 'Android') {
let main = plus.android.runtimeMainActivity();
let sdRoot = main.getCacheDir();
let files = plus.android.invoke(sdRoot, "listFiles");
let len = files.length;
for (let i = 0; i < len; i++) {
let filePath = '' + files[i];
plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
if (entry.isDirectory) {
entry.removeRecursively((entry) => {
uni.showToast({
title: '缓存清理完成',
duration: 2000
});
this.formatSize();
}, (e) => {
console.log(e.message)
});
} else {
entry.remove();
}
}, (e) => {
console.log('文件路径读取失败')
});
}
} else { // ios
plus.cache.clear(() =>{
uni.showToast({
title: '缓存清理完成',
duration: 2000
});
this.formatSize();
});
}
}
本文只做记录学习使用;原文:https://blog.csdn.net/weixin_63515766/article/details/133676748
网友评论