IOS asi--Cache

Source: Internet
Author: User

The ASI cache is a feature of the ASI, as well as Apple's native Nsurlcache, which also provides a single-instance object [Asidownloadcache Sharedcache]

1.ASI conditions for using caching

1> must be a GET request

2> request succeeded, status code is 200

2. How a single request uses the ASI cache

//Set URL

Nsurl *url = [Nsurl urlwithstring:@ "Http://localhots/123.png"];

Initializing requests

ASIHTTPRequest *request = [ASIHTTPRequest Requestwithurl:url];

Listening requests

__weak typeof (Request) temp = Request; //Avoid block circular references

[Request setcompletionblock:^{

NSLog (@ "----%@", temp.responsestring);

}];

//Set the requested cache policy

///1th way, get Asidownloadcache Singleton object, set the default cache policy; The singleton is then set to the Downloadcache (member variable) of the request

Asidownloadcache *cache = [Asidownloadcache Sharedcache];

[Cache Setdefaultcachepolicy:asionlyloadifnotcachedcachepolicy | Asifallbacktocacheifloadfailscachepolicy];

[Request Setdownloadcache:cache];

///2nd way, without Asidownloadcache Singleton object, create a new Asidownloadcache object yourself, then set the cache path and The default cache policy , and then set the cache object to the Downloadcache (member variable) of request

Asidownloadcache *mycache = [[Asidownloadcache alloc]init];

Mycache.storagepath = [[Nssearchpathfordirectoriesindomains (Nscachesdirectory, Nsuserdomainmask, YES) LastObject] stringbyappendingpathcomponent:@ "Mycache"];

Mycache.defaultcachepolicy = Asionlyloadifnotcachedcachepolicy;

[Request Setdownloadcache:mycache];

//Set the cache duration of the request with 2 types, namely session cache and hard disk cache

[Request Setcachestoragepolicy:asicacheforsessiondurationcachestoragepolicy];

Additional notes:

Question1: Why Asidownloadcache a singleton object, do not need to set the cache path, and the custom Asidownloadcache object needs to set the cache path?

Because the cache path has been set in the [Asidownloadcache Sharedcache] method, you can jump into this method to see it.

Question2: What does cachepolicy,cachestoragepolicy mean?

CachePolicy indicates when the cache is loaded and when the network data is loaded

Cachestoragepolicy indicates the length of the cache

3. How to use the ASI cache for multiple requests

Set up request 1 and request 2

Nsurl *URL1 = [Nsurl urlwithstring:@ "xxxxxx"];

ASIHTTPRequest *request1 = [ASIHTTPRequest REQUESTWITHURL:URL1];

Nsurl *URL2 = [Nsurl urlwithstring:@ "xxxxxxxxxxxxx"];

ASIHTTPRequest *request2 = [ASIHTTPRequest requestwithurl:url2];

The default cache management Object

Asidownloadcache *cache = [Asidownloadcache Sharedcache];

[Cache Setdefaultcachepolicy:asionlyloadifnotcachedcachepolicy | Asifallbacktocacheifloadfailscachepolicy];

Set the cache for request 1

[Request1 Setdownloadcache:cache];

Request1.cachepolicy = Asionlyloadifnotcachedcachepolicy; //According to the nearest principle, Request1 final cachepolicy=asionlyloadifnotcachedcachepolicy

Request1.cachestoragepolicy = Asicacheforsessiondurationcachestoragepolicy;

Set the cache for request 2

[Request2 Setdownloadcache:cache];

//Request2 not set CachePolicy, so request2 's CachePolicy = Default cache management caches Defaultcachepolicy

Request2.cachestoragepolicy =asicachepermanentlycachestoragepolicy;

Additional notes:

The 1>asidownloadcache function is to set the cache path of the request, and the other 1 functions provide the default Defaultcachepolicy

2> in this cache path will have 2 folders, one is the hard disk cache, the other 1 is a temporary cache, Request.cachestoragepolicy determines the cache in which folder. So Request.downloadcache and Request.cachestoragepolicy must be set, only these 2 properties, are set to finalize the path of the cache store

3>request.cachepolicy, depending on the actual need to decide whether to set.

If the request is to conform to the global cache policy, it does not have to be set, and it takes Asidownloadcache Defaultcachepolicy

If the request has its own personalization place, it should be set, it will be based on the nearest principle, take their own cachepolicy as the ultimate caching strategy

4. It is still cumbersome to set downloadcache for each request, so ASI provides a convenient way

[ASIHTTPRequest Setdefaultcache:[asidownloadcache Sharedcache];

5. Other features of the cache

[Request SETSECONDSTOCACHE:60 * 60 * 24 * 7]; Valid for 7 days

BOOL Isusecache = [request Didusecachedresponse]; Determine whether the data is from the cache

IOS asi--Cache

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.