Skip to content
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

Lower level cache #34

Merged
merged 13 commits into from
Jul 13, 2023
Merged
8 changes: 4 additions & 4 deletions Example/FlagsmithClient/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@ class AppDelegate: UIResponder, UIApplicationDelegate {
Flag(featureName: "my_name", stringValue:"Testing", enabled: true)]

// set cache on / off (defaults to off)
Flagsmith.shared.useCache = true
Flagsmith.shared.cacheConfig.useCache = true

// set custom cache to use (defaults to shared URLCache)
//Flagsmith.shared.cache = <CUSTOM_CACHE>
//Flagsmith.shared.cacheConfig.cache = <CUSTOM_CACHE>

// set skip API on / off (defaults to off)
Flagsmith.shared.skipAPI = false
Flagsmith.shared.cacheConfig.skipAPI = false

// set cache TTL in seconds (defaults to 0, i.e. infinite)
Flagsmith.shared.cacheTTL = 90
Flagsmith.shared.cacheConfig.cacheTTL = 90

// set analytics on or off
Flagsmith.shared.enableAnalytics = true
Expand Down
31 changes: 19 additions & 12 deletions FlagsmithClient/Classes/Flagsmith.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,9 @@ public class Flagsmith {

/// Default flags to fall back on if an API call fails
public var defaultFlags: [Flag] = []

/// Cache to use when enabled, defaults to the shared app cache
public var cache: URLCache = URLCache.shared

/// Use cached flags as a fallback?
public var useCache: Bool = false

/// TTL for the cache in seconds, default of 0 means infinite
public var cacheTTL: Double = 0

/// Skip API if there is a cache available
public var skipAPI: Bool = false

/// Configuration class for the cache settings
public var cacheConfig:CacheConfig = CacheConfig()

private init() {
}
Expand Down Expand Up @@ -289,3 +280,19 @@ public class Flagsmith {
return returnFlags
}
}

public class CacheConfig {

/// Cache to use when enabled, defaults to the shared app cache
public var cache: URLCache = URLCache.shared

/// Use cached flags as a fallback?
public var useCache: Bool = false

/// TTL for the cache in seconds, default of 0 means infinite
public var cacheTTL: Double = 0

/// Skip API if there is a cache available
public var skipAPI: Bool = false

}
10 changes: 5 additions & 5 deletions FlagsmithClient/Classes/Internal/APIManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class APIManager : NSObject, URLSessionDataDelegate {
func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, willCacheResponse proposedResponse: CachedURLResponse, completionHandler: @escaping (CachedURLResponse?) -> Void) {

// intercept and modify the cache settings for the response
if Flagsmith.shared.useCache {
let newResponse = proposedResponse.response(withExpirationDuration: Int(Flagsmith.shared.cacheTTL))
if Flagsmith.shared.cacheConfig.useCache {
let newResponse = proposedResponse.response(withExpirationDuration: Int(Flagsmith.shared.cacheConfig.cacheTTL))
completionHandler(newResponse)
} else {
completionHandler(proposedResponse)
Expand Down Expand Up @@ -89,10 +89,10 @@ class APIManager : NSObject, URLSessionDataDelegate {

// set the cache policy based on Flagsmith settings
request.cachePolicy = .reloadIgnoringLocalAndRemoteCacheData
session.configuration.urlCache = Flagsmith.shared.cache
if Flagsmith.shared.useCache {
session.configuration.urlCache = Flagsmith.shared.cacheConfig.cache
matthewelwell marked this conversation as resolved.
Show resolved Hide resolved
if Flagsmith.shared.cacheConfig.useCache {
request.cachePolicy = .useProtocolCachePolicy
if Flagsmith.shared.skipAPI {
if Flagsmith.shared.cacheConfig.skipAPI {
request.cachePolicy = .returnCacheDataElseLoad
}
}
Expand Down