• 项目开发中关于 uniapp实现 Android和IOS获取App缓存,清除缓存功能


    新建按钮

    "circle" plain type="info" @click="clearStorage"><text style="color: #000;font-size: 32rpx;">当前缓存: {{fileSizeString}}, 点击清除缓存text>

    获取本机缓存

    1. // 获取缓存
    2. formatSize() {
    3. plus.cache.calculate((size) => {
    4. let sizeCache = parseInt(size);
    5. if (sizeCache == 0) {
    6. this.fileSizeString = "0B";
    7. } else if (sizeCache < 1024) {
    8. this.fileSizeString = sizeCache + "B";
    9. } else if (sizeCache < 1048576) {
    10. this.fileSizeString = (sizeCache / 1024).toFixed(2) + "KB";
    11. } else if (sizeCache < 1073741824) {
    12. this.fileSizeString = (sizeCache / 1048576).toFixed(2) + "MB";
    13. } else {
    14. this.fileSizeString = (sizeCache / 1073741824).toFixed(2) + "GB";
    15. }
    16. });
    17. },

    清除缓存

    1. // 清除缓存确认
    2. clearStorage() {
    3. uni.showModal({
    4. title: '清除缓存',
    5. content: '您确定要清除缓存吗?',
    6. success: (res) => {
    7. if (res.confirm) {
    8. console.log('用户点击确定');
    9. this.clearCache();
    10. } else if (res.cancel) {
    11. console.log('用户点击取消');
    12. }
    13. }
    14. });
    15. },
    16. // 清除缓存
    17. clearCache() {
    18. let os = plus.os.name;
    19. if (os == 'Android') {
    20. let main = plus.android.runtimeMainActivity();
    21. let sdRoot = main.getCacheDir();
    22. let files = plus.android.invoke(sdRoot, "listFiles");
    23. let len = files.length;
    24. for (let i = 0; i < len; i++) {
    25. let filePath = '' + files[i];
    26. plus.io.resolveLocalFileSystemURL(filePath, (entry) => {
    27. if (entry.isDirectory) {
    28. entry.removeRecursively((entry) => {
    29. uni.showToast({
    30. title: '缓存清理完成',
    31. duration: 2000
    32. });
    33. this.formatSize();
    34. }, (e) => {
    35. console.log(e.message)
    36. });
    37. } else {
    38. entry.remove();
    39. }
    40. }, (e) => {
    41. console.log('文件路径读取失败')
    42. });
    43. }
    44. } else { // ios
    45. plus.cache.clear(() =>{
    46. uni.showToast({
    47. title: '缓存清理完成',
    48. duration: 2000
    49. });
    50. this.formatSize();
    51. });
    52. }
    53. }

  • 相关阅读:
    前端入门(一)JavaScript语法、数据类型、运算、函数
    为什么延迟删除可以保证MYSQL 与redis的一致性?
    【高并发基础】Spring 事务传播级别及造成死锁的隐患分析
    HBase-shell命令
    第7章 文件读取操作
    国产化系统加密/国产化系统防泄密
    [WPF]原生TabControl控件实现拖拽排序功能
    全栈经验总结(不间断更新)
    Camera Hal OEM模块 ---- cmr_snapshot.c
    Shell入门
  • 原文地址:https://blog.csdn.net/qq_57162921/article/details/139980708