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{ nsfilemanager *filemanager=[nsfilemanager Defaultmanager]; if ([FileManager Fileexistsatpath:path]) { long size=[filemanager attributesofitematpath:path error: Nil].filesize; return size/1024.0/1024.0; } 0;}
Calculate Directory Size
+ (float) Foldersizeatpath: (nsstring*) path{Nsfilemanager*filemanager=[nsfilemanager Defaultmanager];float foldersize; if ([FileManager Fileexistsatpath:path]) { Nsarray*childerfiles=[filemanager Subpathsatpath:path]; for (NSString*filename in Childerfiles) { NSString *absolutepath=[ Path Stringbyappendingpathcomponent:filename]; Foldersize +=[fileservice Filesizeatpath: Absolutepath]; }//sdwebimage the implementation of the framework's own compute cache Span class= "regexp" > Foldersize+=[[sdimagecache Sharedimagecache] Getsize]/1024.0/1024.0; return foldersize;} 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{Nsfilemanager*filemanager=[nsfilemanager Defaultmanager]; if ([FileManager Fileexistsatpath:path]) { nsarray *childerfiles=[filemanager Subpathsatpath:path]; For (nsstring *filename in childerfiles) { //If necessary, add conditions to filter out files that you do not want to delete Nsstrin G *absolutepath=[path Stringbyappendingpathcomponent:filename]; [FileManager removeitematpath:absolutepath error:nil]; } } [[Sdimagecache Sharedimagecache] cleandisk];}
Implementation results:
iOS Development-the implementation of the clean cache feature