SettingsCacheClient.swift 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import Foundation
  16. /// CacheKey is like a "key" to a "safe". It provides necessary metadata about the current cache to know if it should be expired.
  17. struct CacheKey: Codable {
  18. var createdAt: Date
  19. var googleAppID: String
  20. var appVersion: String
  21. }
  22. /// SettingsCacheClient is responsible for accessing the cache that Settings are stored in.
  23. protocol SettingsCacheClient {
  24. /// Returns in-memory cache content in O(1) time
  25. var cacheContent: [String: Any]? { get }
  26. /// Returns in-memory cache-key, no performance guarantee because type-casting depends on size of CacheKey
  27. var cacheKey: CacheKey? { get }
  28. /// Removes all cache content and cache-key
  29. func removeCache()
  30. }
  31. /// SettingsCache uses UserDefaults to store Settings on-disk, but also directly query UserDefaults when accessing Settings values during run-time. This is because UserDefaults encapsulates both in-memory and persisted-on-disk storage, allowing fast synchronous access in-app while hiding away the complexity of managing persistence asynchronously.
  32. class SettingsCache: SettingsCacheClient {
  33. private static let settingsVersion: Int = 1
  34. private static let content: String = "firebase-sessions-settings"
  35. private static let key: String = "firebase-sessions-cache-key"
  36. /// UserDefaults holds values in memory, making access O(1) and synchronous within the app, while abstracting away async disk IO.
  37. private let cache: UserDefaults = .standard
  38. /// Converting to dictionary is O(1) because object conversion is O(1)
  39. var cacheContent: [String: Any]? {
  40. return cache.dictionary(forKey: SettingsCache.content)
  41. }
  42. /// Casting to Codable from Data is O(n)
  43. var cacheKey: CacheKey? {
  44. if let data = cache.data(forKey: SettingsCache.key) {
  45. do {
  46. return try JSONDecoder().decode(CacheKey.self, from: data)
  47. } catch {
  48. Logger.logError("[Settings] Decoding CacheKey failed with error: \(error)")
  49. }
  50. }
  51. return nil
  52. }
  53. /// Removes stored cache
  54. func removeCache() {
  55. cache.set(nil, forKey: SettingsCache.content)
  56. cache.set(nil, forKey: SettingsCache.key)
  57. }
  58. }