22
22
from .event_dispatcher import EventDispatcher , CustomEventDispatcher
23
23
from .notification_center import NotificationCenter
24
24
from .optimizely import Optimizely
25
+ from .cmab .cmab_client import DefaultCmabClient , CmabRetryConfig
26
+ from .cmab .cmab_service import DefaultCmabService , CmabCacheValue
27
+ from .odp .lru_cache import LRUCache
25
28
26
29
if TYPE_CHECKING :
27
30
# prevent circular dependenacy by skipping import at runtime
28
31
from .user_profile import UserProfileService
29
32
33
+ # Default constants for CMAB cache
34
+ DEFAULT_CMAB_CACHE_TIMEOUT = 30 * 60 * 1000 # 30 minutes in milliseconds
35
+ DEFAULT_CMAB_CACHE_SIZE = 1000
36
+
30
37
31
38
class OptimizelyFactory :
32
39
""" Optimizely factory to provides basic utility to instantiate the Optimizely
@@ -36,6 +43,8 @@ class OptimizelyFactory:
36
43
max_event_flush_interval : Optional [int ] = None
37
44
polling_interval : Optional [float ] = None
38
45
blocking_timeout : Optional [int ] = None
46
+ cmab_cache_size : int = DEFAULT_CMAB_CACHE_SIZE
47
+ cmab_cache_timeout : int = DEFAULT_CMAB_CACHE_TIMEOUT
39
48
40
49
@staticmethod
41
50
def set_batch_size (batch_size : int ) -> int :
@@ -104,16 +113,36 @@ def default_instance(sdk_key: str, datafile: Optional[str] = None) -> Optimizely
104
113
notification_center = notification_center ,
105
114
)
106
115
116
+ # Initialize CMAB components
117
+ cmab_client = DefaultCmabClient (
118
+ retry_config = CmabRetryConfig (),
119
+ logger = logger
120
+ )
121
+ cmab_cache : LRUCache [str , CmabCacheValue ] = LRUCache (OptimizelyFactory .cmab_cache_size ,
122
+ OptimizelyFactory .cmab_cache_timeout )
123
+ cmab_service = DefaultCmabService (
124
+ cmab_cache = cmab_cache ,
125
+ cmab_client = cmab_client ,
126
+ logger = logger
127
+ )
128
+
107
129
optimizely = Optimizely (
108
130
datafile , None , logger , error_handler , None , None , sdk_key , config_manager , notification_center ,
109
- event_processor
131
+ event_processor , cmab_service = cmab_service
110
132
)
111
133
return optimizely
112
134
113
135
@staticmethod
114
136
def default_instance_with_config_manager (config_manager : BaseConfigManager ) -> Optimizely :
137
+ # Initialize CMAB components
138
+ cmab_client = DefaultCmabClient (retry_config = CmabRetryConfig ())
139
+ cmab_cache : LRUCache [str , CmabCacheValue ] = LRUCache (OptimizelyFactory .cmab_cache_size ,
140
+ OptimizelyFactory .cmab_cache_timeout )
141
+ cmab_service = DefaultCmabService (cmab_cache = cmab_cache , cmab_client = cmab_client )
142
+
115
143
return Optimizely (
116
- config_manager = config_manager
144
+ config_manager = config_manager ,
145
+ cmab_service = cmab_service
117
146
)
118
147
119
148
@staticmethod
@@ -174,7 +203,21 @@ def custom_instance(
174
203
notification_center = notification_center ,
175
204
)
176
205
206
+ # Initialize CMAB components
207
+ cmab_client = DefaultCmabClient (
208
+ retry_config = CmabRetryConfig (),
209
+ logger = logger
210
+ )
211
+ cmab_cache : LRUCache [str , CmabCacheValue ] = LRUCache (OptimizelyFactory .cmab_cache_size ,
212
+ OptimizelyFactory .cmab_cache_timeout )
213
+ cmab_service = DefaultCmabService (
214
+ cmab_cache = cmab_cache ,
215
+ cmab_client = cmab_client ,
216
+ logger = logger
217
+ )
218
+
177
219
return Optimizely (
178
220
datafile , event_dispatcher , logger , error_handler , skip_json_validation , user_profile_service ,
179
- sdk_key , config_manager , notification_center , event_processor , settings = settings
221
+ sdk_key , config_manager , notification_center , event_processor , settings = settings ,
222
+ cmab_service = cmab_service
180
223
)
0 commit comments