IOS development ASIHTTPRequest uses download cache

Source: Internet
Author: User

This article describes how to use download cache for iOS development ASIHTTPRequest, including cache policies, storage policies, other cache-related features, and write your own cache.

Since version 1.8, the APIs of ASIDownloadCache and ASICacheDelegate have changed. You may need to modify your code.

In particular, the available options of the cache policy have changed. Now you can use the combined cache Policy for a single request.

ASIHTTPRequest can automatically cache downloaded data, which is useful in many cases.

  • When you are offline, you cannot download the data again, and you need to access the data.
  • After the data is downloaded last time, you only need to download the new data after the data is updated.
  • The data you process will never change, so you only want to download the data once.

In earlier versions of ASIHTTPRequest, you have to handle these policies on your own. In some cases, using download cache allows you to no longer write data to the local cache mechanism.

ASIDownloadCache is a simple URL cache that can be used to cache the data of GET requests. To Cache a request, the request must first be successful without sending an error), and the server must return a HTTP status value. Alternatively, you can set the 301,302,303,307 redirection status code from version 1.8.1.

The cache mechanism for enabling the response value is simple:

 
 
  1. [ASIHTTPRequest setDefaultCache:[ASIDownloadCache sharedCache]]; 

After this is done, all requests will automatically use the cache. If you want to, you can use the shared cache for different requests:

 
 
  1. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  2. [request setDownloadCache:[ASIDownloadCache sharedCache]]; 

You will not be limited to using a single cache. You can create a single cache As Long As You Like ^. When you create a cache by yourself, you must set the cache path-this path must be a directory with write permission.

 
 
  1. ASIDownloadCache * cache = [[ASIDownloadCache alloc] init] autorelease];
  2. [Cache setStoragePath: @ "/Users/ben/Documents/Cached-Downloads"];
  3.  
  4. // Don't forget-you must retaining your own cache!
  5. [Self setMyCache: cache];
  6.  
  7. ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
  8. [Request setDownloadCache: [self myCache];
Cache Policy

The cache policy is the main method for you to control the information in the cache. It controls when to use the cache data instead of downloading the data again.

The cache policy of each request is controlled by the cachePolicy attribute of the request. The cache policy is defined using a mask, so you can "and" operate on them in binary format.

 
 
  1. // Every time I ask the server whether new content is available,
  2. // If the request fails, use the cache data even if the data has expired.
  3. [Request setCachePolicy: ASIAskServerIfModifiedCachePolicy | ASIFallbackToCacheIfLoadFailsCachePolicy];

You can use the following cache policy options to control the request cache Policy:

ASIUseDefaultCachePolicy

The default cache Policy. Do not use this item with other items. When you set a request to use the cache, it will use the defaultCachePolicy of the cache. The default cache Policy of ASIDownloadCache is 'asiaskserverifmodifiedwhenstalecachepolicy '.

ASIDoNotReadFromCacheCachePolicy

With this option, the request will not read data from the cache.

ASIDoNotWriteToCacheCachePolicy

When this item is used, the request will not store the data to the cache.

ASIAskServerIfModifiedWhen

StaleCachePolicy

This is the default cache Policy of ASIDownloadCaches. When this policy is used, the request first checks whether cache data is available. If not, the request will work as a normal request.

If the cached data does not expire, the request uses the cached data and does not communicate with the server. If the cached data expires, the request will first initiate a GET request to check whether the server has a new version of the data. If the server says that the cached data is the current version, the cached data will be used and new data will not be downloaded. In this case, the cache validity period is set to the new validity period provided by the server. If the server provides the updated content, the new content will be downloaded, and the new data and its validity period will be written into the cache.

ASIAskServerIfModifiedCachePolicy

This item correspondsASIAskServerIfModifiedWhenStaleCachePolicySame, except for one point: each request will ask whether the server data has been updated.

ASIOnlyLoadIfNotCachedCachePolicy

When this item is used, the cache data will be used all the time, regardless of the expiration time.

ASIDontLoadCachePolicy

When this item is used, the request succeeds only when the response data is cached. If a request has no response data cached, the request will be stopped and no errors will be set on the request.

ASIFallbackToCacheIf

LoadFailsCachePolicy

When this item is used, when the request fails, the request will return to request the cache data. If the request fails and the request uses the cache data, the request will be successful without errors ). You usually use this item with other items because it applies to specifying the request behavior when an error occurs.

When you set the defaultCachePolicy attribute of a cache object, all requests using this cache object will use this cache policy unless you set another policy for the request.

Storage Policy

The storage policy allows you to define how long a cache can store specific data. ASIHTTPRequest currently supports two storage policies:

ASICacheForSessionDurationCacheStoragePolicyIs the default value. The data is only stored during the session. When cache is used for the first time or when [ASIHTTPRequest clearSession] is called, the data is cleared.

UseASICachePermanentlyCacheStoragePolicyThe cached data is permanently stored. To use this storage policy, set it to request:

 
 
  1. ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url]; 
  2. [request setCacheStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 

To manually clear the cache, call the FunctionClearCachedResponsesForStoragePolicy:To pass in the storage policy for the cache data to be cleared:

 
 
  1. [[ASIDownloadCache sharedCache] clearCachedResponsesForStoragePolicy:ASICachePermanentlyCacheStoragePolicy]; 
Other cache-related features
 
 
  1. // When shouldRespectCacheControlHeaders is disabled, the cache object stores response data and ignores
  2. // The server's explicit "do not cache" statement (for example, cache-control or pragma: no-cache header)
  3. [[ASIDownloadCache sharedCache] setShouldRespectCacheControlHeaders: NO];
  4.  
  5. // You can set the secondsToCache of the request to overwrite the content validity period set by the server. In this case, the response data
  6. // It will be cached until it passes secondsToCache for seconds.
  7. ASIHTTPRequest * request = [ASIHTTPRequest requestWithURL: url];
  8. [Request setSecondsToCache: 60*60*24*30]; // cache for 30 days
  9.  
  10. // After the request starts to be executed, if the response data is obtained from the cache, didUseCachedResponse returns YES
  11. [Request didUseCachedResponse];
  12.  
  13. // Obtain a path from the cache object to store the corresponding data. This is the most efficient way to use download cache,
  14. // At this time, when the request is complete, the data does not need to be copied to the cache.
  15. [Request setDownloadDestinationPath:
  16. [[ASIDownloadCache sharedCache] pathToStoreCachedResponseDataForRequest: request];
Write your own cache

If you already have a download cache and want to insert it into ASIHTTPRequest, or if you want to write your own download cache, implement the ASICacheDelegate protocol for your 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.