Introduction to the
The official description
A mutable collection you use to temporarily store transient key-value pairs that are subject to eviction when resources are low.
A mutable collection for temporary storage of temporary key-value pairs that are vulnerable to expulsion when resources are scarce.
NSCache is an official Caching class provided by Apple. Its usage is similar to that of NSMutableDictionary. It is used to manage caches in AFNetworking and SDWebImage.
Look at the essence from the source
NSCache can be used to cache objects (such as common images) in memory. Compared to NSMutableDictionary, NSCache has the following features:
- NSCache is thread-safe, and we can add, remove, and query objects in the cache from different threads without locking the cache area.
- KeyType does not need to implement NSCopying. A cache object does not copy a key object.
- The NSCache class combines various automatic deletion strategies to ensure that it does not take up too much system memory
NSCache attributes:
Delegate delegate property totalCostLimit: The maximum cost of cache space, beyond which objects are automatically reclaimed. The default is 0. There is no limit. CountLimit: Maximum number of objects that can be cached. The default value is also 0 (there is no limit by default). (when the biggest cost or quantities beyond cache, NSCache will deposit the data that is in front of the beginning to remove) evictsObjectsWithDiscardedContent: marks whether the content of recycling waste, the default value is YES (to be automatic recovery).Copy the code
The internal implementation of NSCache contains:
NSMutableDictionary: stores data and indexes. NSLock: ensures thread-safe dictionary read and write operations by locking () and unlock() each time. NSCacheKey: encapsulates dictionary keys and implements hash and isEqual methods. Even if there is an object that does not implement Hashable as a key, NSObject provides a wrapper class for hashValue NSCacheEntry: dictionary value, and contains additional information: cost: PrevByCost: indicates the previous object in the list. NextByCost: indicates the last object in the listCopy the code
NSCache method:
-objectForKey: Returns the object associated with the key value. -setObject: forKey: sets the value corresponding to the specified key name in the cache. Unlike mutable dictionaries, cached objects do not copy key names. 0 Cost -setobject: forKey: cost: Sets the value of the specified key name in the cache and specifies the cost of the key value pair. Cost Cost is used to calculate the total cost of all objects recorded in the buffer. When a memory warning occurs, or the cost limit for the cache is exceeded, the cache initiates a reclamation process that removes some elements. -removeObjectForKey: deletes the object with the specified key from the cache. -removeAllObjects: deletes all objects in the cache.Copy the code
The use of the NSCache
-
NSCache automatically deletes the cache when the system sends a low memory notification.
For example, when NSCache is continuously filling data, the application memory becomes larger and larger. When the size is 500 MB, the system raises a Received memory warning. The memory is reduced because NSCache automatically deletes the cache when it is warned.
-
NSCache can set the size limit,countLimit can set the size limit, totalCostLimit can set the size limit, but usually not use, and also the calculation is also difficult.
-
Set the agent
- (void)cache:(NSCache *)cache willEvictObject:(id)obj {
NSLog(@"%@",obj);
}
Copy the code
With NSCache NSURLCache
NSCache has nothing to do with NSURLCache
The purpose of using NSURLCache is to enable applications to respond more quickly to user input and run efficiently. Sometimes we need to cache the data obtained by the remote Web server, exchange space for time, reduce the multiple requests to the same URL, reduce the pressure on the server, optimize the client network, and make the user experience better.
NSCache source code learning address