NSCache
NSCache是苹果官方提供的缓存类,具体使用的 NSDictionary类似,在AFN和SDWebImage框架中用来管理缓存。
苹果官方解释:NSCache在系统内存很低时,会自动释放对象。建议:在收到内存警告时,主动调用removeAllObject来释放对象。
NSCache是线程安全的,在多线程操作中,不需要对NSCache加锁。
NSCache只是对key进行strong引用, 不是拷贝(0成本)。
-setObject: forKey: 在缓存中设置指定键名对应的值。与可变字典不同的是,缓存对象不会对键名做copy操作 0成本
-(NSCache *)cache
{
if (_cache == nil) {
_cache = [[NSCache alloc]init];
//能够缓存对象的最大数量,默认值也是0(默认没有限制)。
//(当超出缓存最大成本或数量时,NSCache会把前面的数据即最开始存的给清除掉)
// _cache.countLimit = 6;
//缓存空间的最大成本,超出上限会自动回收对象。默认值是0没有限制。
_cache.totalCostLimit = 10;
_cache.delegate = self;
}
return _cache;
}
- (IBAction)setDataBtnClick:(id)sender {
for (int i = 0; i < 10 ; i++) {
NSData *urlData = [NSData dataWithContentsOfFile:@"/Users/lsp/Desktop/918855_174429094_2.jpg"];
// [self.cache setObject:urlData forKey:@(i)];
//设置该次存储的单个成本,结合总成本使用(totalCostLimit)
[self.cache setObject:urlData forKey:@(i) cost:2];
NSLog(@"------%d",i);
}
NSLog(@"------------存数据-------------");
}
- (IBAction)getDataBtnClick:(id)sender {
NSData *data0 = [self.cache objectForKey:@(0)];
NSLog(@"-data0---%@-------------",@(data0.length));
NSData *data1 = [self.cache objectForKey:@(1)];
NSLog(@"-data1---%@-------------",@(data1.length));
NSData *data2 = [self.cache objectForKey:@(2)];
NSLog(@"-data2---%@-------------",@(data2.length));
NSData *data3 = [self.cache objectForKey:@(3)];
NSLog(@"-data3---%@-------------",@(data3.length));
NSData *data4 = [self.cache objectForKey:@(4)];
NSLog(@"-data4---%@-------------",@(data4.length));
NSData *data5 = [self.cache objectForKey:@(5)];
NSLog(@"-data5---%@-------------",@(data5.length));
NSData *data6 = [self.cache objectForKey:@(6)];
NSLog(@"-data6---%@-------------",@(data6.length));
NSData *data7 = [self.cache objectForKey:@(7)];
NSLog(@"-data7---%@-------------",@(data7.length));
NSData *data8 = [self.cache objectForKey:@(8)];
NSLog(@"-data8---%@-------------",@(data8.length));
NSData *data9 = [self.cache objectForKey:@(9)];
NSLog(@"-data9---%@-------------",@(data9.length));
}
#pragma NSCacheDelegate
-(void)cache:(NSCache *)cache willEvictObject:(id)obj
{
//开启回收时 调用
NSLog(@"-----开启回收---");
}
打印:
2019-03-03 21:57:04.859007+0800 NSCache[23752:742457] ------0
2019-03-03 21:57:04.859504+0800 NSCache[23752:742457] ------1
2019-03-03 21:57:04.859893+0800 NSCache[23752:742457] ------2
2019-03-03 21:57:04.860292+0800 NSCache[23752:742457] ------3
2019-03-03 21:57:04.860676+0800 NSCache[23752:742457] ------4
2019-03-03 21:57:04.861096+0800 NSCache[23752:742457] -----开启回收---
2019-03-03 21:57:04.861226+0800 NSCache[23752:742457] ------5
2019-03-03 21:57:04.861512+0800 NSCache[23752:742457] -----开启回收---
2019-03-03 21:57:04.861646+0800 NSCache[23752:742457] ------6
2019-03-03 21:57:04.861893+0800 NSCache[23752:742457] -----开启回收---
2019-03-03 21:57:04.862055+0800 NSCache[23752:742457] ------7
2019-03-03 21:57:04.862286+0800 NSCache[23752:742457] -----开启回收---
2019-03-03 21:57:04.862408+0800 NSCache[23752:742457] ------8
2019-03-03 21:57:04.862734+0800 NSCache[23752:742457] -----开启回收---
2019-03-03 21:57:04.862853+0800 NSCache[23752:742457] ------9
2019-03-03 21:57:04.863006+0800 NSCache[23752:742457] ------------存数据-------------
2019-03-03 21:57:09.034144+0800 NSCache[23752:742457] -data0---0-------------
2019-03-03 21:57:09.034357+0800 NSCache[23752:742457] -data1---0-------------
2019-03-03 21:57:09.034517+0800 NSCache[23752:742457] -data2---0-------------
2019-03-03 21:57:09.034688+0800 NSCache[23752:742457] -data3---0-------------
2019-03-03 21:57:09.034838+0800 NSCache[23752:742457] -data4---0-------------
2019-03-03 21:57:09.034989+0800 NSCache[23752:742457] -data5---93581-------------
2019-03-03 21:57:09.035126+0800 NSCache[23752:742457] -data6---93581-------------
2019-03-03 21:57:09.035284+0800 NSCache[23752:742457] -data7---93581-------------
2019-03-03 21:57:09.035531+0800 NSCache[23752:742457] -data8---93581-------------
2019-03-03 21:57:09.036017+0800 NSCache[23752:742457] -data9---93581-------------