Clear the implementation code for the cache feature

Source: Internet
Author: User

Cache files stored in the Sandbox folder caches, the implementation of clearing the cache, the main implementation is to find files-delete files (which involves the calculation file size)

The following is the main code that implements the purge cache

Get Cache Path

-(NSString *) getcache{

NSString *cachepath = [Nssearchpathfordirectoriesindomains (nscachesdirectory, Nsuserdomainmask, YES) firstObject];

NSLog (@ "%@", CachePath);

return CachePath;

}

Get folder Size

-(CGFloat) foldersizeatpath{

Creating a File Manager

Nsfilemanager *manager = [Nsfilemanager Defaultmanager];

Get folder path

NSString *folderpath = [self getcache];

Returns 0 if the cache file does not exist

if (![ Manager Fileexistsatpath:folderpath]) {

return 0;

}

Calculate the size of the cache file if the cache file exists

Nsenumerator *childfilesenumerator = [[manager Subpathsatpath:folderpath] objectenumerator];//get enumerator from the file array in the cache path

NSString *filename = nil;//defines an empty file name used to get the file name in the enumerator

Long Long foldersize = 0;//defines and initializes a file size of 0

while ((FileName = [Childfilesenumerator nextobject]) = nil) {

Get the absolute path to a single file

NSString *fileabsolutepath = [FolderPath stringbyappendingpathcomponent:filename];

Define a single file variable and initialize it to 0

float singlefilesize = 0.0;

Gets the size of a single file if the individual file exists

if ([manager Fileexistsatpath:fileabsolutepath]) {

Attributesofitematpath: Get the properties of a file

Singlefilesize = [[Manager Attributesofitematpath:fileabsolutepath Error:nil] fileSize];

}

Foldersize + = Singlefilesize;

}

return foldersize;

}

Clear Cache

-(void) clearcache{

Nsfilemanager *manager = [Nsfilemanager Defaultmanager];

NSString *floderpath = [self getcache];

if ([manager Fileexistsatpath:floderpath]) {

Nsarray *childfiles = [manager Subpathsatpath:floderpath];

For (NSString *filename in childfiles) {

NSString *absolutepath = [Floderpath stringbyappendingpathcomponent:filename];

[Manager Removeitematpath:absolutepath Error:nil];

}

}

[[Sdimagecache Sharedimagecache] cleandisk];//because I used the sdwebimage this third-party class, so with this kind of self-bringing method to implement

}

Clear the implementation code for the cache feature

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.