Skip to content

Commit 3e07522

Browse files
committed
Adjusted snapshot to use a shared IsEnabled cache
1 parent ea8fea1 commit 3e07522

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

src/Microsoft.FeatureManagement/FeatureManagerSnapshot.cs

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,7 @@ class FeatureManagerSnapshot : IFeatureManagerSnapshot, IVariantFeatureManagerSn
1818
{
1919
private readonly IFeatureManager _featureManager;
2020
private readonly IVariantFeatureManager _variantFeatureManager;
21-
private readonly ConcurrentDictionary<string, Task<bool>> _flagCache = new ConcurrentDictionary<string, Task<bool>>();
22-
private readonly ConcurrentDictionary<string, ValueTask<bool>> _variantFlagCache = new ConcurrentDictionary<string, ValueTask<bool>>();
21+
private readonly ConcurrentDictionary<string, ValueTask<bool>> _flagCache = new ConcurrentDictionary<string, ValueTask<bool>>();
2322
private readonly ConcurrentDictionary<string, Variant> _variantCache = new ConcurrentDictionary<string, Variant>();
2423
private IEnumerable<string> _featureNames;
2524

@@ -74,26 +73,26 @@ public Task<bool> IsEnabledAsync(string feature)
7473
{
7574
return _flagCache.GetOrAdd(
7675
feature,
77-
(key) => _featureManager.IsEnabledAsync(key));
76+
(key) => new ValueTask<bool>(_featureManager.IsEnabledAsync(key))).AsTask();
7877
}
7978

8079
public Task<bool> IsEnabledAsync<TContext>(string feature, TContext context)
8180
{
8281
return _flagCache.GetOrAdd(
8382
feature,
84-
(key) => _featureManager.IsEnabledAsync(key, context));
83+
(key) => new ValueTask<bool>(_featureManager.IsEnabledAsync(key, context))).AsTask();
8584
}
8685

8786
public ValueTask<bool> IsEnabledAsync(string feature, CancellationToken cancellationToken)
8887
{
89-
return _variantFlagCache.GetOrAdd(
88+
return _flagCache.GetOrAdd(
9089
feature,
9190
(key) => _variantFeatureManager.IsEnabledAsync(key, cancellationToken));
9291
}
9392

9493
public ValueTask<bool> IsEnabledAsync<TContext>(string feature, TContext context, CancellationToken cancellationToken)
9594
{
96-
return _variantFlagCache.GetOrAdd(
95+
return _flagCache.GetOrAdd(
9796
feature,
9897
(key) => _variantFeatureManager.IsEnabledAsync(key, context, cancellationToken));
9998
}

tests/Tests.FeatureManagement/FeatureManagementTest.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1917,10 +1917,10 @@ public async Task CustomIFeatureManagerTest()
19171917
Assert.False(await featureManagerSnapshot.IsEnabledAsync("NotTest"));
19181918
Assert.False(await featureManagerSnapshot.IsEnabledAsync("OnTestFeature"));
19191919

1920-
// But use IVariantFeatureManager when using new interface
1921-
Assert.False(await featureManagerSnapshot.IsEnabledAsync("Test", CancellationToken.None));
1920+
// Use snapshot results even though IVariantFeatureManager would be called here
1921+
Assert.True(await featureManagerSnapshot.IsEnabledAsync("Test", CancellationToken.None));
19221922
Assert.False(await featureManagerSnapshot.IsEnabledAsync("NotTest", CancellationToken.None));
1923-
Assert.True(await featureManagerSnapshot.IsEnabledAsync("OnTestFeature", CancellationToken.None));
1923+
Assert.False(await featureManagerSnapshot.IsEnabledAsync("OnTestFeature", CancellationToken.None));
19241924
}
19251925

19261926
[Fact]

0 commit comments

Comments
 (0)