NSCache的简单介绍应用

NSCache

介绍
NSCache 是苹果提供的一个专门用来做缓存的类
使用和 NSMutableDictionary 非常相似
是线程安全的
当内存不足的时候,会自动清理缓存
程序开始时,可以指定缓存的数量 & 成本
方法

取值
- (id)objectForKey:(id)key;
设置对象,0成本
- (void)setObject:(id)obj forKey:(id)key;
设置对象并指定成本
- (void)setObject:(id)obj forKey:(id)key cost:(NSUInteger)g;

成本示例,以图片为例:

方案一:缓存 100 张图片
方案二:总缓存成本设定为 10M,以图片的 宽 * 高当作成本,图像像素。这样,无论缓存的多少张照片,只要像素值超过 10M,就会自动清理
结论:在缓存图像时,使用成本,比单纯设置数量要科学!
删除
- (void)removeObjectForKey:(id)key;
删除全部
- (void)removeAllObjects;
属性

缓存总成本
@property NSUInteger totalCostLimit;

缓存总数量
@property NSUInteger countLimit;

是否自动清理缓存,默认是 YES
@property BOOL evictsObjectsWithDiscardedContent;

代码演练

定义缓存属性
@property (nonatomic, strong) NSCache *cache;
懒加载并设置限制
- (NSCache *)cache {
    if (_cache == nil) {
        _cache = [[NSCache alloc] init];
        _cache.delegate = self;
        _cache.countLimit = 10;
    }
    return _cache;
}
触摸事件添加缓存
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    for (int i = 0; i < 20; ++i) {
        NSString *str = [NSString stringWithFormat:@"%d", i];
        NSLog(@"set -> %@", str);
        [self.cache setObject:str forKey:@(i)];
        NSLog(@"set -> %@ over", str);
    }

    // 遍历缓存
    NSLog(@"------");

    for (int i = 0; i < 20; ++i) {
        NSLog(@"%@", [self.cache objectForKey:@(i)]);
    }
}

// 代理方法,仅供观察使用,开发时不建议重写此方法
- (void)cache:(NSCache *)cache willEvictObject:(id)obj {
    NSLog(@"remove -> %@", obj);
}
//修改网络图片框架

//修改图像缓冲池类型,并移动到 .h 中,以便后续测试
///  图像缓冲池
@property (nonatomic, strong) NSCache *imageCache;
//修改懒加载,并设置数量限制
- (NSCache *)imageCache {
    if (_imageCache == nil) {
        _imageCache = [[NSCache alloc] init];
        _imageCache.countLimit = 15;
    }
    return _imageCache;
}
//修改其他几处代码,将 self.imageCache[URLString] 替换为 [self.imageCache setObject:image forKey:URLString];

//测试缓存中的图片变化

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    for (AppInfo *app in self.appList) {
        NSLog(@"%@ %@", [[DownloadImageManager sharedManager].imageCache objectForKey:app.icon], app.name);
    }
}
//注册通知,监听内存警告
- (instancetype)init
{
    self = [super init];
    if (self) {
        // 注册通知
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(clearMemory) name:UIApplicationDidReceiveMemoryWarningNotification object:nil];
    }
    return self;
}

// 提示:虽然执行不到,但是写了也无所谓
- (void)dealloc {
    // 删除通知
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
//清理内存
- (void)clearMemory {
    NSLog(@"%s", __FUNCTION__);

    // 取消所有下载操作
    [self.downloadQueue cancelAllOperations];

    // 删除缓冲池
    [self.operationChache removeAllObjects];
}
**注意:内存警告或者超出限制后,缓存中的任何对象,都有可能被清理**。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值