2222from .event_dispatcher import EventDispatcher , CustomEventDispatcher
2323from .notification_center import NotificationCenter
2424from .optimizely import Optimizely
25+ from .odp .lru_cache import LRUCache
26+ from .cmab .cmab_client import DefaultCmabClient , CmabRetryConfig
27+ from .cmab .cmab_service import DefaultCmabService , CmabCacheValue , DEFAULT_CMAB_CACHE_TIMEOUT , DEFAULT_CMAB_CACHE_SIZE
2528
2629if TYPE_CHECKING :
2730 # prevent circular dependenacy by skipping import at runtime
@@ -36,6 +39,9 @@ class OptimizelyFactory:
3639 max_event_flush_interval : Optional [int ] = None
3740 polling_interval : Optional [float ] = None
3841 blocking_timeout : Optional [int ] = None
42+ cmab_cache_size : Optional [int ] = None
43+ cmab_cache_ttl : Optional [int ] = None
44+ cmab_custom_cache : Optional [LRUCache [str , CmabCacheValue ]] = None
3945
4046 @staticmethod
4147 def set_batch_size (batch_size : int ) -> int :
@@ -75,6 +81,51 @@ def set_blocking_timeout(blocking_timeout: int) -> int:
7581 OptimizelyFactory .blocking_timeout = blocking_timeout
7682 return OptimizelyFactory .blocking_timeout
7783
84+ @staticmethod
85+ def set_cmab_cache_size (cache_size : int , logger : Optional [optimizely_logger .Logger ] = None ) -> Optional [int ]:
86+ """ Convenience method for setting the maximum number of items in CMAB cache.
87+ Args:
88+ cache_size: Maximum number of items in CMAB cache.
89+ logger: Optional logger for logging messages.
90+ """
91+ logger = logger or optimizely_logger .NoOpLogger ()
92+
93+ if not isinstance (cache_size , int ) or cache_size <= 0 :
94+ logger .error (
95+ f"CMAB cache size is invalid, setting to default size { DEFAULT_CMAB_CACHE_SIZE } ."
96+ )
97+ return None
98+
99+ OptimizelyFactory .cmab_cache_size = cache_size
100+ return OptimizelyFactory .cmab_cache_size
101+
102+ @staticmethod
103+ def set_cmab_cache_ttl (cache_ttl : int , logger : Optional [optimizely_logger .Logger ] = None ) -> Optional [int ]:
104+ """ Convenience method for setting CMAB cache TTL.
105+ Args:
106+ cache_ttl: Time in seconds for cache entries to live.
107+ logger: Optional logger for logging messages.
108+ """
109+ logger = logger or optimizely_logger .NoOpLogger ()
110+
111+ if not isinstance (cache_ttl , (int , float )) or cache_ttl <= 0 :
112+ logger .error (
113+ f"CMAB cache TTL is invalid, setting to default TTL { DEFAULT_CMAB_CACHE_TIMEOUT } ."
114+ )
115+ return None
116+
117+ OptimizelyFactory .cmab_cache_ttl = int (cache_ttl )
118+ return OptimizelyFactory .cmab_cache_ttl
119+
120+ @staticmethod
121+ def set_cmab_custom_cache (custom_cache : LRUCache [str , CmabCacheValue ]) -> LRUCache [str , CmabCacheValue ]:
122+ """ Convenience method for setting custom CMAB cache.
123+ Args:
124+ custom_cache: Cache implementation with lookup, save, remove, and reset methods.
125+ """
126+ OptimizelyFactory .cmab_custom_cache = custom_cache
127+ return OptimizelyFactory .cmab_custom_cache
128+
78129 @staticmethod
79130 def default_instance (sdk_key : str , datafile : Optional [str ] = None ) -> Optimizely :
80131 """ Returns a new optimizely instance..
@@ -104,9 +155,17 @@ def default_instance(sdk_key: str, datafile: Optional[str] = None) -> Optimizely
104155 notification_center = notification_center ,
105156 )
106157
158+ # Initialize CMAB components
159+ cmab_client = DefaultCmabClient (retry_config = CmabRetryConfig (), logger = logger )
160+ cmab_cache = OptimizelyFactory .cmab_custom_cache or LRUCache (
161+ OptimizelyFactory .cmab_cache_size or DEFAULT_CMAB_CACHE_SIZE ,
162+ OptimizelyFactory .cmab_cache_ttl or DEFAULT_CMAB_CACHE_TIMEOUT
163+ )
164+ cmab_service = DefaultCmabService (cmab_cache , cmab_client , logger )
165+
107166 optimizely = Optimizely (
108167 datafile , None , logger , error_handler , None , None , sdk_key , config_manager , notification_center ,
109- event_processor
168+ event_processor , cmab_service = cmab_service
110169 )
111170 return optimizely
112171
@@ -174,7 +233,16 @@ def custom_instance(
174233 notification_center = notification_center ,
175234 )
176235
236+ # Initialize CMAB components
237+ cmab_client = DefaultCmabClient (retry_config = CmabRetryConfig (), logger = logger )
238+ cmab_cache = OptimizelyFactory .cmab_custom_cache or LRUCache (
239+ OptimizelyFactory .cmab_cache_size or DEFAULT_CMAB_CACHE_SIZE ,
240+ OptimizelyFactory .cmab_cache_ttl or DEFAULT_CMAB_CACHE_TIMEOUT
241+ )
242+ cmab_service = DefaultCmabService (cmab_cache , cmab_client , logger )
243+
177244 return Optimizely (
178245 datafile , event_dispatcher , logger , error_handler , skip_json_validation , user_profile_service ,
179- sdk_key , config_manager , notification_center , event_processor , settings = settings
246+ sdk_key , config_manager , notification_center , event_processor , settings = settings ,
247+ cmab_service = cmab_service
180248 )
0 commit comments