清除缓存功能的实现代码

缓存文件存储在沙盒文件夹Caches中,实现清除缓存,主要就是实现找到文件 - - 删除文件(其中涉及到计算文件大小)

下载地址:http://pan.baidu.com/s/1mgGcpBy

以下是实现清除缓存的主要代码

#import <Foundation/Foundation.h>

#import <UIKit/UIKit.h>

@interface CleanCache : NSObject

- (NSString *)getCache;

- (NSString *)getDoc;

- (CGFloat)folderSizeAtPath;

- (void)clearCache;

+ (id)defaultCleanCache;

@end

#import "CleanCache.h"

@implementation CleanCache

+ (id)defaultCleanCache{

    static CleanCache *clean = nil;

    static dispatch_once_t onceToken;

    dispatch_once(&onceToken, ^{

        clean = [[CleanCache alloc] init];

    });

    return clean;

}

//获取缓存路径

- (NSString *)getCache{

    NSString *cachePath = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES) firstObject];

    return cachePath;

}

//获取持久化数据的路径

- (NSString *)getDoc{

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *docPath = paths.lastObject;

    return docPath;

}

//获取文件夹大小

- (CGFloat)folderSizeAtPath{

    //创建文件管理器

    NSFileManager *manager = [NSFileManager defaultManager];

    //获取文件夹路径

    NSString *folderPath = [self getCache];

    NSString *docPath = [self getDoc];

    //如果缓存文件不存在,就返回0

    if (![manager fileExistsAtPath:folderPath] && ![manager fileExistsAtPath:docPath]) {

        return 0;

    }

    NSString *fileName = nil;//定义一个空的文件名,用来获取枚举器中的文件名

    long long folderSize = 0;//定义并初始化文件大小为0

    if ([manager fileExistsAtPath:folderPath]) {

        //如果缓存文件存在就计算缓存文件的大小

        NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:folderPath] objectEnumerator];//从缓存路径中的文件数组得到枚举器

        while ((fileName = [childFilesEnumerator nextObject]) != nil) {

            //得到单个文件的绝对路径

            NSString *fileAbsolutePath = [folderPath stringByAppendingPathComponent:fileName];

            //定义单个文件变量并初始化为0

            float singleFileSize = 0.0;

            //如果该单个文件存在,获取单个文件的大小

            if ([manager fileExistsAtPath:fileAbsolutePath]) {

                //attributesOfItemAtPath:获取文件的属性

                singleFileSize = [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];

            }

            folderSize += singleFileSize;

        } 

    }

    if ([manager fileExistsAtPath:docPath]) {

        //如果缓存文件存在就计算缓存文件的大小

        NSEnumerator *childFilesEnumerator = [[manager subpathsAtPath:docPath] objectEnumerator];//从缓存路径中的文件数组得到枚举器

        while ((fileName = [childFilesEnumerator nextObject]) != nil) {

            //得到单个文件的绝对路径

            NSString *fileAbsolutePath = [docPath stringByAppendingPathComponent:fileName];

            //定义单个文件变量并初始化为0

            float singleFileSize = 0.0;

            //如果该单个文件存在,获取单个文件的大小

            if ([manager fileExistsAtPath:fileAbsolutePath]) {

                //attributesOfItemAtPath:获取文件的属性

                singleFileSize = [[manager attributesOfItemAtPath:fileAbsolutePath error:nil] fileSize];

            }

            folderSize += singleFileSize;

        } 

    }

    return folderSize;

}

//清除缓存

- (void)clearCache{

    NSFileManager *manager = [NSFileManager defaultManager];

    NSString *floderPath = [self getCache];

    NSString *docPath = [self getDoc];

    if ([manager fileExistsAtPath:floderPath]) {

        NSArray *childFiles = [manager subpathsAtPath:floderPath];

        for (NSString *fileName in childFiles) {

            NSString *absolutePath = [floderPath stringByAppendingPathComponent:fileName];

            [manager removeItemAtPath:absolutePath error:nil];

        }

    }

    if ([manager fileExistsAtPath:docPath]) {

        NSArray *childFiles = [manager subpathsAtPath:docPath];

        for (NSString *fileName in childFiles) {

            NSString *absolutePath = [docPath stringByAppendingPathComponent:fileName];

            [manager removeItemAtPath:absolutePath error:nil];

        }

    }

//使用第三方类图片异步下载SDWebImage的话,就使用这个第三方类的自带的清除缓存的方法

//    [[SDImageCache sharedImageCache] cleanDisk];

}

@end

原文地址:https://www.cnblogs.com/wangguimin/p/4567322.html