15
15
16
16
import Foundation
17
17
18
+ #if SWIFT_PACKAGE
19
+ @_implementationOnly import GoogleUtilities_UserDefaults
20
+ #else
21
+ @_implementationOnly import GoogleUtilities
22
+ #endif // SWIFT_PACKAGE
23
+
18
24
/// CacheKey is like a "key" to a "safe". It provides necessary metadata about the current cache to
19
25
/// know if it should be expired.
20
26
struct CacheKey : Codable {
@@ -48,22 +54,22 @@ class SettingsCache: SettingsCacheClient {
48
54
49
55
/// UserDefaults holds values in memory, making access O(1) and synchronous within the app, while
50
56
/// abstracting away async disk IO.
51
- private let cache : UserDefaults = . standard
57
+ private let cache : GULUserDefaults = . standard( )
52
58
53
59
/// Converting to dictionary is O(1) because object conversion is O(1)
54
60
var cacheContent : [ String : Any ] {
55
61
get {
56
- return cache. dictionary ( forKey: UserDefaultsKeys . forContent) ?? [ : ]
62
+ return ( cache. object ( forKey: UserDefaultsKeys . forContent) as? [ String : Any ] ) ?? [ : ]
57
63
}
58
64
set {
59
- cache. set ( newValue, forKey: UserDefaultsKeys . forContent)
65
+ cache. setObject ( newValue, forKey: UserDefaultsKeys . forContent)
60
66
}
61
67
}
62
68
63
69
/// Casting to Codable from Data is O(n)
64
70
var cacheKey : CacheKey ? {
65
71
get {
66
- if let data = cache. data ( forKey: UserDefaultsKeys . forCacheKey) {
72
+ if let data = cache. object ( forKey: UserDefaultsKeys . forCacheKey) as? Data {
67
73
do {
68
74
return try JSONDecoder ( ) . decode ( CacheKey . self, from: data)
69
75
} catch {
@@ -74,7 +80,7 @@ class SettingsCache: SettingsCacheClient {
74
80
}
75
81
set {
76
82
do {
77
- try cache. set ( JSONEncoder ( ) . encode ( newValue) , forKey: UserDefaultsKeys . forCacheKey)
83
+ try cache. setObject ( JSONEncoder ( ) . encode ( newValue) , forKey: UserDefaultsKeys . forCacheKey)
78
84
} catch {
79
85
Logger . logError ( " [Settings] Encoding CacheKey failed with error: \( error) " )
80
86
}
@@ -83,7 +89,7 @@ class SettingsCache: SettingsCacheClient {
83
89
84
90
/// Removes stored cache
85
91
func removeCache( ) {
86
- cache. set ( nil , forKey: UserDefaultsKeys . forContent)
87
- cache. set ( nil , forKey: UserDefaultsKeys . forCacheKey)
92
+ cache. setObject ( nil , forKey: UserDefaultsKeys . forContent)
93
+ cache. setObject ( nil , forKey: UserDefaultsKeys . forCacheKey)
88
94
}
89
95
}
0 commit comments