Skip to content

Commit 88b4f1e

Browse files
update: integrate CMAB components into OptimizelyFactory
1 parent 82ec019 commit 88b4f1e

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

optimizely/optimizely_factory.py

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,18 @@
2222
from .event_dispatcher import EventDispatcher, CustomEventDispatcher
2323
from .notification_center import NotificationCenter
2424
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
2528

2629
if TYPE_CHECKING:
2730
# prevent circular dependenacy by skipping import at runtime
2831
from .user_profile import UserProfileService
2932

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+
3037

3138
class OptimizelyFactory:
3239
""" Optimizely factory to provides basic utility to instantiate the Optimizely
@@ -36,6 +43,8 @@ class OptimizelyFactory:
3643
max_event_flush_interval: Optional[int] = None
3744
polling_interval: Optional[float] = None
3845
blocking_timeout: Optional[int] = None
46+
cmab_cache_size: int = DEFAULT_CMAB_CACHE_SIZE
47+
cmab_cache_timeout: int = DEFAULT_CMAB_CACHE_TIMEOUT
3948

4049
@staticmethod
4150
def set_batch_size(batch_size: int) -> int:
@@ -104,16 +113,36 @@ def default_instance(sdk_key: str, datafile: Optional[str] = None) -> Optimizely
104113
notification_center=notification_center,
105114
)
106115

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+
107129
optimizely = Optimizely(
108130
datafile, None, logger, error_handler, None, None, sdk_key, config_manager, notification_center,
109-
event_processor
131+
event_processor, cmab_service=cmab_service
110132
)
111133
return optimizely
112134

113135
@staticmethod
114136
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+
115143
return Optimizely(
116-
config_manager=config_manager
144+
config_manager=config_manager,
145+
cmab_service=cmab_service
117146
)
118147

119148
@staticmethod
@@ -174,7 +203,21 @@ def custom_instance(
174203
notification_center=notification_center,
175204
)
176205

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+
177219
return Optimizely(
178220
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
180223
)

0 commit comments

Comments
 (0)