Analysis on the network cache mechanism of three20, the most important third-party library in IOS:

Source: Internet
Author: User

pre-knowledge recognition :

HTTP protocols from last-modified and etag with , detailed . simple to that is, , server service Returns A Resource contains an ID ( time or a token ), customer User end cache this I D, the next request is the same resource time , including This ID, server server determines resource whether to change change to return different result (200, or 304 ).

 

Three20The default cache solution is as follows:

Tturlrequestcachepolicydefault

= (Tturlrequestcachepolicymemory | tturlrequestcachepolicydisk

| Tturlrequestcachepolicynetwork ),

Tturlrequestcachepolicynetwork indicates that the last-modified policy is used,

Tturlrequestcachepolicymemory | tturlrequestcachepolicydisk indicates that memory and files are used.Cache ResourcesAndResource ID,

 

ChangeChange cache solution:

Tturlrequest request;

// Blah, blah

Request. cachepolicy = cachepolicy | tturlrequestcachepolicyetag;

 

HereAdded the etag function.ServicesSupportedWords, No doubtAsk thisThe best solution. And so on.Cache.

 

How to UseCache:

HerePull a ttimageview generationCodeAt first glance, you will know:

-(Void) Reload {

If(Nil=_ Request&&Nil! =_ Urlpath){

Uiimage*Image= [[Tturlcache Sharedcache]Imageforurl:_ Urlpath];

 

If(Nil! =Image){

Self.Image=Image;

 

}Else{

Tturlrequest* Request = [Tturlrequest Requestwithurl:_ Urlpath Delegate:Self];

Request.Response= [[[Tturlimageresponse Alloc]Init]Autorelease];

 

If(! [RequestSend]) {

// Put the default image in place while waiting for the request to load

If(_ Defaultimage&&Nil=Self.Image){

Self.Image=_ Defaultimage;

}

}

}

}

}

UseTturlcacheOfSingleFor example,YesObtainArbitrary URLResourcesLocalCache.HereOfLogicYesThis wayOf:

First, determine whether the memory existsThis image:

Uiimage*Image= [[Tturlcache Sharedcache]Imageforurl:_ Urlpath]

If not,InitiateOne request, usingRecognizePolicy,Obtain the image. Assume that the image was last openedProgramHas been downloaded, has been cached in disk (this is the default), and the image is not changed on the server, and the server supports if-modified, the request will return the image on disk by default.

 

DetailsYou can see tturlcache

 

Summary:

IfSendOne request,ThenMoRecognizeThe policy can be very good.Implemented CacheMechanism. Some built-in controls, such as tttableviewImage, AlsoImplementationIs ideal.

Related Article

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.