@@ -44,7 +44,7 @@ protocol CmabService {
4444typealias CmabCache = LruCache < String , CmabCacheValue >
4545
4646class DefaultCmabService : CmabService {
47- typealias UserAttributes = [ String : Any ? ]
47+ typealias UserAttributes = [ String : Any ? ]
4848
4949 let cmabClient : CmabClient
5050 let cmabCache : CmabCache
@@ -97,33 +97,38 @@ class DefaultCmabService: CmabService {
9797 let userId = userContext. userId
9898
9999 if options. contains ( . ignoreCmabCache) {
100- self . logger. i ( " Ignoring CMAB cache. " )
100+ self . logger. i ( " Ignoring CMAB cache for user \( userId ) and rule \( ruleId ) " )
101101 fetchDecision ( ruleId: ruleId, userId: userId, attributes: filteredAttributes, completion: completion)
102102 return
103103 }
104104
105105 if options. contains ( . resetCmabCache) {
106- self . logger. i ( " Resetting CMAB cache. " )
106+ self . logger. i ( " Resetting CMAB cache for user \( userId ) and rule \( ruleId ) " )
107107 cmabCache. reset ( )
108108 }
109109
110110 let cacheKey = getCacheKey ( userId: userId, ruleId: ruleId)
111111
112112 if options. contains ( . invalidateUserCmabCache) {
113- self . logger. i ( " Invalidating user CMAB cache. " )
113+ self . logger. i ( " Invalidating CMAB cache for user \( userId ) and rule \( ruleId ) " )
114114 self . cmabCache. remove ( key: cacheKey)
115115 }
116116
117117 let attributesHash = hashAttributes ( filteredAttributes)
118118
119- if let cachedValue = cmabCache. lookup ( key: cacheKey) , cachedValue. attributesHash == attributesHash {
120- let decision = CmabDecision ( variationId: cachedValue. variationId, cmabUUID: cachedValue. cmabUUID)
121- self . logger. i ( " Returning cached CMAB decision. " )
122- completion ( . success( decision) )
123- return
119+ if let cachedValue = cmabCache. lookup ( key: cacheKey) {
120+ if cachedValue. attributesHash == attributesHash {
121+ let decision = CmabDecision ( variationId: cachedValue. variationId, cmabUUID: cachedValue. cmabUUID)
122+ self . logger. i ( " CMAB cache hit for user \( userId) and rule \( ruleId) " )
123+ completion ( . success( decision) )
124+ return
125+ } else {
126+ self . logger. i ( " CMAB cache attributes mismatch for user \( userId) and rule \( ruleId) , fetching new decision " )
127+ cmabCache. remove ( key: cacheKey)
128+ }
129+
124130 } else {
125- self . logger. i ( " CMAB decision not found in cache. " )
126- cmabCache. remove ( key: cacheKey)
131+ self . logger. i ( " CMAB cache miss for user \( userId) and rule \( ruleId) " )
127132 }
128133
129134 fetchDecision ( ruleId: ruleId, userId: userId, attributes: filteredAttributes) { result in
@@ -133,7 +138,6 @@ class DefaultCmabService: CmabService {
133138 variationId: decision. variationId,
134139 cmabUUID: decision. cmabUUID
135140 )
136- self . logger. i ( " Featched CMAB decision and cached it. " )
137141 self . cmabCache. save ( key: cacheKey, value: cacheValue)
138142 }
139143 completion ( result)
@@ -148,11 +152,11 @@ class DefaultCmabService: CmabService {
148152 cmabClient. fetchDecision ( ruleId: ruleId, userId: userId, attributes: attributes, cmabUUID: cmabUUID) { result in
149153 switch result {
150154 case . success( let variaitonId) :
151- self . logger. i ( " Fetched CMAB decision: \( variaitonId) " )
152155 let decision = CmabDecision ( variationId: variaitonId, cmabUUID: cmabUUID)
156+ self . logger. i ( " Featched CMAB decision, (variationId: \( decision. variationId) , cmabUUID: \( decision. cmabUUID) ) " )
153157 completion ( . success( decision) )
154158 case . failure( let error) :
155- self . logger. e ( " Failed to fetch CMAB decision: \( error) " )
159+ self . logger. e ( " Failed to fetch CMAB decision, error : \( error) " )
156160 completion ( . failure( error) )
157161 }
158162 }
0 commit comments