NSCache-最佳的緩存方式

原文鏈接

NSCache 基本上就是一個(gè)會(huì)自動(dòng)移除對象來釋放內(nèi)存的 NSMutableDictionary。無需響應(yīng)內(nèi)存警告或者使用計(jì)時(shí)器來清除緩存。唯一的不同之處是鍵對象不會(huì)像 NSMutableDictionary 中那樣被復(fù)制,這實(shí)際上是它的一個(gè)優(yōu)點(diǎn)(鍵不需要實(shí)現(xiàn) NSCopying 協(xié)議)。

先列一下使用NSCache的好處


  • NSCache是一個(gè)類似NSDictionary一個(gè)可變的集合。
  • 提供了可設(shè)置緩存的數(shù)目與內(nèi)存大小限制的方式。
  • 保證了處理的數(shù)據(jù)的線程安全性。
  • 緩存使用的key不需要是實(shí)現(xiàn)NSCopying的類。
  • 當(dāng)內(nèi)存警告時(shí)內(nèi)部自動(dòng)清理部分緩存數(shù)據(jù)。

NSCache的屬性與方法


@property (assign) id<NSCacheDelegate>delegate;

cache對象的代理 , 用來即將清理cache的時(shí)候得到通知

- (void)cache:(NSCache *)cache willEvictObject:(id)obj;

代理方法 , 這里面不要對cache進(jìn)行改動(dòng) , 如果對象obj需要被持久化存儲(chǔ)的話可以在這里進(jìn)行操作

這里面有幾種情況會(huì)導(dǎo)致該方法執(zhí)行:

  • 手動(dòng)移除(removeObjectForKey)
  • 緩存超過設(shè)定的上線
  • App不活躍
  • 系統(tǒng)內(nèi)存爆炸
@property BOOL evictsObjectsWithDiscardedContent;

該屬性默認(rèn)為True , 表示在內(nèi)存銷毀時(shí)丟棄該對象 。

@property NSUInteger totalCostLimit;

總成本數(shù) , 用來設(shè)置最大緩存數(shù)量

開始使用NSCache

//
//  TDFSetPhoneNumController.m
//  TDFLoginModule
//
//  Created by doubanjiang on 2017/6/5.
//  Copyright ? 2017年 doubanjiang. All rights reserved.
//

#import "viewController.h"

@interface viewController () <NSCacheDelegate>
@property (nonatomic ,strong) NSCache *cache;
@end

@implementation viewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self beginCache];
}

- (void)beginCache {
    for (int i = 0; i<10; i++) {
        NSString *obj = [NSString stringWithFormat:@"object--%d",i];
        [self.cache setObject:obj forKey:@(i) cost:1];
        NSLog(@"%@ cached",obj);
    }
}

#pragma mark - NSCacheDelegate

- (void)cache:(NSCache *)cache willEvictObject:(id)obj {
    
    //evict : 驅(qū)逐
    NSLog(@"%@", [NSString stringWithFormat:@"%@ will be evict",obj]);
}


#pragma mark - Getter

- (NSCache *)cache {
    if (!_cache) {
        _cache = [NSCache new];
        _cache.totalCostLimit = 5;
        _cache.delegate = self;
    }
    return _cache;
}
@end

我們會(huì)看到以下輸出

2018-07-31 09:30:56.485719+0800 Test_Example[52839:214698] object--0 cached
2018-07-31 09:30:56.485904+0800 Test_Example[52839:214698] object--1 cached
2018-07-31 09:30:56.486024+0800 Test_Example[52839:214698] object--2 cached
2018-07-31 09:30:56.486113+0800 Test_Example[52839:214698] object--3 cached
2018-07-31 09:30:56.486254+0800 Test_Example[52839:214698] object--4 cached
2018-07-31 09:30:56.486382+0800 Test_Example[52839:214698] object--0 will be evict
2018-07-31 09:30:56.486480+0800 Test_Example[52839:214698] object--5 cached
2018-07-31 09:30:56.486598+0800 Test_Example[52839:214698] object--1 will be evict
2018-07-31 09:30:56.486681+0800 Test_Example[52839:214698] object--6 cached
2018-07-31 09:30:56.486795+0800 Test_Example[52839:214698] object--2 will be evict
2018-07-31 09:30:56.486888+0800 Test_Example[52839:214698] object--7 cached
2018-07-31 09:30:56.486995+0800 Test_Example[52839:214698] object--3 will be evict
2018-07-31 09:30:56.487190+0800 Test_Example[52839:214698] object--8 cached
2018-07-31 09:30:56.487446+0800 Test_Example[52839:214698] object--4 will be evict
2018-07-31 09:30:56.487604+0800 Test_Example[52839:214698] object--9 cached


總結(jié)

經(jīng)過實(shí)驗(yàn)我們發(fā)現(xiàn)NSCache使用上性價(jià)比還是比較高的,可以在項(xiàng)目里放心去用,可以提升app的性能。

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

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

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