Skip to content

Commit 9c6e9bb

Browse files
fix: for GetVariation & Activate never use cached user profile
1 parent d6258f1 commit 9c6e9bb

File tree

4 files changed

+21
-24
lines changed

4 files changed

+21
-24
lines changed

.github/workflows/csharp.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ on:
88

99
jobs:
1010
failOnDraftPullRequest:
11+
name: Fail If Draft Pull Request
1112
if: github.event.pull_request.draft == true
1213
runs-on: ubuntu-latest
1314
steps:

OptimizelySDK/Bucketing/DecisionService.cs

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -85,35 +85,26 @@ public DecisionService(Bucketer bucketer, IErrorHandler errorHandler,
8585
#endif
8686
}
8787

88-
/// <summary>
89-
/// Get a Variation of an Experiment for a user to be allocated into.
90-
/// </summary>
91-
/// <param name = "experiment" > The Experiment the user will be bucketed into.</param>
92-
/// <param name = "user" > Optimizely user context.</param>
93-
/// <param name = "config" > Project config.</param>
94-
/// <returns>The Variation the user is allocated into.</returns>
95-
public virtual Result<Variation> GetVariation(Experiment experiment,
96-
OptimizelyUserContext user,
97-
ProjectConfig config
98-
)
99-
{
100-
return GetVariation(experiment, user, config, new OptimizelyDecideOption[] { });
101-
}
102-
10388
/// <summary>
10489
/// Get a Variation of an Experiment for a user to be allocated into.
10590
/// </summary>
10691
/// <param name="experiment">The Experiment the user will be bucketed into.</param>
10792
/// <param name="user">optimizely user context.</param>
10893
/// <param name="config">Project Config.</param>
10994
/// <param name="options">An array of decision options.</param>
95+
/// <param name="forceUserProfileLookup">Whether to force a lookup of the user profile when UPS is enabled.</param>
11096
/// <returns>The Variation the user is allocated into.</returns>
11197
public virtual Result<Variation> GetVariation(Experiment experiment,
11298
OptimizelyUserContext user,
11399
ProjectConfig config,
114-
OptimizelyDecideOption[] options
100+
OptimizelyDecideOption[] options = null,
101+
bool forceUserProfileLookup = false
115102
)
116103
{
104+
if (options == null)
105+
{
106+
options = new OptimizelyDecideOption[] { };
107+
}
117108
var reasons = new DecisionReasons();
118109
var userId = user.GetUserId();
119110
if (!ExperimentUtils.IsExperimentActive(experiment, Logger))
@@ -149,7 +140,7 @@ OptimizelyDecideOption[] options
149140
{
150141
try
151142
{
152-
userProfile = _userProfileCache.GetUserProfile(userId);
143+
userProfile = _userProfileCache.GetUserProfile(userId, forceUserProfileLookup);
153144
decisionVariationResult = GetStoredVariation(experiment, userProfile, config);
154145
reasons += decisionVariationResult.DecisionReasons;
155146
if (decisionVariationResult.ResultObject != null)

OptimizelySDK/Bucketing/UserProfileCache.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ public UserProfileCache(UserProfileService userProfileService, ILogger logger)
3333
_logger = logger;
3434
}
3535

36-
public UserProfile GetUserProfile(string userId)
36+
public UserProfile GetUserProfile(string userId, bool forceLookup = false)
3737
{
38-
if (_cache.TryGetValue(userId, out var userProfile))
38+
if (_cache.TryGetValue(userId, out var userProfile) && !forceLookup)
3939
{
4040
return _cache[userId];
4141
}

OptimizelySDK/Optimizely.cs

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ public Variation Activate(string experimentKey, string userId,
310310
return null;
311311
}
312312

313-
var variation = GetVariation(experimentKey, userId, config, userAttributes);
313+
var variation = GetVariation(experimentKey, userId, config, userAttributes,
314+
forceUserProfileLookup: true);
314315

315316
if (variation == null || variation.Key == null)
316317
{
@@ -404,7 +405,8 @@ public Variation GetVariation(string experimentKey, string userId,
404405
)
405406
{
406407
var config = ProjectConfigManager?.GetConfig();
407-
return GetVariation(experimentKey, userId, config, userAttributes);
408+
return GetVariation(experimentKey, userId, config, userAttributes,
409+
forceUserProfileLookup: true);
408410
}
409411

410412
/// <summary>
@@ -414,9 +416,10 @@ public Variation GetVariation(string experimentKey, string userId,
414416
/// <param name="userId">ID for the user</param>
415417
/// <param name="config">ProjectConfig to be used for variation</param>
416418
/// <param name="userAttributes">Attributes for the users</param>
419+
/// <param name="forceUserProfileLookup">Whether to force a lookup of the user profile when UPS is enabled.</param>
417420
/// <returns>null|Variation Representing variation</returns>
418421
private Variation GetVariation(string experimentKey, string userId, ProjectConfig config,
419-
UserAttributes userAttributes = null
422+
UserAttributes userAttributes = null, bool forceUserProfileLookup = false
420423
)
421424
{
422425
if (config == null)
@@ -442,8 +445,10 @@ private Variation GetVariation(string experimentKey, string userId, ProjectConfi
442445
userAttributes = userAttributes ?? new UserAttributes();
443446

444447
var userContext = CreateUserContextCopy(userId, userAttributes);
445-
var variation = DecisionService.GetVariation(experiment, userContext, config)
446-
?.ResultObject;
448+
var variation = DecisionService.
449+
GetVariation(experiment, userContext, config,
450+
forceUserProfileLookup: forceUserProfileLookup)?.
451+
ResultObject;
447452
var decisionInfo = new Dictionary<string, object>
448453
{
449454
{ "experimentKey", experimentKey }, { "variationKey", variation?.Key },

0 commit comments

Comments
 (0)