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:
mayur nilwant 2024-06-07 16:49:02 -04:00
parent 9744fc5333
commit b31c97237c
3 changed files with 49 additions and 10 deletions

View File

@ -50,6 +50,7 @@ public class CachedData: Codable {
@objc public static let shared = PersistentCacheManager()
private let fileManager = FileManager.default
@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() {}

View File

@ -51,8 +51,9 @@ typedef void(^MVMCoreGetImageBlock)(UIImage * _Nullable, NSData * _Nullable, BOO
/// Checks if the module is to be persistently cached.
- (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
- (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.
- (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

View File

@ -137,8 +137,22 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
return [self shouldPersistentlyCacheJSON:jsonDictionary];
}
- (nullable NSURL *)getPathForPersistentCachePage:(nonnull NSString *)pageType {
return [[[[PersistentCacheManager shared].cacheDirectory URLByAppendingPathComponent:@"Pages"] URLByAppendingPathComponent:pageType] URLByAppendingPathExtension:@"json"];
- (BOOL)shouldSaveAtAppLevel:(nonnull NSDictionary *)jsonDictionary{
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 {
@ -172,8 +186,18 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
}
- (NSDictionary * _Nullable)fetchPageFromPersistentCache:(nonnull NSString *)pageType {
NSError *error = nil;
return [[PersistentCacheManager shared] loadForKey:pageType path:[self getPathForPersistentCachePage:pageType] error:&error];
NSError *error = nil;
// 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 {
@ -270,9 +294,11 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt";
[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;
[[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) {
[[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.
[weakSelf.pageTypeCache setObject:jsonDictionary forKey:pageType];
if (![self shouldPersistentlyCachePage:jsonDictionary pageType:pageType]) {
if (![self shouldPersistentlyCachePage:jsonDictionary pageType:pageType]) {
[[PersistentCacheManager shared] removeForKey:pageType error:nil];
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) {