Skip to content

Commit 9202cf4

Browse files
committed
chore: cleanup cached configs if they do not match the current sdk version
1 parent 26e9abb commit 9202cf4

File tree

1 file changed

+26
-4
lines changed

1 file changed

+26
-4
lines changed

DevCycle/Models/Cache.swift

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,18 @@ protocol CacheServiceProtocol {
1818

1919
class CacheService: CacheServiceProtocol {
2020
struct CacheKeys {
21-
static let platform = PlatformDetails()
22-
static let versionPrefix = "VERSION_\(platform.sdkVersion)"
21+
static let versionPrefix = "VERSION_\(PlatformDetails().sdkVersion)."
2322

2423
static let anonUserId = "ANONYMOUS_USER_ID"
25-
static let identifiedConfigKey = "\(versionPrefix).IDENTIFIED_CONFIG"
26-
static let anonymousConfigKey = "\(versionPrefix).ANONYMOUS_CONFIG"
24+
static let identifiedConfig = "IDENTIFIED_CONFIG"
25+
static let anonymousConfig = "ANONYMOUS_CONFIG"
26+
2727
static let userIdSuffix = ".USER_ID"
2828
static let expiryDateSuffix = ".EXPIRY_DATE"
2929

30+
static let identifiedConfigKey = "\(versionPrefix)\(identifiedConfig)"
31+
static let anonymousConfigKey = "\(versionPrefix)\(anonymousConfig)"
32+
3033
// Legacy keys for cleanup
3134
static let legacyUser = "user"
3235
static let legacyConfig = "config"
@@ -39,6 +42,7 @@ class CacheService: CacheServiceProtocol {
3942
init(configCacheTTL: Int = DEFAULT_CONFIG_CACHE_TTL) {
4043
self.configCacheTTL = configCacheTTL
4144
migrateLegacyCache()
45+
clearDeprecatedCachedConfigs()
4246
}
4347

4448
func setAnonUserId(anonUserId: String) {
@@ -129,6 +133,24 @@ class CacheService: CacheServiceProtocol {
129133
return baseKey
130134
}
131135

136+
private func clearDeprecatedCachedConfigs() {
137+
let deprecatedKeys: [String] = defaults.dictionaryRepresentation().keys.compactMap { key in
138+
// Only include keys that contain one of these patterns
139+
guard key.contains(CacheKeys.identifiedConfig) || key.contains(CacheKeys.anonymousConfig) else {
140+
return nil
141+
}
142+
143+
return key.starts(with: CacheKeys.versionPrefix) ? nil : key
144+
}
145+
146+
for key in deprecatedKeys {
147+
if defaults.object(forKey: key) != nil {
148+
defaults.removeObject(forKey: key)
149+
Log.debug("Cleaned up cached config: \(key)")
150+
}
151+
}
152+
}
153+
132154
// MARK: - Legacy Cache Migration
133155

134156
func migrateLegacyCache() {

0 commit comments

Comments
 (0)