iOS学习笔记:NSCache

一、NSCache本身是苹果官方提供,专门用来做缓存的类

//设置成本上限   成本:缓存的是图像  1、设置缓存,上限是100张图片 2、成本,图像,是从左到右,从上到下   
// 宽度*高度=成本  比如上限为1000*1000 图片为100*100,就能放10张图片 类似水桶原理?
@property NSUInteger totalCostLimit;    // limits are imprecise/not strict
//缓存的数量上限
@property NSUInteger countLimit;    // limits are imprecise/not strict
//自动移除上限 默认为YES
@property BOOL evictsObjectsWithDiscardedContent;

二、使用方式跟NSMutableDictionary非常相似

//取值
- (nullable ObjectType)objectForKey:(KeyType)key;
//设置
- (void)setObject:(ObjectType)obj forKey:(KeyType)key; // 0 cost
- (void)setObject:(ObjectType)obj forKey:(KeyType)key cost:(NSUInteger)g;
//删除
- (void)removeObjectForKey:(KeyType)key;
- (void)removeAllObjects;

三、线程安全的

四、当内存不足的时候,就会自动清理缓存

五、程序开始的时候,自己可以指定缓存的数量,以及成本

你可能感兴趣的:(iOS学习笔记:NSCache)