对于有必定开发经验的iOS攻城狮来讲,咱们在对一个APP数据作存储和内存优化的时候,不可避免的须要对缓存作相应的处理,并且缓存处理的优劣,每每也是决定一个APP可否长线发展的重要因素之一,今天就来讲一下常常容易被咱们忽略的一个苹果官方提供的一套缓存机制--->NSCachegit
主要做用于内存缓存的管理方面; 在没有引入NSCache以前,咱们要管理缓存,都是使用的NSMutableDictionary来管理,如:github
// 定义下载操做缓存池 @property (nonatomic, strong) NSMutableDictionary *operationCache; // 定义图片缓存池 @property (nonatomic, strong) NSMutableDictionary *imageCache;
然而,使用NSMutableDictionary来管理缓存是有些不妥的, 知道多线程操做原理的开发者都明白, NSMutableDictionary在线程方面来讲是不安全,这也是苹果官方文档明确说明了的,而若是使用的是NSCache,那就不会出现这些问题.因此接下来咱们先看看两者的区别:缓存
相同点: NSCache和NSMutableDictionary功能用法基本是相同的。安全
区别:多线程
先定义缓存池,并懒加载初始化:优化
#import "ViewController.h" @interface ViewController () <NSCacheDelegate> // 定义缓存池 @property (nonatomic, strong) NSCache *cache; @end @implementation ViewController - (NSCache *)cache { if (_cache == nil) { _cache = [[NSCache alloc] init]; // 缓存中总共能够存储多少条 _cache.countLimit = 5; // 缓存的数据总量为多少 _cache.totalCostLimit = 1024 * 5; } return _cache; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //添加缓存数据 for (int i = 0; i < 10; i++) { [self.cache setObject:[NSString stringWithFormat:@"hello %d",i] forKey:[NSString stringWithFormat:@"h%d",i]]; NSLog(@"添加 %@",[NSString stringWithFormat:@"hello %d",i]); } //输出缓存中的数据 for (int i = 0; i < 10; i++) { NSLog(@"%@",[self.cache objectForKey:[NSString stringWithFormat:@"h%d",i]]); } }
控制台输出结果为: atom
**经过输出结果能够看出: **线程
1.当咱们使用NSCache来建立缓存池的时候,咱们能够很灵活的设置缓存的限额, 2.当程序中的个数超过咱们的限额的时候,会先移除最早建立的 3.若是已经移除了,那么当咱们输出缓存中的数据的时候,就只剩下后面建立的数据了;代理
先设置代理对象: - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. //设置NSCache的代理 self.cache.delegate = self; 调用代理方法: 这里我仅用一个方法来演示:code
//当缓存被移除的时候执行 - (void)cache:(NSCache *)cache willEvictObject:(id)obj{ NSLog(@"缓存移除 %@",obj); }
输出结果为:
经过结果能够看出: NSCache的功能要比NSMutableDictionary的功能要强大不少不少;
代码演示:
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //当收到内存警告,清除内存 [self.cache removeAllObjects]; //输出缓存中的数据 for (int i = 0; i < 10; i++) { NSLog(@"%@",[self.cache objectForKey:[NSString stringWithFormat:@"h%d",i]]); } }
控制台输出结果: 经过结果能够看出: 当收到内存警告以后,清除数据以后,NSCache缓存池中全部的数据都会为空!
代码演示:
- (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; //当收到内存警告,调用removeAllObjects 以后,没法再次往缓存中添加数据 [self.cache removeAllObjects]; //输出缓存中的数据 for (int i = 0; i < 10; i++) { NSLog(@"%@",[self.cache objectForKey:[NSString stringWithFormat:@"h%d",i]]); } } // 触摸事件, 以便验证添加数据 - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event { [self.cache removeAllObjects]; //添加缓存数据 for (int i = 0; i < 10; i++) { [self.cache setObject:[NSString stringWithFormat:@"hello %d",i] forKey:[NSString stringWithFormat:@"h%d",i]]; // NSLog(@"添加 %@",[NSString stringWithFormat:@"hello %d",i]); } //输出缓存中的数据 for (int i = 0; i < 10; i++) { NSLog(@"%@",[self.cache objectForKey:[NSString stringWithFormat:@"h%d",i]]); } }
控制台输出结果为:
经过输出结果,咱们能够看出: 当收到内存警告,而咱们又调用removeAllObjects 以后,则没法再次往缓存中添加数据;
更多详情能够查看个人Github项目: https://github.com/DXSmile/NSCache-some-understanding..git