清除緩存的功能

清除緩存在每一個(gè)應(yīng)用是一個(gè)很常見(jiàn)的功能,今天這里小結(jié)一下。

將數(shù)據(jù)永久性的存儲(chǔ)我們稱(chēng)為數(shù)據(jù)持久化,其本質(zhì)是將數(shù)據(jù)存儲(chǔ)到文件中,放到本地,共手機(jī)使用。

由于緩存文件是存在APP沙盒文件中的,所以我們要實(shí)現(xiàn)緩存的清除,需要通過(guò)NSFileManager的API來(lái)對(duì)緩存的清除,

沙盒機(jī)制

沙盒是針對(duì)安裝到移動(dòng)終端上的每一個(gè)APP單獨(dú)生成的文件夾,存儲(chǔ)用戶(hù)的個(gè)性設(shè)置,每一個(gè)用戶(hù)的內(nèi)容不同,只具有讀寫(xiě)功能

沙盒文件夾的幾個(gè)路徑文件夾

Documents文件夾

存放數(shù)據(jù)持久化文件,比如:通訊錄信息,備份時(shí)也備份該文件夾內(nèi)容,該文件夾內(nèi)容不能過(guò)大,否則無(wú)法上傳到AppStore

獲取方法


NSString *documentsPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];

NSLog(@"doo = %@",documentsPath);


Library文件夾

Library 包含了Documents,Preferences,tmp。

Caches:存放緩存文件,下載的視頻,圖片,音頻,小說(shuō)都在該文件夾下

Preferences:存放用戶(hù)偏好設(shè)置,比如:存儲(chǔ)用戶(hù)的用戶(hù)名和密碼

tmp:存放臨時(shí)文件,比如:下載的zip包,解壓之后將zip包刪除,將解壓內(nèi)容移到Caches文件夾下.

Caches獲取方法



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

NSLog(@"cachesPath = %@",cachesPath);


下面的代碼是可以封裝成一個(gè) tool類(lèi)


/**

*? 獲取緩存路徑

*

*? @return 緩存路徑

*/

+(NSString *)cachePath;

/**

*? 清除緩存

*/

+(BOOL)clearCache;


/**

*? 獲取緩存大小(單位:M)

*

*? @return 緩存大小

*/

+ (float)cacheSize;


/**

*? 獲取緩存大小,(以..kb/..M)形式獲取

*? 小于1M,以kb形式返回,大于1M,以M形式返回

*? @return 緩存大小+單位

*/

+(NSString *)cacheSizeFormat;


/**

*? 獲取緩存路徑

*

*? @return 緩存路徑

*/

+(NSString *)cachePath {

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

return cachesPath;

}

/**

*? 清除緩存

*/

+(BOOL)clearCache {

///創(chuàng)建文件管理類(lèi)

NSFileManager *fileManager = [NSFileManager defaultManager];

///獲取路徑

NSString *path = [self cachePath];

BOOL result = [fileManager removeItemAtPath:path error:nil];

[self checkDirectory:path];

return result;

}

//檢查路徑

+(void)checkDirectory:(NSString *)path {

NSFileManager *fileManager = [NSFileManager defaultManager];

BOOL isDir;

if (![fileManager fileExistsAtPath:path isDirectory:&isDir]) {

[self createBaseDirectoryAtPath:path];

} else {

if (!isDir) {

NSError *error = nil;

[fileManager removeItemAtPath:path error:&error];

[self createBaseDirectoryAtPath:path];

}

}

}

+ (void)createBaseDirectoryAtPath:(NSString *)path {

__autoreleasing NSError *error = nil;

[[NSFileManager defaultManager] createDirectoryAtPath:path withIntermediateDirectories:YES

attributes:nil error:&error];

if (error) {

DebugLog(@"create cache directory failed, error = %@", error);

} else {

DebugLog(@"path = %@",path);

[self addDoNotBackupAttribute:path];

}

}

+ (void)addDoNotBackupAttribute:(NSString *)path {

NSURL *url = [NSURL fileURLWithPath:path];

NSError *error = nil;

[url setResourceValue:[NSNumber numberWithBool:YES] forKey:NSURLIsExcludedFromBackupKey error:&error];

if (error) {

DebugLog(@"error to set do not backup attribute, error = %@", error);

}

}

/**

*? 獲取緩存大小(單位:M)

*

*? @return 緩存大小

*/

+ (float)cacheSize {

NSString *directoryPath = [self cachePath];

BOOL isDir = NO;

unsigned long long total = 0;

if ([[NSFileManager defaultManager] fileExistsAtPath:directoryPath isDirectory:&isDir]) {

if (isDir) {

NSError *error = nil;

NSArray *array = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:directoryPath error:&error];

if (error == nil) {

for (NSString *subpath in array) {

NSString *path = [directoryPath stringByAppendingPathComponent:subpath];

NSDictionary *dict = [[NSFileManager defaultManager] attributesOfItemAtPath:path

error:&error];

if (!error) {

total += [dict[NSFileSize] unsignedIntegerValue];

}

}

}

}

}

return total/(1024.0*1024.0);

}

/**

*? 獲取緩存大小,(以..kb/..M)形式獲取

*? 小于1M,以kb形式返回,大于1M,以M形式返回

*? @return 緩存大小+單位

*/

+(NSString *)cacheSizeFormat {

NSString *sizeUnitString;

float size = [self cacheSize];

if(size < 1)

{

size *= 1024.0;//小于1M轉(zhuǎn)化為kb

sizeUnitString = [NSString stringWithFormat:@"%.1fkb",size];

}

else{

sizeUnitString = [NSString stringWithFormat:@"%.1fM",size];

}

return sizeUnitString;

}


當(dāng)然我們?cè)贏PP 中有時(shí)也會(huì)加載webView,那個(gè)有時(shí)webView的緩存也要清除,我們主要清除是Cookie

- (void)viewDidDisappear:(BOOL)animated {

? ?[super viewDidDisappear:animated];

_webView = nil;

[self cleanCacheAndCookie];

}

/**清除緩存和cookie*/

- (void)cleanCacheAndCookie{

//清除cookies

NSHTTPCookie *cookie;

NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

for (cookie in [storage cookies]){

[storage deleteCookie:cookie];

}

//清除UIWebView的緩存

[[NSURLCache sharedURLCache] removeAllCachedResponses];

NSURLCache * cache = [NSURLCache sharedURLCache];

[cache removeAllCachedResponses];

[cache setDiskCapacity:0];

[cache setMemoryCapacity:0];

}



最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容