Skip to content

Commit 4feb857

Browse files
update: refactor OptimizelyFactory to remove CMAB cache methods and adjust instance creation logic
1 parent 7c97734 commit 4feb857

File tree

1 file changed

+21
-77
lines changed

1 file changed

+21
-77
lines changed

core-httpclient-impl/src/main/java/com/optimizely/ab/OptimizelyFactory.java

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,13 @@
2424

2525
import com.optimizely.ab.cmab.DefaultCmabClient;
2626
import com.optimizely.ab.cmab.client.CmabClientConfig;
27-
import com.optimizely.ab.cmab.service.CmabCacheValue;
2827
import com.optimizely.ab.cmab.service.DefaultCmabService;
2928
import com.optimizely.ab.config.HttpProjectConfigManager;
3029
import com.optimizely.ab.config.ProjectConfig;
3130
import com.optimizely.ab.config.ProjectConfigManager;
3231
import com.optimizely.ab.event.AsyncEventHandler;
3332
import com.optimizely.ab.event.BatchEventProcessor;
3433
import com.optimizely.ab.event.EventHandler;
35-
import com.optimizely.ab.internal.CacheWithRemove;
3634
import com.optimizely.ab.internal.PropertyUtils;
3735
import com.optimizely.ab.notification.NotificationCenter;
3836
import com.optimizely.ab.odp.DefaultODPApiManager;
@@ -60,8 +58,6 @@
6058
public final class OptimizelyFactory {
6159
private static final Logger logger = LoggerFactory.getLogger(OptimizelyFactory.class);
6260

63-
private static CacheWithRemove<CmabCacheValue> customCmabCache;
64-
6561
/**
6662
* Convenience method for setting the maximum number of events contained within a batch.
6763
* {@link AsyncEventHandler}
@@ -210,48 +206,6 @@ public static void setDatafileAccessToken(String datafileAccessToken) {
210206
PropertyUtils.set(HttpProjectConfigManager.CONFIG_DATAFILE_AUTH_TOKEN, datafileAccessToken);
211207
}
212208

213-
/**
214-
* Convenience method for setting the CMAB cache size.
215-
* {@link DefaultCmabService.Builder#withCmabCacheSize(int)}
216-
*
217-
* @param cacheSize The maximum number of CMAB cache entries
218-
*/
219-
public static void setCmabCacheSize(int cacheSize) {
220-
if (cacheSize <= 0) {
221-
logger.warn("CMAB cache size cannot be <= 0. Reverting to default configuration.");
222-
return;
223-
}
224-
PropertyUtils.set("optimizely.cmab.cache.size", Integer.toString(cacheSize));
225-
}
226-
227-
/**
228-
* Convenience method for setting the CMAB cache timeout.
229-
* {@link DefaultCmabService.Builder#withCmabCacheTimeoutInSecs(int)}
230-
*
231-
* @param timeoutInSecs The timeout in seconds before CMAB cache entries expire
232-
*/
233-
public static void setCmabCacheTimeoutInSecs(int timeoutInSecs) {
234-
if (timeoutInSecs <= 0) {
235-
logger.warn("CMAB cache timeout cannot be <= 0. Reverting to default configuration.");
236-
return;
237-
}
238-
PropertyUtils.set("optimizely.cmab.cache.timeout", Integer.toString(timeoutInSecs));
239-
}
240-
241-
/**
242-
* Convenience method for setting a custom CMAB cache implementation.
243-
* {@link DefaultCmabService.Builder#withCustomCache(Cache)}
244-
*
245-
* @param cache The custom cache implementation
246-
*/
247-
public static void setCustomCmabCache(CacheWithRemove<CmabCacheValue> cache) {
248-
if (cache == null) {
249-
logger.warn("Custom CMAB cache cannot be null. Reverting to default configuration.");
250-
return;
251-
}
252-
customCmabCache = cache;
253-
}
254-
255209
/**
256210
* Returns a new Optimizely instance based on preset configuration.
257211
*
@@ -406,6 +360,20 @@ public static Optimizely newDefaultInstance(ProjectConfigManager configManager,
406360
* @return A new Optimizely instance
407361
* */
408362
public static Optimizely newDefaultInstance(ProjectConfigManager configManager, NotificationCenter notificationCenter, EventHandler eventHandler, ODPApiManager odpApiManager) {
363+
return newDefaultInstance(configManager, notificationCenter, eventHandler, odpApiManager, null);
364+
}
365+
366+
/**
367+
* Returns a new Optimizely instance based on preset configuration.
368+
*
369+
* @param configManager The {@link ProjectConfigManager} supplied to Optimizely instance.
370+
* @param notificationCenter The {@link NotificationCenter} supplied to Optimizely instance.
371+
* @param eventHandler The {@link EventHandler} supplied to Optimizely instance.
372+
* @param odpApiManager The {@link ODPApiManager} supplied to Optimizely instance.
373+
* @param cmabService The {@link DefaultCmabService} supplied to Optimizely instance.
374+
* @return A new Optimizely instance
375+
* */
376+
public static Optimizely newDefaultInstance(ProjectConfigManager configManager, NotificationCenter notificationCenter, EventHandler eventHandler, ODPApiManager odpApiManager, DefaultCmabService cmabService) {
409377
if (notificationCenter == null) {
410378
notificationCenter = new NotificationCenter();
411379
}
@@ -419,44 +387,20 @@ public static Optimizely newDefaultInstance(ProjectConfigManager configManager,
419387
.withApiManager(odpApiManager != null ? odpApiManager : new DefaultODPApiManager())
420388
.build();
421389

422-
DefaultCmabClient defaultCmabClient = new DefaultCmabClient(CmabClientConfig.withDefaultRetry());
423-
424-
DefaultCmabService.Builder cmabBuilder = DefaultCmabService.builder()
425-
.withClient(defaultCmabClient);
426-
427-
// Always apply cache size from properties if set
428-
String cacheSizeStr = PropertyUtils.get("optimizely.cmab.cache.size");
429-
if (cacheSizeStr != null) {
430-
try {
431-
cmabBuilder.withCmabCacheSize(Integer.parseInt(cacheSizeStr));
432-
} catch (NumberFormatException e) {
433-
logger.warn("Invalid CMAB cache size property value: {}", cacheSizeStr);
434-
}
390+
// If no cmabService provided, create default one
391+
if (cmabService == null) {
392+
DefaultCmabClient defaultCmabClient = new DefaultCmabClient(CmabClientConfig.withDefaultRetry());
393+
cmabService = DefaultCmabService.builder()
394+
.withClient(defaultCmabClient)
395+
.build();
435396
}
436-
437-
// Always apply cache timeout from properties if set
438-
String cacheTimeoutStr = PropertyUtils.get("optimizely.cmab.cache.timeout");
439-
if (cacheTimeoutStr != null) {
440-
try {
441-
cmabBuilder.withCmabCacheTimeoutInSecs(Integer.parseInt(cacheTimeoutStr));
442-
} catch (NumberFormatException e) {
443-
logger.warn("Invalid CMAB cache timeout property value: {}", cacheTimeoutStr);
444-
}
445-
}
446-
447-
// If custom cache is provided, it overrides the size/timeout settings
448-
if (customCmabCache != null) {
449-
cmabBuilder.withCustomCache(customCmabCache);
450-
}
451-
452-
DefaultCmabService defaultCmabService = cmabBuilder.build();
453397

454398
return Optimizely.builder()
455399
.withEventProcessor(eventProcessor)
456400
.withConfigManager(configManager)
457401
.withNotificationCenter(notificationCenter)
458402
.withODPManager(odpManager)
459-
.withCmabService(defaultCmabService)
403+
.withCmabService(cmabService)
460404
.build();
461405
}
462406
}

0 commit comments

Comments
 (0)