BTCacheDateValidator.m 1.0 KB

1234567891011121314151617181920212223242526272829
  1. #import <Foundation/Foundation.h>
  2. #import "BTCacheDateValidator_Internal.h"
  3. @implementation BTCacheDateValidator : NSObject
  4. - (instancetype)init {
  5. _timeToLiveMinutes = 5;
  6. return self;
  7. }
  8. -(BOOL) isCacheInvalid:(NSCachedURLResponse *)cachedConfigurationResponse {
  9. NSDate *currentTimestamp = [[NSDate alloc] init];
  10. // Invalidate cached configuration after 5 minutes
  11. NSDate *invalidCacheTimestamp = [currentTimestamp dateByAddingTimeInterval:-60*_timeToLiveMinutes];
  12. NSHTTPURLResponse *cachedResponse = (NSHTTPURLResponse*)cachedConfigurationResponse.response;
  13. NSString *cachedResponseDateString = cachedResponse.allHeaderFields[@"Date"];
  14. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  15. [dateFormatter setDateFormat:@"EEE',' dd' 'MMM' 'yyyy HH':'mm':'ss zzz"];
  16. NSDate *cachedResponseDate = [dateFormatter dateFromString:cachedResponseDateString];
  17. NSDate *earlierDate = [cachedResponseDate earlierDate:invalidCacheTimestamp];
  18. return [earlierDate isEqualToDate:cachedResponseDate];
  19. }
  20. @end