ios-Data Cache Essays

Source: Internet
Author: User

Data cache, is to load the data from the network, stored locally, the purpose is to facilitate the subsequent invocation of the same data, the direct read cache, do not need network requests, not only saves the client side of the time cost, has saved the serve end of the load cost.

In IOS, the cached object Nsurlcache,ios before 5: Only the memory cache is supported. Starting with iOS 5: Supports both memory cache and hard disk cache.

Common uses of 3.NSURLCache

(1) Get the Global cache object (no need to create it manually) Nsurlcache *cache = [Nsurlcache Sharedurlcache];

(2) Set the maximum capacity of the memory cache (bytes, default is 512KB)-(void) Setmemorycapacity: (Nsuinteger) memorycapacity;

(3) Set the maximum capacity of the hard disk cache (bytes, default is 10M)-(void) Setdiskcapacity: (Nsuinteger) diskcapacity;

(4) The location of the hard disk cache: Sandbox/library/caches

(5) Cache of a request-(Nscachedurlresponse *) Cachedresponseforrequest: (nsurlrequest *) request;

(6) Clear cache of a request-(void) Removecachedresponseforrequest: (nsurlrequest *) request;

(7) Clear all Cache-(void) removeallcachedresponses;

4. Caching GET Requests

It's very easy to cache a GET request for data

Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];

Set Cache Policy

Request.cachepolicy = Nsurlrequestreturncachedataelseload;

As long as the cache policy is set, the system automatically uses Nsurlcache for data caching

4. Caching GET Requests

It's very easy to cache a GET request for data

Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url];

Set Cache Policy

Request.cachepolicy = Nsurlrequestreturncachedataelseload;

As long as the cache policy is set, the system automatically uses Nsurlcache for data caching

5.iOS provides 7 caching strategies for Nsurlrequest: (only 4 can actually be used)

Nsurlrequestuseprotocolcachepolicy//Default cache policy (depending on protocol)

Nsurlrequestreloadignoringlocalcachedata//Ignore cache, re-request

Nsurlrequestreloadignoringlocalandremotecachedata//Not implemented

Nsurlrequestreloadignoringcachedata = nsurlrequestreloadignoringlocalcachedata//Ignore cache, re-request

nsurlrequestreturncachedataelseload//cache with cache, re-request without cache

nsurlrequestreturncachedatadontload//with cache, no cache, no request, as request error handling (for offline mode)

Nsurlrequestreloadrevalidatingcachedata//Not implemented

-(void) Touchesbegan: (Nsset *) touches withevent: (Uievent *) Event 2 {3//1. Create request 4 Nsurl *url = [Nsurl Urlwithstri ng:@ "Http://127.0.0.1:8080/YYServer/video"]; 5 Nsmutableurlrequest *request = [Nsmutableurlrequest Requestwithurl:url]; 6 7//2. Set the cache policy (cache is used, no cache is requested again) 8 request.cachepolicy = nsurlrequestreturncachedataelseload; 9 10//3. Send request one by one [nsurlconnection sendasynchronousrequest:request queue:[nsoperationqueue Mainqueue] Completio nhandler:^ (Nsurlresponse *response, NSData *data, Nserror *connectionerror) {if (data) {Nsdictio             nary *dict = [nsjsonserialization jsonobjectwithdata:data options:nsjsonreadingmutableleaves error:nil];14 15 NSLog (@ "%@", dict); 16}17}];18}19 20/**21//Periodic processing cache on/if (cache not up to 7 days) {//requ Est.cachepolicy = nsurlrequestreturncachedataelseload;24//}25//Get Global Cache object All Nsurlcache *cache = [Nsurlcache Share Durlcache];
To periodically clean up the implementation of the cache:
The server returns the time (12,312,312,312 seconds), the client translates into the number of days to judge, if more than seven days (for example, seven days cache time), then clean the cache first, then request data, if less than seven days, read the cache.
if (cache reaches 7 days) {//[cache removecachedresponseforrequest:request]; +//}//lastcachedate = 2014-06-30 11:0 Nscachedurlresponse *response = [Cache cachedresponseforrequest:request]; if (response) {NSLog (@ "---this request already exists cache"), or else {PNS NSLog (@ "---this request is not cached"); 38}
Summarize:

Afnetworking, the caching mechanism should be implemented internally, the cache will load the memory cache, no cache and then re-request. (Afnetworking, the Nsurlcache should be integrated and the network class sends an asynchronous request).

To periodically clean up the implementation of the cache:

The server returns the time (12,312,312,312 seconds), the client translates into the number of days to judge, if more than seven days (for example, seven days cache time), then clean the cache first, then request data, if less than seven days, read the cache.

Seemingly can also achieve the client side of the time contrast, regularly clean the cache, the idea of students can think about it.

ios-Data Cache Essays

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.