Skip to content

Commit effbd74

Browse files
update: refactor DefaultCmabService to use Cache interface, enhance cache size and timeout, and remove CacheWithRemove interface
1 parent 4b35768 commit effbd74

File tree

5 files changed

+17
-39
lines changed

5 files changed

+17
-39
lines changed

core-api/src/main/java/com/optimizely/ab/cmab/service/DefaultCmabService.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,23 @@
3030
import com.optimizely.ab.config.Attribute;
3131
import com.optimizely.ab.config.Experiment;
3232
import com.optimizely.ab.config.ProjectConfig;
33-
import com.optimizely.ab.internal.CacheWithRemove;
33+
import com.optimizely.ab.internal.Cache;
3434
import com.optimizely.ab.internal.DefaultLRUCache;
3535
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
3636

3737
public class DefaultCmabService implements CmabService {
38-
public static final int DEFAULT_CMAB_CACHE_SIZE = 1000;
39-
public static final int DEFAULT_CMAB_CACHE_TIMEOUT_SECS = 300; // 5 minutes
38+
public static final int DEFAULT_CMAB_CACHE_SIZE = 10000;
39+
public static final int DEFAULT_CMAB_CACHE_TIMEOUT_SECS = 30*60; // 30 minutes
4040

41-
private final CacheWithRemove<CmabCacheValue> cmabCache;
41+
private final Cache<CmabCacheValue> cmabCache;
4242
private final CmabClient cmabClient;
4343
private final Logger logger;
4444

45-
public DefaultCmabService(CmabClient cmabClient, CacheWithRemove<CmabCacheValue> cmabCache) {
45+
public DefaultCmabService(CmabClient cmabClient, Cache<CmabCacheValue> cmabCache) {
4646
this(cmabClient, cmabCache, null);
4747
}
4848

49-
public DefaultCmabService(CmabClient cmabClient, CacheWithRemove<CmabCacheValue> cmabCache, Logger logger) {
49+
public DefaultCmabService(CmabClient cmabClient, Cache<CmabCacheValue> cmabCache, Logger logger) {
5050
this.cmabCache = cmabCache;
5151
this.cmabClient = cmabClient;
5252
this.logger = logger != null ? logger : LoggerFactory.getLogger(DefaultCmabService.class);
@@ -199,13 +199,13 @@ public static Builder builder() {
199199
public static class Builder {
200200
private int cmabCacheSize = DEFAULT_CMAB_CACHE_SIZE;
201201
private int cmabCacheTimeoutInSecs = DEFAULT_CMAB_CACHE_TIMEOUT_SECS;
202-
private CacheWithRemove<CmabCacheValue> customCache;
202+
private Cache<CmabCacheValue> customCache;
203203
private CmabClient client;
204204

205205
/**
206206
* Set the maximum size of the CMAB cache.
207207
*
208-
* Default value is 1000 entries.
208+
* Default value is 10000 entries.
209209
*
210210
* @param cacheSize The maximum number of entries to store in the cache
211211
* @return Builder instance
@@ -218,7 +218,7 @@ public Builder withCmabCacheSize(int cacheSize) {
218218
/**
219219
* Set the timeout duration for cached CMAB decisions.
220220
*
221-
* Default value is 300 seconds (5 minutes).
221+
* Default value is 30 * 60 seconds (30 minutes).
222222
*
223223
* @param timeoutInSecs The timeout in seconds before cached entries expire
224224
* @return Builder instance
@@ -249,7 +249,7 @@ public Builder withClient(CmabClient client) {
249249
* @param cache The custom cache instance implementing {@link Cache}
250250
* @return Builder instance
251251
*/
252-
public Builder withCustomCache(CacheWithRemove<CmabCacheValue> cache) {
252+
public Builder withCustomCache(Cache<CmabCacheValue> cache) {
253253
this.customCache = cache;
254254
return this;
255255
}
@@ -259,7 +259,7 @@ public DefaultCmabService build() {
259259
throw new IllegalStateException("CmabClient is required");
260260
}
261261

262-
CacheWithRemove<CmabCacheValue> cache = customCache != null ? customCache :
262+
Cache<CmabCacheValue> cache = customCache != null ? customCache :
263263
new DefaultLRUCache<>(cmabCacheSize, cmabCacheTimeoutInSecs);
264264

265265
return new DefaultCmabService(client, cache);

core-api/src/main/java/com/optimizely/ab/internal/Cache.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,8 @@ public interface Cache<T> {
2222
void save(String key, T value);
2323
T lookup(String key);
2424
void reset();
25+
default void remove(String key) {
26+
// Default implementation does nothing
27+
// Implementations should override this method to provide actual removal functionality
28+
}
2529
}

core-api/src/main/java/com/optimizely/ab/internal/CacheWithRemove.java

Lines changed: 0 additions & 21 deletions
This file was deleted.

core-api/src/main/java/com/optimizely/ab/internal/DefaultLRUCache.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323

2424
import com.optimizely.ab.annotations.VisibleForTesting;
2525

26-
public class DefaultLRUCache<T> implements CacheWithRemove<T> {
26+
public class DefaultLRUCache<T> implements Cache<T> {
2727

2828
private final ReentrantLock lock = new ReentrantLock();
2929

@@ -96,6 +96,7 @@ public void reset() {
9696
}
9797
}
9898

99+
@Override
99100
public void remove(String key) {
100101
if (maxSize == 0) {
101102
// Cache is disabled when maxSize = 0

core-api/src/test/java/com/optimizely/ab/OptimizelyUserContextTest.java

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import com.optimizely.ab.bucketing.UserProfileUtils;
2626
import com.optimizely.ab.config.*;
2727
import com.optimizely.ab.config.parser.ConfigParseException;
28-
import com.optimizely.ab.event.EventHandler;
2928
import com.optimizely.ab.event.EventProcessor;
3029
import com.optimizely.ab.event.ForwardingEventProcessor;
3130
import com.optimizely.ab.event.internal.ImpressionEvent;
@@ -44,8 +43,6 @@
4443
import org.junit.Test;
4544
import org.mockito.ArgumentCaptor;
4645
import org.mockito.Mockito;
47-
import org.slf4j.Logger;
48-
import org.slf4j.LoggerFactory;
4946

5047
import java.util.*;
5148
import java.util.concurrent.CountDownLatch;
@@ -57,9 +54,6 @@
5754
import static junit.framework.TestCase.assertEquals;
5855
import static junit.framework.TestCase.assertTrue;
5956
import static org.junit.Assert.*;
60-
import static org.mockito.Matchers.any;
61-
import static org.mockito.Matchers.anyObject;
62-
import static org.mockito.Matchers.eq;
6357
import static org.mockito.Mockito.*;
6458

6559
public class OptimizelyUserContextTest {

0 commit comments

Comments
 (0)