Sdwebimage source Reading (Fri) Sdimagecacheconfig/sdimagecache (next)

Source: Internet
Author: User

All the methods and properties in the SDImageCache.h file have been analyzed in the previous article. Probably have a comprehensive understanding of the functions that sdimagecache can achieve. In this article, we focus on studying the realization of these functions and the principle of implementation.

Sdimagecache is used to do cache in Sdwebimage class, although only for the image of the cache, but in fact, in IOS development and even in the program development, the cache class to the cache file type differences and to separate for the file type to do the processing needs not much, Whether it is a cache of pictures or other types of files, they are basically consistent in the caching principle. Through the Sdimagecache to the image of the implementation of the cache processing, in the future development needs to do other types of file cache processing, you can imitate and learn, create a similar cache management class.

Here's how to start learning SDIMAGECACHE.M code:

The first is to define a class Autopurgecache that inherits from Nscache in the sdimagecache.m inner nesting.

Autopurgecache class. H/.M All content:

1 // Seehttps://github.com/rs/SDWebImage/pull/1141For discussion2 @interfaceAutopurgecache:nscache3 @end4 5 @implementationAutopurgecache6 7-(nonnull instancetype) init {8Self =[Super init];9     if(self) {Ten #ifSd_uikit One[[Nsnotificationcenter Defaultcenter] addobserver:self selector: @selector (removeallobjects) Name: UiapplicationdidreceivememorywarningnotificationObject: nil]; A #endif -     } -     returnSelf ; the } -  -- (void) Dealloc { - #ifSd_uikit +[[Nsnotificationcenter Defaultcenter] removeobserver:self name:uiapplicationdidreceivememorywarningnotificationObject: nil]; - #endif + } A  at @end

If the current development platform contains the Uikit framework, add a name to the Init method of the Autopurgecache class

uiapplicationdidreceivememorywarningnotification Notification, when receiving notification of the execution

The removeallobjects method, in the dealloc method of the class, removes the name from the

Notice of uiapplicationdidreceivememorywarningnotification .

  Uiapplicationdidreceivememorywarningnotification is an immutable string constant defined in the UIApplication.h:

1 Const Uiapplicationdidreceivememorywarningnotification;

2 typedef nsstring *NSNOTIFICATIONNAME Ns_extensible_string_enum;

When an application receives a memory warning, it sends the notification, allowing the application to clean up the memory.

Next, learn about the Nscache class:

  Nscache is a class contained within the Foundation framework, and its. h all code is more than 40 lines:

1 #import<Foundation/NSObject.h>2 3 @classNSString;4 @protocolnscachedelegate;5 6 Ns_assume_nonnull_begin7 8 ns_class_available (10_6, 4_0)9 @interfaceNscache <keytype, objecttype>: nsobject {Ten @private One     ID_delegate; A     void*_private[5]; -     void*_reserved; - } the  -@property (copy) NSString *name; -  -@property (nullable, assign)ID<NSCacheDelegate>Delegate; +  --(Nullable ObjectType) Objectforkey: (KeyType) key; +- (void) SetObject: (ObjectType) obj Forkey: (KeyType) key;//0 Cost A- (void) SetObject: (ObjectType) obj Forkey: (KeyType) key cost: (Nsuinteger) G; at- (void) Removeobjectforkey: (KeyType) key; -  -- (void) removeallobjects; -  -@property Nsuinteger Totalcostlimit;//limits is Imprecise/not strict -@property Nsuinteger Countlimit;//limits is Imprecise/not strict in @property BOOL evictsobjectswithdiscardedcontent; -  to @end +  - @protocolNscachedelegate <NSObject> the @optional *- (void) Cache: (Nscache *) cache Willevictobject: (ID) obj; $ @endPanax Notoginseng  -Ns_assume_nonnull_end

Nscache is a system-provided cache that is similar to a collection (nsmutabledictionary), which differs from the set:

1.NSCache has the feature of automatic deletion to reduce the memory occupied by the system.

2.NSCache is thread-safe and does not require an add-on lock.

3. The key object is not copied as in Nsmutabledictionary. (Keys do not need to implement the Nscopying protocol)

Reference Link: http://www.jianshu.com/p/a33d5abf686b

http://www.jianshu.com/p/5e69e211b161

Sdwebimage source Reading (Fri) Sdimagecacheconfig/sdimagecache (next)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.