IOS developmentMedium third-party libraryThree20 Network Cache MechanismIs the content to be introduced in this article, mainly to learnThree20NetworkCache MechanismFor more information, see the detailed description in this article.
Prerequisites:
The http-based Last-Modified and ETag can be searched online in detail. Simply put, the server contains an ID or a token when returning the resource.) The client caches the ID, which is contained in the next request to the same resource, the server determines whether the resource is changed based on this ID and returns different results (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 the use of memory and File Cache resources and resource IDs,
Change the cache scheme:
- TTURLRequest request;
- //blah,blah
- request.cachePolicy = cachePolicy | TTURLRequestCachePolicyEtag;
The Etag function is added here. If the server supports it, there is no doubt that this is the best solution. And so on. For example, cache is not required.
How to Use cache:
Here is a piece of TTImageView code. You can see it at a Glance:
- - (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 (![request send]) {
- // Put the default image in place while waiting for the request to load
- if (_defaultImage && nil == self.image) {
- self.image = _defaultImage;
- }
- }
- }
- }
- }
You can use the TTURLCache Singleton to obtain the local cache of any URL resource. The logic here is as follows:
First, determine whether the image exists in the memory:
- UIImage* image = [[TTURLCache sharedCache] imageForURL:_urlPath]
If it does not exist, initiate a request and use the Default policy to obtain the image. Assume that the image has been downloaded when the program was opened last time and has been cached on disk. This is the default value), and the image is not changed on the server, and the server supports if-modified, by default, the request returns the image on disk.
For details, see TTURLCache. If you manually send a request, the Default policy can implement the cache mechanism. Some built-in controls, such as TTTableView, are ideal if they contain images.
Summary:IOS developmentMedium third-party libraryThree20NetworkCache MechanismI hope this article will help you!