美文网首页
清理App的缓存

清理App的缓存

作者: 面糊 | 来源:发表于2016-06-01 00:12 被阅读128次

    一. 缓存的简单介绍

    1. App中的缓存, 都是存放在Caches文件夹中, 该文件夹专门用于保存App在运行时产生的, 需要持久化的数据. 随着App的不断使用, 网络数据的加载, 该文件会越来越大, 导致App占用的磁盘内存也越拉越大, 因此要定时清理
    2. 缓存的类型
      • 在使用App过程中缓存的图片
      • 使用WebView等浏览器控件产生的网页缓存
      • 使用了一些第三方框架或者自己写的, 将一些反复使用的数据做的缓存

    二. 清理方法一: 暴力清除缓存

    1. 该方法的清理方式比较暴力, 是直接获取Caches文件夹, 并且遍历该文件夹下的所有子目录(包括文件夹/文件), 然后清除所有的文件
    2. 该方法的步骤:
      • 使用异步并发队列, 遍历Caches文件夹下的所有路径

      • 使用NSFileManager来获取这些路径的文件属性

      • 通过文件属性, 计算所有文件尺寸的大小, 展示给用户当前的缓存大小

      • 直接删除遍历到的每个文件

          // 多次遍历使用异步并发队列, 暴力方法, 会将或得到的所有路径全部删除, 包括文件夹路径
          dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
              
              // 获取caches文件夹路径
              NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
              // 文件路径遍历器
              NSDirectoryEnumerator *fileEnumerator = [[NSFileManager defaultManager] enumeratorAtPath:cachePath];
              
              for (NSString *path in fileEnumerator) {
                  // 这里注意要做个判断, DS_Store是系统的文件, 没有必要删除他
                  if ([path containsString:@"DS_Store"]) continue;
                  
                  // 拼接文件路径
                  NSString *filePath = [cachePath stringByAppendingPathComponent:path];
                  // 获取文件的属性
                  NSDictionary *fileAttr = [[NSFileManager defaultManager] attributesOfItemAtPath:filePath error:nil];
                  // 取出文件的尺寸, 拼接
                  _totalSize += [fileAttr fileSize];
                  
                  // 保存所有的路径
                  [self.filePathArry addObject:filePath];
                  
                  // 删除Caches中的所有文件
                  [[NSFileManager defaultManager] removeItemAtPath:filePath error:nil];
              }
          });
        

    三. 清理方法二: 优雅的清理

    1. 与暴力清理方法的根本区别, 就是第二个方法不会连文件夹也一起清理掉

    2. 鄙人将获取文件夹尺寸和删除文件拆分为两个方法, 并且归于工具类使用

    3. 作为工具类, 为了更好的协调团队开发, 给别人使用的话最好在前面做一些防止异常的判断处理

       + (NSInteger)getSizeOfDirectoryPath:(NSString *)directoryPath {
           
           NSFileManager *manager = [NSFileManager defaultManager];
           
           // 1. 判断传入的路径是否为文件夹, 是否存在
           BOOL isDirectory = NO;
           BOOL isExists = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
           
           if (!isDirectory || !isExists) {
               NSException *exc = [NSException exceptionWithName:@"FilePathError" reason:@"传入的路径不合法" userInfo:nil];
               [exc raise];
           }
           
           // 2. 如果路径存在, 获取该路径下的所有文件
           NSInteger totalSize = 0;
           NSArray *subPathArray = [manager subpathsOfDirectoryAtPath:directoryPath error:nil];
           
           for (NSString *subPath in subPathArray) {
               
               NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
               NSDictionary *fileAttr = [manager attributesOfItemAtPath:filePath error:nil];
               totalSize += [fileAttr fileSize];
           }
           
           return totalSize;
       }
       
       + (void)removeAllItemInDirectoryPath:(NSString *)directoryPath {
           
           NSFileManager *manager = [NSFileManager defaultManager];
           
           BOOL isDirectory = NO;
           BOOL isExists = [manager fileExistsAtPath:directoryPath isDirectory:&isDirectory];
           
           if (!isDirectory || !isExists) {
               
               NSException *exc = [NSException exceptionWithName:@"FilePathError" reason:@"传入的路径不合法" userInfo:nil];
               [exc raise];
           }
           
           // 此方法会忽略文件夹, 只获取文件
           NSArray *subPathArray = [manager subpathsOfDirectoryAtPath:directoryPath error:nil];
           
           for (NSString *subPath in subPathArray) {
               NSString *filePath = [directoryPath stringByAppendingPathComponent:subPath];
               [manager removeItemAtPath:filePath error:nil];
           }
       }
      

    四. 额外补充

    补充一个用来展示当前缓存为MB还是KB的小玩意, 使用NSFileManager获取到的尺寸大小为bit, 所以要让用户理解的话, 还要自己进一步做个加工

    NSString *sizeStr = @"清除缓存";
    if (size > 1000 * 1000) {
    
        CGFloat sizeMB = size / 1000.0 / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%@(%.1fMB)", sizeStr, sizeMB];
    
    } else if (size > 1000) {
        
        CGFloat sizeKB = size / 1000.0;
        sizeStr = [NSString stringWithFormat:@"%@(%.1fKB)", sizeStr, sizeKB];
    } else if (size > 0) {
        
        sizeStr = [NSString stringWithFormat:@"%@(%ldB)", sizeStr, size];
    }

    相关文章

      网友评论

          本文标题:清理App的缓存

          本文链接:https://www.haomeiwen.com/subject/mehhdttx.html