-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rework the caching logic for subscription and entitlements #741
Changes from 5 commits
caf766b
2fcc47e
da45ce4
b388f92
f9f510d
9ad326a
6b142ea
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -56,7 +56,9 @@ | |
public convenience init(subscriptionAppGroup: String) { | ||
let accessTokenStorage = SubscriptionTokenKeychainStorage(keychainType: .dataProtection(.named(subscriptionAppGroup))) | ||
self.init(accessTokenStorage: accessTokenStorage, | ||
entitlementsCache: UserDefaultsCache<[Entitlement]>(subscriptionAppGroup: subscriptionAppGroup, key: UserDefaultsCacheKey.subscriptionEntitlements)) | ||
entitlementsCache: UserDefaultsCache<[Entitlement]>(subscriptionAppGroup: subscriptionAppGroup, | ||
key: UserDefaultsCacheKey.subscriptionEntitlements, | ||
settings: UserDefaultsCacheSettings(defaultExpirationInterval: .minutes(20)))) | ||
} | ||
|
||
public init(storage: AccountStorage = AccountKeychainStorage(), | ||
|
@@ -304,16 +306,22 @@ | |
} | ||
} | ||
|
||
public func checkSubscriptionState() async { | ||
os_log(.info, log: .subscription, "[AccountManager] checkSubscriptionState") | ||
public func refreshSubscriptionAndEntitlements() async { | ||
os_log(.info, log: .subscription, "[AccountManager] refreshSubscriptionAndEntitlements") | ||
|
||
guard let token = accessToken else { return } | ||
guard let token = accessToken else { | ||
SubscriptionService.signOut() | ||
entitlementsCache.reset() | ||
return | ||
} | ||
|
||
if case .success(let subscription) = await SubscriptionService.getSubscription(accessToken: token) { | ||
if case .success(let subscription) = await SubscriptionService.getSubscription(accessToken: token, cachePolicy: .reloadIgnoringLocalCacheData) { | ||
if !subscription.isActive { | ||
signOut() | ||
} | ||
} | ||
|
||
_ = await fetchEntitlements(cachePolicy: .reloadIgnoringLocalCacheData) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If |
||
} | ||
|
||
@discardableResult | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -35,7 +35,8 @@ public final class SubscriptionService: APIService { | |
} | ||
} | ||
|
||
private static let subscriptionCache = UserDefaultsCache<Subscription>(key: UserDefaultsCacheKey.subscription) | ||
private static let subscriptionCache = UserDefaultsCache<Subscription>(key: UserDefaultsCacheKey.subscription, | ||
settings: UserDefaultsCacheSettings(defaultExpirationInterval: .minutes(20))) | ||
|
||
public enum CachePolicy { | ||
case reloadIgnoringLocalCacheData | ||
|
@@ -55,7 +56,9 @@ public final class SubscriptionService: APIService { | |
|
||
switch result { | ||
case .success(let subscriptionResponse): | ||
subscriptionCache.set(subscriptionResponse) | ||
let defaultExpiryDate = Date().addingTimeInterval(subscriptionCache.settings.defaultExpirationInterval) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This relies on system time which can be manipulated. If device time is set to a distance future, any entitlement check that depends on the cache would just succeed. While we do use |
||
let expiryDate = min(defaultExpiryDate, subscriptionResponse.expiresOrRenewsAt) | ||
subscriptionCache.set(subscriptionResponse, expires: expiryDate) | ||
return .success(subscriptionResponse) | ||
case .failure(let error): | ||
return .failure(.apiError(error)) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be a configurable param as well.