NSCache详解

2020-03-14  本文已影响0人  MiniCoder

NSCache:专门做缓存的类

官方描述

A mutable collection you use to temporarily store transient key-value pairs that are subject to eviction when resources are low.
一个可变的集合,用于临时存储在资源不足时易被驱逐的临时键值对。

Cache objects differ from other mutable collections in a few ways:
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.
You can add, remove, and query items in the cache from different threads without having to lock the cache yourself.
Unlike an NSMutableDictionary object, a cache does not copy the key objects that are put into it.
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.
Objects that have subcomponents that can be discarded when not being used can adopt the NSDiscardableContent protocol to improve cache eviction behavior. By default, 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 discardContentIfPossible on it upon its removal.

翻译

缓存对象与其他可变集合在以下几个方面不同:
NSCache类合并了各种自动清除策略,这些策略可以确保缓存不会占用太多的系统内存。如果其他应用程序需要内存,这些策略将从缓存中删除一些项,从而最小化其内存占用。
您可以从不同的线程添加、删除和查询缓存中的项,而不必自己锁定缓存。
与NSMutableDictionary对象不同,缓存不会复制放入其中的键对象。
您通常使用NSCache对象来临时存储具有瞬态数据的对象,这些对象的创建开销很大。重用这些对象可以提供性能优势,因为不必重新计算它们的值。但是,这些对象对应用程序并不重要,如果内存紧张,可以丢弃它们。如果被丢弃,则必须在需要时重新计算它们的值。
拥有可以在不使用时丢弃的子组件的对象可以采用NSDiscardableContent协议来改进缓存清除行为。默认情况下,如果缓存中的NSDiscardableContent对象的内容被丢弃,那么它们将被自动删除,不过这个自动删除策略可以更改。如果一个NSDiscardableContent对象被放入到缓存中,缓存将在它被删除时调用discardContentIfPossible。

1)NSCache是苹果官方提供的缓存类,具体使用和NSMutableDictionary类似,在AFN和SDWebImage框架中被使用来管理缓存
2)苹果官方解释NSCache在系统内存很低时,会自动释放对象.
    建议:接收到内存警告时主动调用removeAllObject方法释放对象
3)NSCache是线程安全的,在多线程操作中,不需要对NSCache加锁
4)NSCache的Key只是对对象进行Strong引用,不是拷贝,在清理的时候计算的是实际大小而不是引用的大小

NSCache属性和方法介绍

1)属性介绍
    name:名称
    delegete:设置代理
    totalCostLimit:缓存空间的最大总成本,超出上限会自动回收对象。默认值为0,表示没有限制
    countLimit:能够缓存的对象的最大数量。默认值为0,表示没有限制
    evictsObjectsWithDiscardedContent:标识缓存是否回收废弃的内容
2)方法介绍
    - (void)setObject:(ObjectType)obj forKey:(KeyType)key;//在缓存中设置指定键名对应的值,0成本
    - (void)setObject:(ObjectType)obj forKey:(KeyType)keycost:(NSUInteger)g;
    //在缓存中设置指定键名对应的值,并且指定该键值对的成本,用于计算记录在缓存中的所有对象的总成本
    //当出现内存警告或者超出缓存总成本上限的时候,缓存会开启一个回收过程,删除部分元素
    - (void)removeObjectForKey:(KeyType)key;//删除缓存中指定键名的对象
    - (void)removeAllObjects;//删除缓存中所有的对象

该代理只有一个回调方法,移除存储内容的时候会执行代理方法。

Called when an object is about to be evicted or removed from the cache.
当要从缓存中清除或删除对象时调用。
- (void)cache:(NSCache *)cache willEvictObject:(id)obj;
上一篇 下一篇

猜你喜欢

热点阅读