NSCache caching how come

What is NSCache

NSCache mainly used to store temporary data (key-value pairs), when the memory resources is not enough, the system will automatically release the part of the data. It has three characteristics:
• NSCache order to maintain the system does not take up too much memory, it has a variety of automatic memory recovery strategy; when there is insufficient system memory occurs, it will make the recovery of part of the memory system to normal operation, this recovery is not controllable .
• NSCache can be in multiple threads access the same time does not need to be locked because it is thread-safe.
• NSMutableDictionary and different, NSCache not copy its internal key object.

Seen from the top of the features, NSCache is a good memory cache class, through which we can implement caching function data. Common open source framework is also of use NSCache, AFN's image cache, SDWebImage and so on.


NSCache test

The following validation characteristics NSCache, comprising three parts. NSCache cache capacity be? Is it safe multi-threaded access? It will copy of its internal object?

1. Cache capacity
NSCache provides totalCostLimit and countLimit property can be let outside cache size and number of cache limit, but inaccurate. Speaking of online posts about other NSCache cache capacity is 500M, and now we verify them.

    * Cache = NSCache [[NSCache the alloc] the init];
    
    int A = 0;
    the while (YES) {
        NSString * @ String = "string long list"; // about 1000 or so characters
        NSData * data = [string dataUsingEncoding: NSUTF8StringEncoding];
        NSString * Key = [NSString stringWithFormat: @ "% D", a];
        [Cache the setObject: Data forKey: Key];
        a ++;
    }

by the above code to run on iPhone6, memory rose 600M multi directly crashes , then the limit is 500 should be correct. At the same time in the fast memory reached the limit, the system is released too late to recover, it should be used with caution, and the top of the code is an infinite loop, very fast.

2. Multi-thread access

multithreaded access reading alone is not a problem, unless too many threads to access, where we simulate multithreading simultaneously written.

    NSCache * cache = [[NSCache alloc ] init];
    NSString * string = @ "long list of string"; // about about 1000 characters
    for (int I = 0; I <10; I ++) {
        dispatch_async (dispatch_get_global_queue (0, 0), {^
            [the setObject Cache: [ stringWithFormat NSString: @ "% D% @", I, String] forKey: @ "MulPth"];
            NSLog (@ "% @ IS Object", [objectForKey Cache: @ "MulPth"]);
        });
    }

If the string is set to very short string, the effect may not be obvious, so the ability to cache a copy of the string in the test came, from the print results, there are significant differences in the time stamp, and the order is not 0123456789, and consistent with the rest of the content can be drawn is thread-safe.
Essentially, NSCache in its internal use a mutex pthread_mutex thread-safe protection.

Guess you like

Origin www.cnblogs.com/xuanyishare/p/11105359.html