Nscache Novice asks to fly with

Source: Internet
Author: User


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

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.