NSCache官方文档学习

基本阐述

1、A mutable collection you use to temporarily store transient key-value pairs that are subject to eviction when resources are low.

  • t r a n s i e n t \color{red}{transient} transient 短暂的路过的
  • e v i c t i o n \color{red}{eviction} eviction:逐出,赶出,收回
  • 这是一个临时存储键值对的容器,在资源(主要是内存)低的时候存储的值会被清除。

2、最近在学习SDWebImage框架,学习到了NSCache,就单独拎出来学习学习。

Overview概要

1、The NSCache class incorporates various auto-eviction policies, which ensure that a cache doesn’t use too much of the system’s memory. If memory is needed by other applications, these policies remove some items from the cache, minimizing its memory footprint.

  • i n c o r p o r a t e s \color{red}{incorporates} incorporates: 包含
  • i a u t o − e v i c t i o n \color{red}{iauto-eviction} iautoeviction:自动清除
  • NSCache包含变化的字移除策略,这种策略确保一个cache不使用过多的系统内存,如果其他应用需要内存,这些(定好的)策略会从cache中移除一些项目,降低它的内存足迹。

2、You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.

  • 从不同线程添加、移除、查询项目,不会死锁cache,也就是线程安全。

3、Unlike an N S M u t a b l e D i c t i o n a r y \color{red}{NSMutableDictionary} NSMutableDictionary object, a cache does not copy the key objects that are put into it.

  • 这句话的意思就是较NSMutableDictionary相比,cache没有字典的拷贝,字典拷贝应该是value,这点我可能认识是错误的。

4、You typically use NSCache objects to temporarily store objects with transient data that are expensive to create. Reusing these objects can provide performance benefits, because their values do not have to be recalculated. However, the objects are not critical to the application and can be discarded if memory is tight. If discarded, their values will have to be recomputed again when needed.

  • that are expensive to create 我理解就是这些数据创建起来比较费劲
  • 意思是:你通常用NSCache对象来临时短暂存储一些比较难创建的数据,重用这些数据能提高性能,因为这些值不需要被重新计算。然而,这些对象对你的应用来说不是非常重要,如果内存紧张,能被抛弃,如果这些对象被废弃了,这些值在需要的时候被重新计算。

5、Objects that have subcomponents that can be discarded when not being used can adopt the N S D i s c a r d a b l e C o n t e n t p r o t o c o l \color{red}{NSDiscardableContent protocol} NSDiscardableContentprotocol to improve cache eviction behavior. By default, N S D i s c a r d a b l e C o n t e n t \color{red}{NSDiscardableContent} NSDiscardableContent objects in a cache are automatically removed if their content is discarded, although this automatic removal policy can be changed. If an NSDiscardableContent object is put into the cache, the cache calls d i s c a r d C o n t e n t I f P o s s i b l e ( ) \color{red}{discardContentIfPossible()} discardContentIfPossible() on it upon its removal.

  • 这里详细说明了一些废弃政策。
  • 遵守NSDiscardableContent协议
  • to improve cache eviction behavior改善缓存废弃行为
  • 调用discardContentIfPossible移除NSDiscardableContent object

方法学习

  • protocol NSDiscardableContent
    You implement this protocol when a class’s objects have subcomponents that can be discarded when not being used, thereby giving an application a smaller memory footprint.
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值