ios缓存大小, 清除缓存 成 封装工具

    xiaoxiao2021-03-25  112

    今日开发的资讯类APP, 应用在处理网络资源时,做了资讯数据缓存,其中以图片缓存最为典型,其中很流行的离线缓存框架为SDWebImage。

    但是,离线缓存会占用手机存储空间,所以缓存清理功能基本成为资讯、购物、阅读类app的标配功能。

    在开发中将读取 和 清除的功能封装成了工具类,方便调用;

    其中, JClearCacheTool.h 中,代码如下:

    #import <Foundation/Foundation.h> @interface JFileManagerTool : NSObject //计算目录大小: + (float)folderSize; // 清除缓存 + (void)clearFile; @end

    .h文件中很简单, 一个获取文件夹的大小,一个是清除.

    然后 就是 JClearCacheTool.m 中:

    #import "JFileManagerTool.h" @implementation JFileManagerTool //计算目录大小: + (float)folderSize{ NSFileManager *fileManager = [NSFileManager defaultManager]; NSString *cachPath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0]; float folderSize = 0; if ([fileManager fileExistsAtPath:cachPath]) { NSArray *childerFiles = [fileManager subpathsAtPath:cachPath]; for (NSString *fileName in childerFiles) { NSString *absolutePath=[cachPath stringByAppendingPathComponent:fileName]; folderSize += [self fileSizeAtPath:absolutePath]; } folderSize += [[SDImageCache sharedImageCache] getSize]/1024.0/1024.0; // 加上 SDImageCache 的图片缓存 } return folderSize; } //计算单个文件大小: +(float)fileSizeAtPath:(NSString *)path{ NSFileManager *fileManager=[NSFileManager defaultManager]; if([fileManager fileExistsAtPath:path]){ long long size = [fileManager attributesOfItemAtPath:path error:nil].fileSize; return size/1024.0/1024.0; } return 0; } // 清除缓存 + (void)clearFile { NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject]; NSArray * files = [[NSFileManager defaultManager ]subpathsAtPath:cachePath]; for ( NSString * p in files) { NSError * error = nil ; //获取文件全路径 NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent:p]; if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) { [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error]; } } [[SDImageCache sharedImageCache] cleanDisk]; //也清理一下图片 } @end

    以上是我个人封装的工具类, 如有不合适的地方, 欢迎指出, 感激不尽.

    下面这个是我网上搜到的方法,其实都差不多, 贴出来,大家看看

    1. 获取缓存文件的大小 -( float )readCacheSize { NSString *cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES) firstObject]; return [ self folderSizeAtPath :cachePath]; } 由于缓存文件存在沙箱中,我们可以通过NSFileManager API来实现对缓存文件大小的计算。 // 遍历文件夹获得文件夹大小,返回多少 M - ( float ) folderSizeAtPath:( NSString *) folderPath{ NSFileManager * manager = [NSFileManager defaultManager]; if (![manager fileExistsAtPath :folderPath]) return 0 ; NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath :folderPath] objectEnumerator]; NSString * fileName; long long folderSize = 0 ; while ((fileName = [childFilesEnumerator nextObject]) != nil ){ //获取文件全路径 NSString * fileAbsolutePath = [folderPath stringByAppendingPathComponent :fileName]; folderSize += [ self fileSizeAtPath :fileAbsolutePath]; } return folderSize/( 1024.0 * 1024.0); } // 计算 单个文件的大小 - ( long long ) fileSizeAtPath:( NSString *) filePath{ NSFileManager * manager = [NSFileManager defaultManager]; if ([manager fileExistsAtPath :filePath]){ return [[manager attributesOfItemAtPath :filePath error : nil] fileSize]; } return 0; } 2. 清除缓存 - (void)clearFile { NSString * cachePath = [NSSearchPathForDirectoriesInDomains (NSCachesDirectory , NSUserDomainMask , YES ) firstObject]; NSArray * files = [[NSFileManager defaultManager ] subpathsAtPath :cachePath]; //NSLog ( @"cachpath = %@" , cachePath); for ( NSString * p in files) { NSError * error = nil ; //获取文件全路径 NSString * fileAbsolutePath = [cachePath stringByAppendingPathComponent :p]; if ([[NSFileManager defaultManager ] fileExistsAtPath :fileAbsolutePath]) { [[NSFileManager defaultManager ] removeItemAtPath :fileAbsolutePath error :&error]; } } //读取缓存大小 float cacheSize = [self readCacheSize] *1024; self.cacheSize.text = [NSString stringWithFormat:@"%.2fKB",cacheSize]; }
    转载请注明原文地址: https://ju.6miu.com/read-18568.html

    最新回复(0)