Skip to content

Commit 1a7597f

Browse files
authored
optional (#171)
* optional * rem ext * fully optional * rem none policy * rem bad test * rem isenabled * test no events * merge * rem prop fix perf
1 parent 4b5c32d commit 1a7597f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+921
-935
lines changed

BitFaster.Caching.Benchmarks/Lru/LruCycleBench.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ public class LruCycleBench
4141
[GlobalSetup]
4242
public void GlobalSetup()
4343
{
44-
concurrentLruEvent.Events.ItemRemoved += OnItemRemoved;
44+
concurrentLruEvent.Events.Value.ItemRemoved += OnItemRemoved;
4545
}
4646

4747
public static int field;

BitFaster.Caching.HitRateAnalysis/Arc/Analysis.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public Analysis(int cacheSize)
2424

2525
public int CacheSize => concurrentLru.Capacity;
2626

27-
public double ConcurrentLruHitRate => concurrentLru.Metrics.HitRatio * 100;
27+
public double ConcurrentLruHitRate => concurrentLru.Metrics.Value.HitRatio * 100;
2828

29-
public double ClassicLruHitRate => classicLru.Metrics.HitRatio * 100;
29+
public double ClassicLruHitRate => classicLru.Metrics.Value.HitRatio * 100;
3030

3131
public void TestKey(long key)
3232
{

BitFaster.Caching.HitRateAnalysis/Glimpse/Analysis.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public Analysis(int cacheSize)
2424

2525
public int CacheSize => this.concurrentLru.Capacity;
2626

27-
public double ConcurrentLruHitRate => this.concurrentLru.Metrics.HitRatio * 100;
27+
public double ConcurrentLruHitRate => this.concurrentLru.Metrics.Value.HitRatio * 100;
2828

29-
public double ClassicLruHitRate => this.classicLru.Metrics.HitRatio * 100;
29+
public double ClassicLruHitRate => this.classicLru.Metrics.Value.HitRatio * 100;
3030

3131
public void TestKey(long key)
3232
{

BitFaster.Caching.HitRateAnalysis/Wikibench/Analysis.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ public Analysis(int cacheSize)
2424

2525
public int CacheSize => this.concurrentLru.Capacity;
2626

27-
public double ConcurrentLruHitRate => this.concurrentLru.Metrics.HitRatio * 100;
27+
public double ConcurrentLruHitRate => this.concurrentLru.Metrics.Value.HitRatio * 100;
2828

29-
public double ClassicLruHitRate => this.classicLru.Metrics.HitRatio * 100;
29+
public double ClassicLruHitRate => this.classicLru.Metrics.Value.HitRatio * 100;
3030

3131
public void TestUri(Uri uri)
3232
{

BitFaster.Caching.HitRateAnalysis/Zipfian/Runner.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ public static void Run()
122122
CacheSizePercent = a.CacheSizePercent * 100.0,
123123
Samples = a.Samples,
124124
IsScan = false,
125-
HitRatio = classicLru.Metrics.HitRatio * 100.0,
125+
HitRatio = classicLru.Metrics.Value.HitRatio * 100.0,
126126
Duration = clruSw.Elapsed,
127127
});
128128

@@ -134,7 +134,7 @@ public static void Run()
134134
CacheSizePercent = a.CacheSizePercent * 100.0,
135135
Samples = a.Samples,
136136
IsScan = false,
137-
HitRatio = concurrentLru.Metrics.HitRatio * 100.0,
137+
HitRatio = concurrentLru.Metrics.Value.HitRatio * 100.0,
138138
Duration = lruSw.Elapsed,
139139
});
140140

@@ -146,7 +146,7 @@ public static void Run()
146146
CacheSizePercent = a.CacheSizePercent * 100.0,
147147
Samples = a.Samples,
148148
IsScan = true,
149-
HitRatio = classicLruScan.Metrics.HitRatio * 100.0,
149+
HitRatio = classicLruScan.Metrics.Value.HitRatio * 100.0,
150150
Duration = clruSwScan.Elapsed,
151151
});
152152

@@ -158,7 +158,7 @@ public static void Run()
158158
CacheSizePercent = a.CacheSizePercent * 100.0,
159159
Samples = a.Samples,
160160
IsScan = true,
161-
HitRatio = concurrentLruScan.Metrics.HitRatio * 100.0,
161+
HitRatio = concurrentLruScan.Metrics.Value.HitRatio * 100.0,
162162
Duration = lruSwScan.Elapsed,
163163
});
164164
}

BitFaster.Caching.UnitTests/Atomic/AtomicFactoryAsyncCacheTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using BitFaster.Caching.Atomic;
99
using FluentAssertions;
1010
using Xunit;
11+
using Moq;
1112

1213
namespace BitFaster.Caching.UnitTests.Atomic
1314
{
@@ -29,7 +30,7 @@ public void WhenInnerCacheIsNullCtorThrows()
2930
[Fact]
3031
public void WhenCreatedCapacityPropertyWrapsInnerCache()
3132
{
32-
this.cache.Policy.Eviction.Capacity.Should().Be(capacity);
33+
this.cache.Policy.Eviction.Value.Capacity.Should().Be(capacity);
3334
}
3435

3536
[Fact]
@@ -48,14 +49,25 @@ public async Task WhenItemIsAddedThenLookedUpMetricsAreCorrect()
4849
this.cache.AddOrUpdate(1, 1);
4950
await this.cache.GetOrAddAsync(1, k => Task.FromResult(k));
5051

51-
this.cache.Metrics.Misses.Should().Be(0);
52-
this.cache.Metrics.Hits.Should().Be(1);
52+
this.cache.Metrics.Value.Misses.Should().Be(0);
53+
this.cache.Metrics.Value.Hits.Should().Be(1);
54+
}
55+
56+
[Fact]
57+
public void WhenNoInnerEventsNoOuterEvents()
58+
{
59+
var inner = new Mock<ICache<int, AsyncAtomicFactory<int, int>>>();
60+
inner.SetupGet(c => c.Events).Returns(Optional<ICacheEvents<int, AsyncAtomicFactory<int, int>>>.None);
61+
62+
var cache = new AtomicFactoryAsyncCache<int, int>(inner.Object);
63+
64+
cache.Events.HasValue.Should().BeFalse();
5365
}
5466

5567
[Fact]
5668
public void WhenEventHandlerIsRegisteredItIsFired()
5769
{
58-
this.cache.Events.ItemRemoved += OnItemRemoved;
70+
this.cache.Events.Value.ItemRemoved += OnItemRemoved;
5971

6072
this.cache.AddOrUpdate(1, 1);
6173
this.cache.TryRemove(1);
@@ -114,7 +126,7 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue()
114126
this.cache.AddOrUpdate(1, 1);
115127
this.cache.AddOrUpdate(2, 2);
116128

117-
this.cache.Policy.Eviction.Trim(1);
129+
this.cache.Policy.Eviction.Value.Trim(1);
118130

119131
this.cache.TryGet(0, out var value).Should().BeFalse();
120132
}

BitFaster.Caching.UnitTests/Atomic/AtomicFactoryCacheTests.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using BitFaster.Caching.Atomic;
99
using FluentAssertions;
1010
using Xunit;
11+
using Moq;
1112

1213
namespace BitFaster.Caching.UnitTests.Atomic
1314
{
@@ -29,7 +30,7 @@ public void WhenInnerCacheIsNullCtorThrows()
2930
[Fact]
3031
public void WhenCreatedCapacityPropertyWrapsInnerCache()
3132
{
32-
this.cache.Policy.Eviction.Capacity.Should().Be(capacity);
33+
this.cache.Policy.Eviction.Value.Capacity.Should().Be(capacity);
3334
}
3435

3536
[Fact]
@@ -48,21 +49,32 @@ public void WhenItemIsAddedThenLookedUpMetricsAreCorrect()
4849
this.cache.AddOrUpdate(1, 1);
4950
this.cache.GetOrAdd(1, k => k);
5051

51-
this.cache.Metrics.Misses.Should().Be(0);
52-
this.cache.Metrics.Hits.Should().Be(1);
52+
this.cache.Metrics.Value.Misses.Should().Be(0);
53+
this.cache.Metrics.Value.Hits.Should().Be(1);
5354
}
5455

5556
[Fact]
5657
public void WhenEventHandlerIsRegisteredItIsFired()
5758
{
58-
this.cache.Events.ItemRemoved += OnItemRemoved;
59+
this.cache.Events.Value.ItemRemoved += OnItemRemoved;
5960

6061
this.cache.AddOrUpdate(1, 1);
6162
this.cache.TryRemove(1);
6263

6364
this.removedItems.First().Key.Should().Be(1);
6465
}
6566

67+
[Fact]
68+
public void WhenNoInnerEventsNoOuterEvents()
69+
{
70+
var inner = new Mock<ICache<int, AtomicFactory<int, int>>>();
71+
inner.SetupGet(c => c.Events).Returns(Optional<ICacheEvents<int, AtomicFactory<int, int>>>.None);
72+
73+
var cache = new AtomicFactoryCache<int, int>(inner.Object);
74+
75+
cache.Events.HasValue.Should().BeFalse();
76+
}
77+
6678
[Fact]
6779
public void WhenKeyDoesNotExistAddOrUpdateAddsNewItem()
6880
{
@@ -114,7 +126,7 @@ public void WhenCacheContainsValuesTrim1RemovesColdestValue()
114126
this.cache.AddOrUpdate(1, 1);
115127
this.cache.AddOrUpdate(2, 2);
116128

117-
this.cache.Policy.Eviction.Trim(1);
129+
this.cache.Policy.Eviction.Value.Trim(1);
118130

119131
this.cache.TryGet(0, out var value).Should().BeFalse();
120132
}

BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedAsyncCacheTests.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
using BitFaster.Caching.Atomic;
88
using FluentAssertions;
99
using Xunit;
10+
using Moq;
1011

1112
namespace BitFaster.Caching.UnitTests.Atomic
1213
{
1314
public class AtomicFactoryScopedAsyncCacheTests : ScopedAsyncCacheTestBase
1415
{
15-
16-
1716
public AtomicFactoryScopedAsyncCacheTests()
1817
: base(new AtomicFactoryScopedAsyncCache<int, Disposable>(new ConcurrentLru<int, ScopedAsyncAtomicFactory<int, Disposable>>(capacity)))
1918
{
@@ -57,5 +56,16 @@ public async Task GetOrAddAsyncDisposedScopeThrows()
5756

5857
await getOrAdd.Should().ThrowAsync<InvalidOperationException>();
5958
}
59+
60+
[Fact]
61+
public void WhenNoInnerEventsNoOuterEvents()
62+
{
63+
var inner = new Mock<ICache<int, ScopedAsyncAtomicFactory<int, Disposable>>>();
64+
inner.SetupGet(c => c.Events).Returns(Optional<ICacheEvents<int, ScopedAsyncAtomicFactory<int, Disposable>>>.None());
65+
66+
var cache = new AtomicFactoryScopedAsyncCache<int, Disposable>(inner.Object);
67+
68+
cache.Events.HasValue.Should().BeFalse();
69+
}
6070
}
6171
}

BitFaster.Caching.UnitTests/Atomic/AtomicFactoryScopedCacheTests.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using BitFaster.Caching.Atomic;
88
using FluentAssertions;
99
using Xunit;
10+
using Moq;
1011

1112
namespace BitFaster.Caching.UnitTests.Atomic
1213
{
@@ -55,5 +56,16 @@ public void GetOrAddDisposedScopeThrows()
5556

5657
getOrAdd.Should().Throw<InvalidOperationException>();
5758
}
59+
60+
[Fact]
61+
public void WhenNoInnerEventsNoOuterEvents()
62+
{
63+
var inner = new Mock<ICache<int, ScopedAtomicFactory<int, Disposable>>>();
64+
inner.SetupGet(c => c.Events).Returns(Optional<ICacheEvents<int, ScopedAtomicFactory<int, Disposable>>>.None());
65+
66+
var cache = new AtomicFactoryScopedCache<int, Disposable>(inner.Object);
67+
68+
cache.Events.HasValue.Should().BeFalse();
69+
}
5870
}
5971
}

BitFaster.Caching.UnitTests/CacheEventProxyBaseTests.cs

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ public CacheEventProxyBaseTests()
2222
this.eventProxy = new EventProxy<int, int>(this.testCacheEvents);
2323
}
2424

25-
[Fact]
26-
public void EventsAreEnabled()
27-
{
28-
this.testCacheEvents.IsEnabled = true;
29-
30-
this.eventProxy.IsEnabled.Should().BeTrue();
31-
}
32-
3325
[Fact]
3426
public void WhenEventHandlerIsRegisteredItIsFired()
3527
{
@@ -75,8 +67,6 @@ private void OnItemRemovedThrow(object sender, ItemRemovedEventArgs<int, int> e)
7567

7668
private class TestCacheEvents<K, V> : ICacheEvents<K, AtomicFactory<K, V>>
7769
{
78-
public bool IsEnabled { get; set; }
79-
8070
public event EventHandler<ItemRemovedEventArgs<K, AtomicFactory<K, V>>> ItemRemoved;
8171

8272
public void Fire(K key, AtomicFactory<K, V> value, ItemRemovedReason reason)

0 commit comments

Comments
 (0)