Nscache is a system-provided cache that is similar to a collection (nsmutabledictionary), Nscache has the ability to automatically delete to reduce the memory consumed by the system.
Here is a simple click event using the cache:
-(void) Touchesbegan: (Nsset<uitouch *> *) touches withevent: (Uievent *) Event {
Classes used by the system for caching
Nscache * cache = [[Nscache alloc] init];
Totalcostlimit Setting the cache size
Set the number of caches, clear the cache first
Cache.countlimit = 5;
for (Nsinteger i = 0; i <; i + +) {
[Cache setobject:[nsstring stringwithformat:@ "Cache-%@", @ (i)] forkey:@ (i)];
}
for (Nsinteger i = 0; i <; i + +) {
NSLog (@ "%@", [Cache objectforkey:@ (i)]);
}
}
Who is not in the cache
-(void) cache: (Nscache *) cache Willevictobject: (id) obj {
NSLog (@ "%@", obj);
}
Here are some Nscache properties and methods, and if there is no special case, use the system's Nscache, which is better:
@property Nsuinteger Totalcostlimit;
Setting the amount of memory consumed by the cache is not a strict limit, and when the total exceeds the value set by Totalcostlimit, the system clears a portion of the cache until the total consumption is lower than the Totalcostlimit value.
@property Nsuinteger Countlimit;
Setting the size of the cache object is not a strict limitation.
-(ID) Objectforkey: (ID) key;
Gets the cache object, based on the Key-value
-(void) SetObject: (ID) obj Forkey: (id) key; 0 cost
Store cached objects, taking into account the cache's restricted attributes;
Nscache Novice asks to fly with