Complete instance code for cache calculation and cleanup in iOS

  • 2021-12-13 17:23:57
  • OfStack

1. First of all, 1 cache in our project is divided into 2 large blocks, and 1 is 1 data cached by ourselves; There is also a picture file cache that we use SDWebImage, a third-party library, which gives us automatic cache

< 1 > How to calculate the cache size (mainly using the NSFileManager class provided by the system)

$1. Calculation of single file size


-(long long)fileSizeAtPath:(NSString *)path{
  NSFileManager *fileManager=[NSFileManager defaultManager];
  if([fileManager fileExistsAtPath:path]){
    long long size=[fileManager attributesOfItemAtPath:path error:nil].fileSize;
    return size;
  }
  return 0;
}

$2. Calculate the folder size (using the method provided in $1 above)


-(float)folderSizeAtPath:(NSString *)path{
  NSFileManager *fileManager=[NSFileManager defaultManager];
  NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  cachePath=[cachePath stringByAppendingPathComponent:path];
  long long folderSize=0;
  if ([fileManager fileExistsAtPath:cachePath])
  {
    NSArray *childerFiles=[fileManager subpathsAtPath:cachePath];
    for (NSString *fileName in childerFiles)
    {
      NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];
      long long size=[self fileSizeAtPath:fileAbsolutePath];
      folderSize += size;
      NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);

    }
    //SDWebImage Implementation of self-computing cache in framework 
    folderSize+=[[SDImageCache sharedImageCache] getSize];
    return folderSize/1024.0/1024.0;
  }
  return 0;
}

Where folderSize+= [[SDImageCache sharedImageCache] getSize]; This line of code is the method that SDWebImage gives us to calculate the local cache picture size. (Of course, the underlying implementation of this method is still done with NSFileManager.)

Using the above two methods together, we can calculate how much cache we generate in total.....

2. After calculating the cache, how to clear it? ?


// Is also using NSFileManager API Perform file operations, SDWebImage The framework has implemented the cache cleaning operation itself, which we can call directly. 
-(void)clearCache:(NSString *)path{
  NSString *cachePath=[NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];
  cachePath=[cachePath stringByAppendingPathComponent:path];

  NSFileManager *fileManager=[NSFileManager defaultManager];
  if ([fileManager fileExistsAtPath:cachePath]) {
    NSArray *childerFiles=[fileManager subpathsAtPath:cachePath];
    for (NSString *fileName in childerFiles) {
      // If necessary, add conditions to filter out files that you don't want to delete 
      NSString *fileAbsolutePath=[cachePath stringByAppendingPathComponent:fileName];
      NSLog(@"fileAbsolutePath=%@",fileAbsolutePath);
      [fileManager removeItemAtPath:fileAbsolutePath error:nil];
    }
  }
  [[SDImageCache sharedImageCache] cleanDisk];
}

It is clear above that 2 places have been cleared when changing storage, and 1 is our own cached folder; There is also the picture file cached by SDWebImage....


Related articles: