diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h index 66686b4..3d0365d 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.h @@ -36,7 +36,14 @@ typedef void(^MVMCoreGetImageBlock)(UIImage * _Nullable, NSData * _Nullable, BOO // Checks the set of modules to be cached for the given module - (BOOL)shouldCacheJSONWithModule:(nonnull NSString *)module; -- (BOOL)shouldPersistentlyCache:(nonnull NSDictionary *)jsonDictionary identifier:(nonnull NSString *)identifier; +/// Returns if the json is expired or not. +- (BOOL)isJSONExpired:(nonnull NSDictionary *)jsonDictionary; + +/// Checks if the page is to be persistently cached. +- (BOOL)shouldPersistentlyCachePage:(nonnull NSDictionary *)jsonDictionary pageType:(nonnull NSString *)pageType; + +/// Checks if the module is to be persistently cached. +- (BOOL)shouldPersistentlyCacheModule:(nonnull NSDictionary *)jsonDictionary module:(nonnull NSString *)module; // For pages external to the mobile first framework to be added to the list to not cache - (void)addPageTypesToNotCache:(nullable NSArray *)array; diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index aef62c8..8fd3a82 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -39,8 +39,6 @@ @property (nullable, strong, nonatomic) NSCache *playerItemCache; -@property (nullable, strong, nonatomic) NSSet *itemsToPersist; - @end @implementation MVMCoreCache @@ -67,8 +65,6 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; self.videoQueue = dispatch_queue_create("video_queue", dispatch_queue_attr_make_with_qos_class(DISPATCH_QUEUE_SERIAL, QOS_CLASS_USER_INTERACTIVE, 0)); self.playerItemCache = [[NSCache alloc] init]; - - self.itemsToPersist = [NSSet setWithObjects:@"myFeed",@"FeedOrder",@"HabContent",@"launchModule",@"tabBar",@"DeepLinkService", nil]; } return self; } @@ -103,6 +99,28 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; return ![self.modulesToNotCache containsObject:module]; } +- (BOOL)isJSONExpired:(nonnull NSDictionary *)jsonDictionary { + double expiryTime = [[jsonDictionary dict:@"expiry"] doubleValue] * 1000; + NSTimeInterval timeSinceUnixEpoc = [[NSDate date] timeIntervalSince1970]; + return timeSinceUnixEpoc > expiryTime; +} + +- (BOOL)shouldPersistentlyCacheJSON:(nonnull NSDictionary *)jsonDictionary { + NSDictionary *cachePolicy = [jsonDictionary dict:@"cachePolicy"]; + if (!cachePolicy || ![cachePolicy boolForKey:@"persist"]) { + return NO; + } + return [self isJSONExpired:jsonDictionary]; +} + +- (BOOL)shouldPersistentlyCachePage:(nonnull NSDictionary *)jsonDictionary pageType:(nonnull NSString *)pageType { + return [self shouldPersistentlyCacheJSON:jsonDictionary]; +} + +- (BOOL)shouldPersistentlyCacheModule:(nonnull NSDictionary *)jsonDictionary module:(nonnull NSString *)module { + return [self shouldPersistentlyCacheJSON:jsonDictionary]; +} + - (void)addPageTypesToNotCache:(nullable NSArray *)array { if (array) { [self.pageTypesToNotCache addObjectsFromArray:array]; @@ -220,13 +238,6 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; [self addModulesToCache:jsonDictionary queue:nil waitUntilFinished:NO completionBlock:NULL]; } -- (BOOL)shouldPersistentlyCache:(nonnull NSDictionary *)jsonDictionary identifier:(nonnull NSString *)identifier { - if ([self.itemsToPersist containsObject:identifier]) { - return YES; - } - return NO; -} - #pragma mark - Advanced Insertion - (void)addPageToCache:(nonnull NSDictionary *)jsonDictionary pageType:(nonnull NSString *)pageType queue:(nullable NSOperationQueue *)queue waitUntilFinished:(BOOL)waitUntilFinished completionBlock:(nullable void (^)(void))completionBlock { @@ -251,7 +262,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; NSNumber *shouldCache = [jsonDictionary optionalNumberForKey:@"cache"]; if (shouldCache == nil || shouldCache.boolValue) { [weakSelf.pageTypeCache setObject:jsonDictionary forKey:pageType]; - if ([self shouldPersistentlyCache:jsonDictionary identifier:pageType]) { + if ([self shouldPersistentlyCachePage:jsonDictionary pageType:pageType]) { NSError *error = nil; [[DataCacheManager shared] saveWithData:jsonDictionary forKey:pageType cacheDuration:604800 shouldPersist:YES error:&error]; } @@ -293,7 +304,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; if (shouldCache == nil || shouldCache.boolValue) { [weakSelf.moduleCache setObject:obj forKey:key]; - if ([self shouldPersistentlyCache:obj identifier:key]) { + if ([self shouldPersistentlyCacheModule:obj module:key]) { NSError *error = nil; [[DataCacheManager shared] saveWithData:obj forKey:key cacheDuration:604800 shouldPersist:YES error:&error]; }