iOS development-the implementation of the clean cache feature
Last Update:2015-07-24
Source: Internet
Author: User
<span id="Label3"></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">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.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">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.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">The implementation of the offline caching feature introduced today is mainly divided into the cache file size acquisition, Delete cache file Implementation.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px"><span style="margin:0px; padding:0px; font-size:16px">Get the size of the cache file</span></span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">Because the cache file exists in the sandbox, we can use the Nsfilemanager API to calculate the size of the cache File.</p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px">Calculate a single File size</span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><pre style="margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word; font-family:‘Courier New‘!important">+(<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">float</span></span>) Filesizeatpath: (nsstring *<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">) Path{nsfilemanager</span></span>*filemanager=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[nsfilemanager defaultmanager]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">if</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">([filemanager fileexistsatpath:path]) {</span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">Long</span></span> <span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">Long</span></span>Size=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[filemanager Attributesofitematpath:path error:nil].filesize; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span>size/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span>/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">; } </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span> <span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">;}</span></span></pre><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px">Calculate Directory Size</span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><pre style="font-family:‘Courier New‘!important; margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word">+(<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">float</span></span>) Foldersizeatpath: (nsstring *<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">) Path{nsfilemanager</span></span>*filemanager=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[nsfilemanager defaultmanager]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">float</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">foldersize; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">if</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">([filemanager fileexistsatpath:path]) {nsarray</span></span>*childerfiles=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[filemanager subpathsatpath:path]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"></span> for</span>(nsstring *filename<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">inch</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">childerfiles) {nsstring</span></span>*absolutepath=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[path stringbyappendingpathcomponent:filename]; Foldersize</span></span>+=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[fileservice filesizeatpath:absolutepath]; }<br style="margin:0px; padding:0px"> <span style="margin:0px; padding:0px; color:rgb(51,153,102); line-height:1.5!important">implementation of The//sdwebimage Framework's own compute cache</span> foldersize</span></span>+=[[sdimagecache sharedimagecache] getsize]/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span>/<span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">1024.0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">foldersize; } </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">return</span></span> <span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(128,0,128); line-height:1.5!important">0</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">;}</span></span></pre><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><br></span></span><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><br></span></span><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span style="font-family:verdana,‘courier new‘; font-size:14px; line-height:23px">implementation Results:</span></span></span><span style="font-family:verdana,courier new"><span style="font-family:verdana,courier new"><span style="font-size:14px; line-height:23px"><u><br></u></span></span></span><span style="font-family:verdana,courier new"><span style="font-family:verdana,courier new"><span style="font-size:14px; line-height:23px"><u><br></u></span></span></span><span class="cnblogs_code_copy" style="font-family:‘Courier New‘!important; margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="font-family:‘Courier New‘!important; margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"><p style="margin-top:0px; margin-bottom:0px; font-family:Helvetica"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px"><span style="margin:0px; padding:0px; font-size:16px">Clean up cache files</span></span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><span style="margin:0px; padding:0px; font-size:13px"><span style="margin:0px; padding:0px; font-size:14px"><span style="margin:0px; padding:0px; line-height:24px">Also using the Nsfilemanager API for file operations, The Sdwebimage framework itself implements the cleanup cache operation, which we can call Directly.</span></span></span></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"><p style="margin:10px auto; padding-top:0px; padding-bottom:0px; font-family:verdana,‘courier new‘; font-size:14px; line-height:23px"></p></p><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"><span class="cnblogs_code_copy" style="margin:0px; padding:0px 5px 0px 0px; line-height:1.5!important"></span></span><pre style="margin-top:0px; margin-bottom:0px; padding:0px; white-space:pre-wrap; word-wrap:break-word; font-family:‘Courier New‘!important">+(<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">void</span></span>) clearcache: (nsstring *<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">) Path{nsfilemanager</span></span>*filemanager=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[nsfilemanager defaultmanager]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">if</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">([filemanager fileexistsatpath:path]) {nsarray</span></span>*childerfiles=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[filemanager subpathsatpath:path]; </span></span><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"></span> for</span>(nsstring *filename<span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,0,255); line-height:1.5!important">inch</span></span><span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">childerfiles) { </span></span><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important">//</span></span><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important"><span style="margin:0px; padding:0px; color:rgb(0,128,0); line-height:1.5!important">if necessary, add conditions to filter out files that you do not want to delete</span></span>NSString *absolutepath=<span style="margin:0px; padding:0px; line-height:1.5!important"><span style="margin:0px; padding:0px; line-height:1.5!important">[path stringbyappendingpathcomponent:filename]; [filemanager Removeitematpath:absolutepath error:nil]; }} [[sdimagecache sharedimagecache] cleandisk];}</span></span></pre> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p> iOS development-the implementation of the clean cache feature </p> </p></span>