1. Written logic to analyze app level page type and store it accordingly in different directory.
2 Also implemented logic to fetch from app level or user level.
This commit is contained in:
parent
a00eff7cf1
commit
e76313da4e
@ -50,6 +50,7 @@ public class CachedData: Codable {
|
|||||||
@objc public static let shared = PersistentCacheManager()
|
@objc public static let shared = PersistentCacheManager()
|
||||||
private let fileManager = FileManager.default
|
private let fileManager = FileManager.default
|
||||||
@objc public lazy var cacheDirectory = { fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("Atomic")}()
|
@objc public lazy var cacheDirectory = { fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("Atomic")}()
|
||||||
|
@objc public lazy var appLevelCacheDirectory = { fileManager.urls(for: .cachesDirectory, in: .userDomainMask).first!.appendingPathComponent("AppAtomic")}()
|
||||||
|
|
||||||
private override init() {}
|
private override init() {}
|
||||||
|
|
||||||
|
|||||||
@ -51,8 +51,9 @@ typedef void(^MVMCoreGetImageBlock)(UIImage * _Nullable, NSData * _Nullable, BOO
|
|||||||
/// Checks if the module is to be persistently cached.
|
/// Checks if the module is to be persistently cached.
|
||||||
- (BOOL)shouldPersistentlyCacheModule:(nonnull NSDictionary *)jsonDictionary module:(nonnull NSString *)module;
|
- (BOOL)shouldPersistentlyCacheModule:(nonnull NSDictionary *)jsonDictionary module:(nonnull NSString *)module;
|
||||||
|
|
||||||
/// Can override the path for the page to be cached. Currently Cache/Atomic/Pages/pageType
|
|
||||||
- (nullable NSURL *)getPathForPersistentCachePage:(nonnull NSString *)pageType;
|
/// Can override the path for the page to be cached at app level and user level. Currently Cache/*
|
||||||
|
- (nullable NSURL *)getPathForPersistentCachePage:(nonnull NSString *)pageType isAppLevel:(BOOL)isAppLevel;
|
||||||
|
|
||||||
/// Can override the path for the page to be cached. Currently Cache/Atomic/Modules/moduleName
|
/// Can override the path for the page to be cached. Currently Cache/Atomic/Modules/moduleName
|
||||||
- (nullable NSURL *)getPathForPersistentCacheModule:(nonnull NSString *)moduleName;
|
- (nullable NSURL *)getPathForPersistentCacheModule:(nonnull NSString *)moduleName;
|
||||||
@ -198,5 +199,6 @@ typedef void(^MVMCoreGetImageBlock)(UIImage * _Nullable, NSData * _Nullable, BOO
|
|||||||
/// Loads an AVPlayerAsset from the shared asset cache.
|
/// Loads an AVPlayerAsset from the shared asset cache.
|
||||||
- (void)playerAssetFromFileName:(nonnull NSString *)filename trackKeys:(nonnull NSArray *)trackKeys onComplete:(void(^_Nonnull)(AVAsset * _Nullable, NSString * _Nonnull, MVMCoreErrorObject * _Nullable))completionHandler;
|
- (void)playerAssetFromFileName:(nonnull NSString *)filename trackKeys:(nonnull NSArray *)trackKeys onComplete:(void(^_Nonnull)(AVAsset * _Nullable, NSString * _Nonnull, MVMCoreErrorObject * _Nullable))completionHandler;
|
||||||
|
|
||||||
|
- (BOOL)shouldSaveAtAppLevel:(nonnull NSDictionary *)jsonDictionary;
|
||||||
|
|
||||||
@end
|
@end
|
||||||
|
|||||||
@ -137,8 +137,22 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
|
|||||||
return [self shouldPersistentlyCacheJSON:jsonDictionary];
|
return [self shouldPersistentlyCacheJSON:jsonDictionary];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (nullable NSURL *)getPathForPersistentCachePage:(nonnull NSString *)pageType {
|
- (BOOL)shouldSaveAtAppLevel:(nonnull NSDictionary *)jsonDictionary{
|
||||||
return [[[[PersistentCacheManager shared].cacheDirectory URLByAppendingPathComponent:@"Pages"] URLByAppendingPathComponent:pageType] URLByAppendingPathExtension:@"json"];
|
|
||||||
|
NSDictionary *cachePolicy = [jsonDictionary dict:KeyCachePolicy];
|
||||||
|
if (!cachePolicy || ![cachePolicy boolForKey:@"persist"] || ![cachePolicy boolForKey:@"applicationLevelCache"]) {
|
||||||
|
return NO;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (nullable NSURL *)getPathForPersistentCachePage:(nonnull NSString *)pageType isAppLevel:(BOOL)isAppLevel{
|
||||||
|
|
||||||
|
if (isAppLevel) {
|
||||||
|
return [[[[PersistentCacheManager shared].appLevelCacheDirectory URLByAppendingPathComponent:@"Pages"] URLByAppendingPathComponent:pageType] URLByAppendingPathExtension:@"json"];
|
||||||
|
} else {
|
||||||
|
return [[[[PersistentCacheManager shared].cacheDirectory URLByAppendingPathComponent:@"Pages"] URLByAppendingPathComponent:pageType] URLByAppendingPathExtension:@"json"];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (nullable NSURL *)getPathForPersistentCacheModule:(nonnull NSString *)moduleName {
|
- (nullable NSURL *)getPathForPersistentCacheModule:(nonnull NSString *)moduleName {
|
||||||
@ -172,8 +186,18 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
|
|||||||
}
|
}
|
||||||
|
|
||||||
- (NSDictionary * _Nullable)fetchPageFromPersistentCache:(nonnull NSString *)pageType {
|
- (NSDictionary * _Nullable)fetchPageFromPersistentCache:(nonnull NSString *)pageType {
|
||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
return [[PersistentCacheManager shared] loadForKey:pageType path:[self getPathForPersistentCachePage:pageType] error:&error];
|
// APP_CACHE
|
||||||
|
// if check in getPathForPersistentCachePage
|
||||||
|
// else getPathForPersistentAppCachePage
|
||||||
|
// return
|
||||||
|
NSMutableDictionary *fileDataDictionary = [[PersistentCacheManager shared] loadForKey:pageType path:[self getPathForPersistentCachePage:pageType isAppLevel:YES] error:&error];
|
||||||
|
|
||||||
|
if (fileDataDictionary != nil) {
|
||||||
|
return fileDataDictionary;
|
||||||
|
}else {
|
||||||
|
return [[PersistentCacheManager shared] loadForKey:pageType path:[self getPathForPersistentCachePage:pageType isAppLevel:YES] error:&error];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
- (NSDictionary * _Nullable)fetchModuleFromPersistentCache:(nonnull NSString *)moduleName {
|
- (NSDictionary * _Nullable)fetchModuleFromPersistentCache:(nonnull NSString *)moduleName {
|
||||||
@ -270,9 +294,11 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
|
|||||||
[self addModulesToCache:jsonDictionary queue:nil waitUntilFinished:NO completionBlock:NULL];
|
[self addModulesToCache:jsonDictionary queue:nil waitUntilFinished:NO completionBlock:NULL];
|
||||||
}
|
}
|
||||||
|
|
||||||
- (void)addPageToPersistentCache:(nonnull NSDictionary *)jsonDictionary pageType:(nonnull NSString *)pageType expirationDate:(nonnull NSDate *)expirationDate {
|
|
||||||
|
|
||||||
|
- (void)addPageToPersistentCache:(nonnull NSDictionary *)jsonDictionary pageType:(nonnull NSString *)pageType expirationDate:(nonnull NSDate *)expirationDate isApplicationLevel:(BOOL)isApplicationLevel {
|
||||||
NSError *error = nil;
|
NSError *error = nil;
|
||||||
[[PersistentCacheManager shared] saveWithData:jsonDictionary forKey:pageType path:[self getPathForPersistentCachePage:pageType] expirationDate:expirationDate error:&error];
|
[[PersistentCacheManager shared] saveWithData:jsonDictionary forKey:pageType path:[self getPathForPersistentCachePage:pageType isAppLevel:isApplicationLevel] expirationDate:expirationDate error:&error];
|
||||||
if (error) {
|
if (error) {
|
||||||
[[MVMCoreLoggingHandler sharedLoggingHandler] addErrorToLog:[MVMCoreErrorObject createErrorObjectForNSError:error location:[NSString stringWithFormat:@"%s_%@",__PRETTY_FUNCTION__,pageType]]];
|
[[MVMCoreLoggingHandler sharedLoggingHandler] addErrorToLog:[MVMCoreErrorObject createErrorObjectForNSError:error location:[NSString stringWithFormat:@"%s_%@",__PRETTY_FUNCTION__,pageType]]];
|
||||||
}
|
}
|
||||||
@ -314,11 +340,21 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
|
|||||||
// Adds json to cache with page type key.
|
// Adds json to cache with page type key.
|
||||||
[weakSelf.pageTypeCache setObject:jsonDictionary forKey:pageType];
|
[weakSelf.pageTypeCache setObject:jsonDictionary forKey:pageType];
|
||||||
|
|
||||||
if (![self shouldPersistentlyCachePage:jsonDictionary pageType:pageType]) {
|
if (![self shouldPersistentlyCachePage:jsonDictionary pageType:pageType]) {
|
||||||
[[PersistentCacheManager shared] removeForKey:pageType error:nil];
|
[[PersistentCacheManager shared] removeForKey:pageType error:nil];
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
[self addPageToPersistentCache:jsonDictionary pageType:pageType expirationDate:[self getExpirationDateForJSON:jsonDictionary]];
|
|
||||||
|
[self addPageToPersistentCache:jsonDictionary pageType:pageType expirationDate:[self getExpirationDateForJSON:jsonDictionary] isApplicationLevel:[self shouldSaveAtAppLevel:jsonDictionary]];
|
||||||
|
|
||||||
|
// if ([self shouldSaveAtAppLevel:jsonDictionary]) {
|
||||||
|
// //[self addPageToPersistentCache:jsonDictionary pageType:pageType expirationDate:expirationDate isApplicationLevel:YES];
|
||||||
|
// [self addPageToPersistentCache:jsonDictionary pageType:pageType expirationDate:[self getExpirationDateForJSON:jsonDictionary] isApplicationLevel:YES];
|
||||||
|
//
|
||||||
|
// } else {
|
||||||
|
// // [self addPageToPersistentCache:jsonDictionary pageType:pageType expirationDate:expirationDate isApplicationLevel:NO];
|
||||||
|
// }
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (completionBlock) {
|
if (completionBlock) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user