Skip to content

Commit 8c96222

Browse files
update: introduce CacheWithRemove interface and refactor DefaultCmabService to utilize it
1 parent e459321 commit 8c96222

File tree

5 files changed

+35
-13
lines changed

5 files changed

+35
-13
lines changed

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

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,15 @@
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.Cache;
33+
import com.optimizely.ab.internal.CacheWithRemove;
3434
import com.optimizely.ab.internal.DefaultLRUCache;
3535
import com.optimizely.ab.optimizelydecision.OptimizelyDecideOption;
3636

3737
public class DefaultCmabService implements CmabService {
3838
public static final int DEFAULT_CMAB_CACHE_SIZE = 1000;
3939
public static final int DEFAULT_CMAB_CACHE_TIMEOUT_SECS = 300; // 5 minutes
4040

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

@@ -48,7 +48,7 @@ public class DefaultCmabService implements CmabService {
4848
// this.logger = logger;
4949
// }
5050

51-
public DefaultCmabService(CmabClient cmabClient, Cache<CmabCacheValue> cmabCache, Logger logger) {
51+
public DefaultCmabService(CmabClient cmabClient, CacheWithRemove<CmabCacheValue> cmabCache, Logger logger) {
5252
this.cmabCache = cmabCache;
5353
this.cmabClient = cmabClient;
5454
this.logger = logger;
@@ -200,7 +200,7 @@ public static Builder builder() {
200200
public static class Builder {
201201
private int cmabCacheSize = DEFAULT_CMAB_CACHE_SIZE;
202202
private int cmabCacheTimeoutInSecs = DEFAULT_CMAB_CACHE_TIMEOUT_SECS;
203-
private Cache<CmabCacheValue> customCache;
203+
private CacheWithRemove<CmabCacheValue> customCache;
204204
private CmabClient client;
205205
private Logger logger;
206206

@@ -251,7 +251,7 @@ public Builder withClient(CmabClient client) {
251251
* @param cache The custom cache instance implementing {@link Cache}
252252
* @return Builder instance
253253
*/
254-
public Builder withCustomCache(Cache<CmabCacheValue> cache) {
254+
public Builder withCustomCache(CacheWithRemove<CmabCacheValue> cache) {
255255
this.customCache = cache;
256256
return this;
257257
}
@@ -278,7 +278,7 @@ public DefaultCmabService build() {
278278
logger = LoggerFactory.getLogger(DefaultCmabService.class);
279279
}
280280

281-
Cache<CmabCacheValue> cache = customCache != null ? customCache :
281+
CacheWithRemove<CmabCacheValue> cache = customCache != null ? customCache :
282282
new DefaultLRUCache<>(cmabCacheSize, cmabCacheTimeoutInSecs);
283283

284284

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,4 @@ public interface Cache<T> {
2222
void save(String key, T value);
2323
T lookup(String key);
2424
void reset();
25-
void remove(String key);
2625
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
*
3+
* Copyright 2025, Optimizely
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package com.optimizely.ab.internal;
18+
19+
public interface CacheWithRemove<T> extends Cache<T> {
20+
void remove(String key);
21+
}

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

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616
*/
1717
package com.optimizely.ab.internal;
1818

19-
import com.optimizely.ab.annotations.VisibleForTesting;
20-
21-
import java.util.*;
19+
import java.util.Date;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
2222
import java.util.concurrent.locks.ReentrantLock;
2323

24+
import com.optimizely.ab.annotations.VisibleForTesting;
25+
2426
public class DefaultLRUCache<T> implements Cache<T> {
2527

2628
private final ReentrantLock lock = new ReentrantLock();

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
import com.optimizely.ab.event.AsyncEventHandler;
3333
import com.optimizely.ab.event.BatchEventProcessor;
3434
import com.optimizely.ab.event.EventHandler;
35-
import com.optimizely.ab.internal.Cache;
35+
import com.optimizely.ab.internal.CacheWithRemove;
3636
import com.optimizely.ab.internal.PropertyUtils;
3737
import com.optimizely.ab.notification.NotificationCenter;
3838
import com.optimizely.ab.odp.DefaultODPApiManager;
@@ -60,7 +60,7 @@
6060
public final class OptimizelyFactory {
6161
private static final Logger logger = LoggerFactory.getLogger(OptimizelyFactory.class);
6262

63-
private static Cache<CmabCacheValue> customCmabCache;
63+
private static CacheWithRemove<CmabCacheValue> customCmabCache;
6464

6565
/**
6666
* Convenience method for setting the maximum number of events contained within a batch.
@@ -244,7 +244,7 @@ public static void setCmabCacheTimeoutInSecs(int timeoutInSecs) {
244244
*
245245
* @param cache The custom cache implementation
246246
*/
247-
public static void setCustomCmabCache(Cache<CmabCacheValue> cache) {
247+
public static void setCustomCmabCache(CacheWithRemove<CmabCacheValue> cache) {
248248
if (cache == null) {
249249
logger.warn("Custom CMAB cache cannot be null. Reverting to default configuration.");
250250
return;

0 commit comments

Comments
 (0)