Mobile applications in the processing of network resources, generally do offline cache processing, which is the most typical picture cache, where the popular offline cache framework is sdwebimage.
However, offline caching takes up storage space on your phone, so the cache cleanup function is basically a standard feature for information, shopping, and reading apps.
The implementation of the offline caching feature introduced today is mainly divided into the cache file size acquisition, delete cache file implementation.
Get the size of the cache file
Because the cache file exists in the sandbox, we can use the Nsfilemanager API to calculate the size of the cache file.
Calculate a single File size
+ (float) Filesizeatpath: (NSString *) path{ *filemanager=[Nsfilemanager Defaultmanager]; if ([FileManager Fileexistsatpath:path]) { longlong size=[FileManager Attributesofitematpath:path error:nil].filesize; return size/1024.0/1024.0; } return 0 ;}
Calculate Directory Size
+(float) Foldersizeatpath: (NSString *) path{Nsfilemanager*filemanager=[Nsfilemanager Defaultmanager]; floatfoldersize; if([FileManager Fileexistsatpath:path]) {Nsarray*childerfiles=[FileManager Subpathsatpath:path]; for(NSString *filenameinchchilderfiles) {NSString*absolutepath=[path Stringbyappendingpathcomponent:filename]; Foldersize+=[Fileservice Filesizeatpath:absolutepath]; }
implementation of the//sdwebimage framework's own compute cache foldersize+=[[sdimagecache Sharedimagecache] getsize]/1024.0/1024.0; returnfoldersize; } return 0;}
Clean up cache files
Also using the Nsfilemanager API for file operations, the Sdwebimage framework itself implements the cleanup cache operation, which we can call directly.
+ (void) ClearCache: (NSString *) path{ *filemanager=[Nsfilemanager Defaultmanager]; if ([FileManager Fileexistsatpath:path]) { *childerfiles=[FileManager Subpathsatpath:path]; for inch childerfiles) { // If necessary, add conditions to filter out files that you do not want to delete NSString *absolutepath=[path stringbyappendingpathcomponent:filename]; [FileManager Removeitematpath:absolutepath Error:nil]; } [[Sdimagecache Sharedimagecache] cleandisk];}
Implementation results:
iOS development-The implementation of the clean cache feature