From 4fc4aa21f3414a9a5e44a7243c9aa506030ce8de Mon Sep 17 00:00:00 2001 From: Scott Pfeil Date: Thu, 28 Mar 2024 13:35:07 -0400 Subject: [PATCH] Digital PCT265 story MVAPCT-48 - More expiry logic fixes --- .../MVMCore/OtherHandlers/MVMCoreCache+Extension.swift | 4 ++-- MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m | 10 +++++----- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift index ac5b0fb..ff7fda5 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache+Extension.swift @@ -59,7 +59,7 @@ public class CachedData: Codable { do { let dataToSave = try JSONEncoder().encode(cachedData) try dataToSave.write(to: filePath, options: .atomicWrite) - } catch let error as EncodingError { + } catch is EncodingError { throw CacheError.serializationFailed } catch { throw CacheError.saveFailed(error) @@ -67,12 +67,12 @@ public class CachedData: Codable { } @objc public func load(forKey key: String) throws -> [String: AnyHashable] { - let keyNSString = NSString(string: key) let filePath = self.filePath(forKey: key) do { let data = try Data(contentsOf: filePath) let decodedCachedData = try JSONDecoder().decode(CachedData.self, from: data) if Date() < decodedCachedData.expirationDate { + MVMCoreLoggingHandler.logDebugMessage(withDelegate: "CACHEDFEED: LOADED FROM PERSISTENT CACHE, key:\(key)") return decodedCachedData.data } else { MVMCoreLoggingHandler.logDebugMessage(withDelegate: "CACHEDFEED: EXPIRED, key:\(key)") diff --git a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m index 754f4a3..1a4aa98 100644 --- a/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m +++ b/MVMCore/MVMCore/OtherHandlers/MVMCoreCache.m @@ -105,11 +105,11 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; - (BOOL)isJSONExpired:(nonnull NSDictionary *)jsonDictionary { NSDate *expirationDate = [self getExpirationDateForJSON:jsonDictionary]; - NSDate *today = [NSDate date]; - if (today > expirationDate) { - [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:@"CACHEDFEED: NEW DATA ALREADY EXPIRED %@ %@ %@ %@",[jsonDictionary stringForKey:KeyPageType],[jsonDictionary stringForKey:@"moduleName"],today,expirationDate]]; + NSDate *now = [NSDate date]; + if ([now compare:expirationDate] == NSOrderedDescending) { + [MVMCoreLoggingHandler logDebugMessageWithDelegate:[NSString stringWithFormat:@"CACHEDFEED: NEW DATA ALREADY EXPIRED %@ now:%@ expirationDate:%@",jsonDictionary,now,expirationDate]]; } - return today > expirationDate; + return [now compare:expirationDate] == NSOrderedDescending; } - (nonnull NSDate *)getExpirationDateForJSON:(nonnull NSDictionary *)jsonDictionary { @@ -123,7 +123,7 @@ static NSString * const STATIC_CACHE_COMPONENT = @"StaticCache.txt"; if (!cachePolicy || ![cachePolicy boolForKey:@"persist"]) { return NO; } - return [self isJSONExpired:jsonDictionary]; + return ![self isJSONExpired:jsonDictionary]; } - (BOOL)shouldPersistentlyCachePage:(nonnull NSDictionary *)jsonDictionary pageType:(nonnull NSString *)pageType {