Skip to content

Commit

Permalink
fix: for GetVariation & Activate never use cached user profile
Browse files Browse the repository at this point in the history
  • Loading branch information
mikechu-optimizely committed Oct 18, 2024
1 parent d6258f1 commit 9c6e9bb
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 24 deletions.
1 change: 1 addition & 0 deletions .github/workflows/csharp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ on:

jobs:
failOnDraftPullRequest:
name: Fail If Draft Pull Request
if: github.event.pull_request.draft == true
runs-on: ubuntu-latest
steps:
Expand Down
25 changes: 8 additions & 17 deletions OptimizelySDK/Bucketing/DecisionService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,26 @@ public DecisionService(Bucketer bucketer, IErrorHandler errorHandler,
#endif
}

/// <summary>
/// Get a Variation of an Experiment for a user to be allocated into.
/// </summary>
/// <param name = "experiment" > The Experiment the user will be bucketed into.</param>
/// <param name = "user" > Optimizely user context.</param>
/// <param name = "config" > Project config.</param>
/// <returns>The Variation the user is allocated into.</returns>
public virtual Result<Variation> GetVariation(Experiment experiment,
OptimizelyUserContext user,
ProjectConfig config
)
{
return GetVariation(experiment, user, config, new OptimizelyDecideOption[] { });
}

/// <summary>
/// Get a Variation of an Experiment for a user to be allocated into.
/// </summary>
/// <param name="experiment">The Experiment the user will be bucketed into.</param>
/// <param name="user">optimizely user context.</param>
/// <param name="config">Project Config.</param>
/// <param name="options">An array of decision options.</param>
/// <param name="forceUserProfileLookup">Whether to force a lookup of the user profile when UPS is enabled.</param>
/// <returns>The Variation the user is allocated into.</returns>
public virtual Result<Variation> GetVariation(Experiment experiment,
OptimizelyUserContext user,
ProjectConfig config,
OptimizelyDecideOption[] options
OptimizelyDecideOption[] options = null,
bool forceUserProfileLookup = false
)
{
if (options == null)
{
options = new OptimizelyDecideOption[] { };
}
var reasons = new DecisionReasons();
var userId = user.GetUserId();
if (!ExperimentUtils.IsExperimentActive(experiment, Logger))
Expand Down Expand Up @@ -149,7 +140,7 @@ OptimizelyDecideOption[] options
{
try
{
userProfile = _userProfileCache.GetUserProfile(userId);
userProfile = _userProfileCache.GetUserProfile(userId, forceUserProfileLookup);
decisionVariationResult = GetStoredVariation(experiment, userProfile, config);
reasons += decisionVariationResult.DecisionReasons;
if (decisionVariationResult.ResultObject != null)
Expand Down
4 changes: 2 additions & 2 deletions OptimizelySDK/Bucketing/UserProfileCache.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@ public UserProfileCache(UserProfileService userProfileService, ILogger logger)
_logger = logger;
}

public UserProfile GetUserProfile(string userId)
public UserProfile GetUserProfile(string userId, bool forceLookup = false)
{
if (_cache.TryGetValue(userId, out var userProfile))
if (_cache.TryGetValue(userId, out var userProfile) && !forceLookup)
{
return _cache[userId];
}
Expand Down
15 changes: 10 additions & 5 deletions OptimizelySDK/Optimizely.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ public Variation Activate(string experimentKey, string userId,
return null;
}

var variation = GetVariation(experimentKey, userId, config, userAttributes);
var variation = GetVariation(experimentKey, userId, config, userAttributes,
forceUserProfileLookup: true);

if (variation == null || variation.Key == null)
{
Expand Down Expand Up @@ -404,7 +405,8 @@ public Variation GetVariation(string experimentKey, string userId,
)
{
var config = ProjectConfigManager?.GetConfig();
return GetVariation(experimentKey, userId, config, userAttributes);
return GetVariation(experimentKey, userId, config, userAttributes,
forceUserProfileLookup: true);
}

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

var userContext = CreateUserContextCopy(userId, userAttributes);
var variation = DecisionService.GetVariation(experiment, userContext, config)
?.ResultObject;
var variation = DecisionService.
GetVariation(experiment, userContext, config,
forceUserProfileLookup: forceUserProfileLookup)?.
ResultObject;
var decisionInfo = new Dictionary<string, object>
{
{ "experimentKey", experimentKey }, { "variationKey", variation?.Key },
Expand Down

0 comments on commit 9c6e9bb

Please sign in to comment.