YTKRequest.h 4.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. //
  2. // YTKRequest.h
  3. //
  4. // Copyright (c) 2012-2016 YTKNetwork https://github.com/yuantiku
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. #import "YTKBaseRequest.h"
  24. NS_ASSUME_NONNULL_BEGIN
  25. FOUNDATION_EXPORT NSString *const YTKRequestCacheErrorDomain;
  26. NS_ENUM(NSInteger) {
  27. YTKRequestCacheErrorExpired = -1,
  28. YTKRequestCacheErrorVersionMismatch = -2,
  29. YTKRequestCacheErrorSensitiveDataMismatch = -3,
  30. YTKRequestCacheErrorAppVersionMismatch = -4,
  31. YTKRequestCacheErrorInvalidCacheTime = -5,
  32. YTKRequestCacheErrorInvalidMetadata = -6,
  33. YTKRequestCacheErrorInvalidCacheData = -7,
  34. };
  35. /// YTKRequest is the base class you should inherit to create your own request class.
  36. /// Based on YTKBaseRequest, YTKRequest adds local caching feature. Note download
  37. /// request will not be cached whatsoever, because download request may involve complicated
  38. /// cache control policy controlled by `Cache-Control`, `Last-Modified`, etc.
  39. @interface YTKRequest : YTKBaseRequest
  40. /// Whether to use cache as response or not.
  41. /// Default is NO, which means caching will take effect with specific arguments.
  42. /// Note that `cacheTimeInSeconds` default is -1. As a result cache data is not actually
  43. /// used as response unless you return a positive value in `cacheTimeInSeconds`.
  44. ///
  45. /// Also note that this option does not affect storing the response, which means response will always be saved
  46. /// even `ignoreCache` is YES.
  47. @property (nonatomic) BOOL ignoreCache;
  48. /// Whether data is from local cache.
  49. - (BOOL)isDataFromCache;
  50. /// Manually load cache from storage.
  51. ///
  52. /// @param error If an error occurred causing cache loading failed, an error object will be passed, otherwise NULL.
  53. ///
  54. /// @return Whether cache is successfully loaded.
  55. - (BOOL)loadCacheWithError:(NSError * __autoreleasing *)error;
  56. /// Start request without reading local cache even if it exists. Use this to update local cache.
  57. - (void)startWithoutCache;
  58. /// Save response data (probably from another request) to this request's cache location
  59. - (void)saveResponseDataToCacheFile:(NSData *)data;
  60. #pragma mark - Subclass Override
  61. /// The max time duration that cache can stay in disk until it's considered expired.
  62. /// Default is -1, which means response is not actually saved as cache.
  63. - (NSInteger)cacheTimeInSeconds;
  64. /// Version can be used to identify and invalidate local cache. Default is 0.
  65. - (long long)cacheVersion;
  66. /// This can be used as additional identifier that tells the cache needs updating.
  67. ///
  68. /// @discussion The `description` string of this object will be used as an identifier to verify whether cache
  69. /// is invalid. Using `NSArray` or `NSDictionary` as return value type is recommended. However,
  70. /// If you intend to use your custom class type, make sure that `description` is correctly implemented.
  71. - (nullable id)cacheSensitiveData;
  72. /// Whether cache is asynchronously written to storage. Default is YES.
  73. - (BOOL)writeCacheAsynchronously;
  74. @end
  75. NS_ASSUME_NONNULL_END