Skip to content

Commit 3312944

Browse files
Update: Refactor get_decision method
1 parent 6384e29 commit 3312944

File tree

1 file changed

+27
-22
lines changed

1 file changed

+27
-22
lines changed

optimizely/cmab/cmab_service.py

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -69,36 +69,41 @@ def get_decision(self, project_config: ProjectConfig, user_context: OptimizelyUs
6969

7070
lock_index = self._get_lock_index(user_context.user_id, rule_id)
7171
with self.locks[lock_index]:
72-
filtered_attributes = self._filter_attributes(project_config, user_context, rule_id)
72+
return self._get_decision(project_config, user_context, rule_id, options)
7373

74-
if OptimizelyDecideOption.IGNORE_CMAB_CACHE in options:
75-
return self._fetch_decision(rule_id, user_context.user_id, filtered_attributes)
74+
def _get_decision(self, project_config: ProjectConfig, user_context: OptimizelyUserContext,
75+
rule_id: str, options: List[str]) -> CmabDecision:
7676

77-
if OptimizelyDecideOption.RESET_CMAB_CACHE in options:
78-
self.cmab_cache.reset()
77+
filtered_attributes = self._filter_attributes(project_config, user_context, rule_id)
7978

80-
cache_key = self._get_cache_key(user_context.user_id, rule_id)
79+
if OptimizelyDecideOption.IGNORE_CMAB_CACHE in options:
80+
return self._fetch_decision(rule_id, user_context.user_id, filtered_attributes)
8181

82-
if OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE in options:
83-
self.cmab_cache.remove(cache_key)
82+
if OptimizelyDecideOption.RESET_CMAB_CACHE in options:
83+
self.cmab_cache.reset()
84+
85+
cache_key = self._get_cache_key(user_context.user_id, rule_id)
8486

85-
cached_value = self.cmab_cache.lookup(cache_key)
87+
if OptimizelyDecideOption.INVALIDATE_USER_CMAB_CACHE in options:
88+
self.cmab_cache.remove(cache_key)
8689

87-
attributes_hash = self._hash_attributes(filtered_attributes)
90+
cached_value = self.cmab_cache.lookup(cache_key)
8891

89-
if cached_value:
90-
if cached_value['attributes_hash'] == attributes_hash:
91-
return CmabDecision(variation_id=cached_value['variation_id'], cmab_uuid=cached_value['cmab_uuid'])
92-
else:
93-
self.cmab_cache.remove(cache_key)
92+
attributes_hash = self._hash_attributes(filtered_attributes)
9493

95-
cmab_decision = self._fetch_decision(rule_id, user_context.user_id, filtered_attributes)
96-
self.cmab_cache.save(cache_key, {
97-
'attributes_hash': attributes_hash,
98-
'variation_id': cmab_decision['variation_id'],
99-
'cmab_uuid': cmab_decision['cmab_uuid'],
100-
})
101-
return cmab_decision
94+
if cached_value:
95+
if cached_value['attributes_hash'] == attributes_hash:
96+
return CmabDecision(variation_id=cached_value['variation_id'], cmab_uuid=cached_value['cmab_uuid'])
97+
else:
98+
self.cmab_cache.remove(cache_key)
99+
100+
cmab_decision = self._fetch_decision(rule_id, user_context.user_id, filtered_attributes)
101+
self.cmab_cache.save(cache_key, {
102+
'attributes_hash': attributes_hash,
103+
'variation_id': cmab_decision['variation_id'],
104+
'cmab_uuid': cmab_decision['cmab_uuid'],
105+
})
106+
return cmab_decision
102107

103108
def _fetch_decision(self, rule_id: str, user_id: str, attributes: UserAttributes) -> CmabDecision:
104109
cmab_uuid = str(uuid.uuid4())

0 commit comments

Comments
 (0)