ASICacheDelegate.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. //
  2. // ASICacheDelegate.h
  3. // Part of ASIHTTPRequest -> http://allseeing-i.com/ASIHTTPRequest
  4. //
  5. // Created by Ben Copsey on 01/05/2010.
  6. // Copyright 2010 All-Seeing Interactive. All rights reserved.
  7. //
  8. #import <Foundation/Foundation.h>
  9. @class ASIHTTPRequest;
  10. // Cache policies control the behaviour of a cache and how requests use the cache
  11. // When setting a cache policy, you can use a combination of these values as a bitmask
  12. // For example: [request setCachePolicy:ASIAskServerIfModifiedCachePolicy|ASIFallbackToCacheIfLoadFailsCachePolicy|ASIDoNotWriteToCacheCachePolicy];
  13. // Note that some of the behaviours below are mutally exclusive - you cannot combine ASIAskServerIfModifiedWhenStaleCachePolicy and ASIAskServerIfModifiedCachePolicy, for example.
  14. typedef enum _ASICachePolicy {
  15. // The default cache policy. When you set a request to use this, it will use the cache's defaultCachePolicy
  16. // ASIDownloadCache's default cache policy is 'ASIAskServerIfModifiedWhenStaleCachePolicy'
  17. ASIUseDefaultCachePolicy = 0,
  18. // Tell the request not to read from the cache
  19. ASIDoNotReadFromCacheCachePolicy = 1,
  20. // The the request not to write to the cache
  21. ASIDoNotWriteToCacheCachePolicy = 2,
  22. // Ask the server if there is an updated version of this resource (using a conditional GET) ONLY when the cached data is stale
  23. ASIAskServerIfModifiedWhenStaleCachePolicy = 4,
  24. // Always ask the server if there is an updated version of this resource (using a conditional GET)
  25. ASIAskServerIfModifiedCachePolicy = 8,
  26. // If cached data exists, use it even if it is stale. This means requests will not talk to the server unless the resource they are requesting is not in the cache
  27. ASIOnlyLoadIfNotCachedCachePolicy = 16,
  28. // If cached data exists, use it even if it is stale. If cached data does not exist, stop (will not set an error on the request)
  29. ASIDontLoadCachePolicy = 32,
  30. // Specifies that cached data may be used if the request fails. If cached data is used, the request will succeed without error. Usually used in combination with other options above.
  31. ASIFallbackToCacheIfLoadFailsCachePolicy = 64
  32. } ASICachePolicy;
  33. // Cache storage policies control whether cached data persists between application launches (ASICachePermanentlyCacheStoragePolicy) or not (ASICacheForSessionDurationCacheStoragePolicy)
  34. // Calling [ASIHTTPRequest clearSession] will remove any data stored using ASICacheForSessionDurationCacheStoragePolicy
  35. typedef enum _ASICacheStoragePolicy {
  36. ASICacheForSessionDurationCacheStoragePolicy = 0,
  37. ASICachePermanentlyCacheStoragePolicy = 1
  38. } ASICacheStoragePolicy;
  39. @protocol ASICacheDelegate <NSObject>
  40. @required
  41. // Should return the cache policy that will be used when requests have their cache policy set to ASIUseDefaultCachePolicy
  42. - (ASICachePolicy)defaultCachePolicy;
  43. // Returns the date a cached response should expire on. Pass a non-zero max age to specify a custom date.
  44. - (NSDate *)expiryDateForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;
  45. // Updates cached response headers with a new expiry date. Pass a non-zero max age to specify a custom date.
  46. - (void)updateExpiryForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;
  47. // Looks at the request's cache policy and any cached headers to determine if the cache data is still valid
  48. - (BOOL)canUseCachedDataForRequest:(ASIHTTPRequest *)request;
  49. // Removes cached data for a particular request
  50. - (void)removeCachedDataForRequest:(ASIHTTPRequest *)request;
  51. // Should return YES if the cache considers its cached response current for the request
  52. // Should return NO is the data is not cached, or (for example) if the cached headers state the request should have expired
  53. - (BOOL)isCachedDataCurrentForRequest:(ASIHTTPRequest *)request;
  54. // Should store the response for the passed request in the cache
  55. // When a non-zero maxAge is passed, it should be used as the expiry time for the cached response
  56. - (void)storeResponseForRequest:(ASIHTTPRequest *)request maxAge:(NSTimeInterval)maxAge;
  57. // Removes cached data for a particular url
  58. - (void)removeCachedDataForURL:(NSURL *)url;
  59. // Should return an NSDictionary of cached headers for the passed URL, if it is stored in the cache
  60. - (NSDictionary *)cachedResponseHeadersForURL:(NSURL *)url;
  61. // Should return the cached body of a response for the passed URL, if it is stored in the cache
  62. - (NSData *)cachedResponseDataForURL:(NSURL *)url;
  63. // Returns a path to the cached response data, if it exists
  64. - (NSString *)pathToCachedResponseDataForURL:(NSURL *)url;
  65. // Returns a path to the cached response headers, if they url
  66. - (NSString *)pathToCachedResponseHeadersForURL:(NSURL *)url;
  67. // Returns the location to use to store cached response headers for a particular request
  68. - (NSString *)pathToStoreCachedResponseHeadersForRequest:(ASIHTTPRequest *)request;
  69. // Returns the location to use to store a cached response body for a particular request
  70. - (NSString *)pathToStoreCachedResponseDataForRequest:(ASIHTTPRequest *)request;
  71. // Clear cached data stored for the passed storage policy
  72. - (void)clearCachedResponsesForStoragePolicy:(ASICacheStoragePolicy)cachePolicy;
  73. @end