The original link

NSCache is basically an NSMutableDictionary that automatically removes objects to free up memory. There is no need to respond to memory warnings or use timers to clear the cache. The only difference is that key objects are not copied as they are in NSMutableDictionary, which is actually an advantage (keys don’t need to implement the NSCopying protocol).

Here’s a list of the benefits of using NSCache


  • NSCache is a mutable collection similar to NSDictionary.
  • Provides a way to set the number of caches and memory size limits.
  • This ensures thread-safety of the data being processed.
  • The key used by the cache need not be a class that implements NSCopying.
  • Internal automatic cleansing of some cached data when memory warning.

NSCache properties and methods


@property (assign) id<NSCacheDelegate>delegate;
Copy the code

Proxy for the cache object, used to be notified when the cache is about to be cleaned

- (void)cache:(NSCache *)cache willEvictObject:(id)obj;
Copy the code

The proxy method, which does not modify the cache, can be used if obj needs to be persisted

There are several cases in which this method can be executed:

  • Manually remove (removeObjectForKey)
  • The cache exceeded the preset threshold
  • The App is not active
  • System memory explosion
@property BOOL evictsObjectsWithDiscardedContent;
Copy the code

This property defaults to True, indicating that the object is discarded during memory destruction.

@property NSUInteger totalCostLimit;
Copy the code

Total number, used to set the maximum number of caches

Start using NSCache

//
// TDFSetPhoneNumController.m
// TDFLoginModule
//
// Created by doubanjiang on 2017/6/5.
// Copyright © 2017 Doubanjiang. All rights reserved.
//

#import "viewController.h"

@interface viewController()"NSCacheDelegate>
@property (nonatomic ,strong) NSCache *cache;
@end

@implementation viewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self beginCache];
}

- (void)beginCache {
    for (int i = 0; i<10; i++) {
        NSString *obj = [NSString stringWithFormat:@"object--%d",i];
        [self.cache setObject:obj forKey:@(i) cost:1];
        NSLog(@"%@ cached",obj); }}#pragma mark - NSCacheDelegate

- (void)cache:(NSCache *)cache willEvictObject:(id)obj {
    
    / / evict: expulsion
    NSLog(@ "% @"[NSString stringWithFormat:@"%@ will be evict",obj]);
}


#pragma mark - Getter

- (NSCache *)cache {
    if(! _cache) { _cache = [NSCache new];
        _cache.totalCostLimit = 5;
        _cache.delegate = self;
    }
    return _cache;
}
@end
Copy the code

We should see the following output

201807 -- 31 09:30:56.485719+0800 Test_Example[52839:214698] object-0 cached
201807 -- 31 09:30:56.485904+0800 Test_Example[52839:214698] object-- 1 cached
201807 -- 31 09:30:56.486024+0800 Test_Example[52839:214698] object-2 - cached
201807 -- 31 09:30:56.486113+0800 Test_Example[52839:214698] object-- 3 cached
201807 -- 31 09:30:56.486254+0800 Test_Example[52839:214698] object-4 - cached
201807 -- 31 09:30:56.486382+0800 Test_Example[52839:214698] object-0 will be evict
201807 -- 31 09:30:56.486480+0800 Test_Example[52839:214698] object-- 5 cached
201807 -- 31 09:30:56.486598+0800 Test_Example[52839:214698] object-- 1 will be evict
201807 -- 31 09:30:56.486681+0800 Test_Example[52839:214698] object-- 6 cached
201807 -- 31 09:30:56.486795+0800 Test_Example[52839:214698] object-2 - will be evict
201807 -- 31 09:30:56.486888+0800 Test_Example[52839:214698] object-7 - cached
201807 -- 31 09:30:56.486995+0800 Test_Example[52839:214698] object-- 3 will be evict
201807 -- 31 09:30:56.487190+0800 Test_Example[52839:214698] object-- 8 - cached
201807 -- 31 09:30:56.487446+0800 Test_Example[52839:214698] object-4 - will be evict
201807 -- 31 09:30:56.487604+0800 Test_Example[52839:214698] object-9 - cached
Copy the code


conclusion

Through experiments, we found that NSCache is cost-effective to use, which can be safely used in the project to improve the performance of APP.