|
3 | 3 | import java.util.*;
|
4 | 4 |
|
5 | 5 | import com.google.gson.JsonElement;
|
| 6 | +import com.google.gson.JsonObject; |
6 | 7 | import growthbook.sdk.java.callback.ExperimentRunCallback;
|
7 | 8 | import growthbook.sdk.java.callback.FeatureRefreshCallback;
|
8 | 9 | import growthbook.sdk.java.evaluators.ExperimentEvaluator;
|
|
17 | 18 | import growthbook.sdk.java.multiusermode.configurations.GlobalContext;
|
18 | 19 | import growthbook.sdk.java.multiusermode.configurations.Options;
|
19 | 20 | import growthbook.sdk.java.multiusermode.configurations.UserContext;
|
20 |
| -import growthbook.sdk.java.multiusermode.util.TransformationUtil; |
21 | 21 | import growthbook.sdk.java.repository.GBFeaturesRepository;
|
| 22 | +import growthbook.sdk.java.sandbox.CacheManagerFactory; |
| 23 | +import growthbook.sdk.java.sandbox.CacheMode; |
| 24 | +import growthbook.sdk.java.sandbox.GbCacheManager; |
22 | 25 | import growthbook.sdk.java.util.GrowthBookJsonUtils;
|
23 | 26 | import lombok.extern.slf4j.Slf4j;
|
24 | 27 |
|
@@ -58,14 +61,20 @@ public boolean initialize() {
|
58 | 61 | try {
|
59 | 62 |
|
60 | 63 | if (repository == null) {
|
| 64 | + GbCacheManager cm = this.options.getCacheManager() != null |
| 65 | + ? this.options.getCacheManager() |
| 66 | + : CacheManagerFactory.create(this.options.getCacheMode(), this.options.getCacheDirectory() |
| 67 | + ); |
| 68 | + |
61 | 69 | repository = GBFeaturesRepository.builder()
|
62 | 70 | .apiHost(this.options.getApiHost())
|
63 | 71 | .clientKey(this.options.getClientKey())
|
64 | 72 | .decryptionKey(this.options.getDecryptionKey())
|
65 | 73 | .refreshStrategy(this.options.getRefreshStrategy())
|
66 |
| - .isCacheDisabled(this.options.getIsCacheDisabled()) |
67 |
| - .cacheManager(this.options.getCacheManager()) |
68 |
| - .requestBodyForRemoteEval(configurePayloadForRemoteEval(this.options)) // if we don't want to pre-fetch for remote eval we can delete this line |
| 74 | + .isCacheDisabled(this.options.getIsCacheDisabled() || this.options.getCacheMode() == CacheMode.NONE) |
| 75 | + .cacheManager(cm) |
| 76 | + // if we don't want to pre-fetch for remote eval we can delete this line |
| 77 | + .requestBodyForRemoteEval(configurePayloadForRemoteEval(this.options)) |
69 | 78 | .build();
|
70 | 79 |
|
71 | 80 | // Add featureRefreshCallback
|
@@ -227,31 +236,19 @@ public void onError(Throwable throwable) {
|
227 | 236 | }
|
228 | 237 |
|
229 | 238 | private EvaluationContext getEvalContext(UserContext userContext) {
|
230 |
| - // Refresh features on each evaluation to ensure cache refresh is triggered |
231 |
| - this.globalContext.setFeatures(repository.getParsedFeatures()); |
232 |
| - |
233 |
| - HashMap<String, JsonElement> globalAttributes = null; |
| 239 | + // Merge attributes using JsonObject to avoid parse/serialize churn |
| 240 | + JsonObject merged = new JsonObject(); |
234 | 241 | if (this.options.getGlobalAttributes() != null) {
|
235 |
| - globalAttributes = GrowthBookJsonUtils.getInstance().gson.fromJson(this.options.getGlobalAttributes(), HashMap.class); |
236 |
| - } |
237 |
| - |
238 |
| - HashMap<String, JsonElement> userAttributes = null; |
239 |
| - if (userContext.getAttributes() != null) { |
240 |
| - userAttributes = GrowthBookJsonUtils.getInstance().gson.fromJson(userContext.getAttributes(), HashMap.class); |
| 242 | + merged = GrowthBookJsonUtils.getInstance().gson.fromJson(this.options.getGlobalAttributes(), JsonObject.class); |
| 243 | + if (merged == null) merged = new JsonObject(); |
241 | 244 | }
|
242 |
| - |
243 |
| - if (globalAttributes != null) { |
244 |
| - if (userAttributes != null) { |
245 |
| - globalAttributes.putAll(userAttributes); |
| 245 | + JsonObject userAttrs = userContext.getAttributes(); |
| 246 | + if (userAttrs != null) { |
| 247 | + for (Map.Entry<String, JsonElement> e : userAttrs.entrySet()) { |
| 248 | + merged.add(e.getKey(), e.getValue()); |
246 | 249 | }
|
247 |
| - } else { |
248 |
| - globalAttributes = userAttributes; |
249 | 250 | }
|
250 |
| - |
251 |
| - String attributesJson = GrowthBookJsonUtils.getInstance().gson.toJson(globalAttributes); |
252 |
| - |
253 |
| - UserContext updatedUserContext = userContext.witAttributesJson(attributesJson); |
254 |
| - |
| 251 | + UserContext updatedUserContext = userContext.withAttributes(merged); |
255 | 252 | return new EvaluationContext(this.globalContext, updatedUserContext, new EvaluationContext.StackContext(), this.options);
|
256 | 253 | }
|
257 | 254 |
|
|
0 commit comments