Skip to content

Commit 3efdce1

Browse files
authored
enumerate keys (#111)
1 parent 4af8050 commit 3efdce1

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,15 @@ public void WhenItemIsAddedCountIsCorrect()
5656
lru.Count.Should().Be(1);
5757
}
5858

59+
[Fact]
60+
public void WhenItemsAddedKeysContainsTheKeys()
61+
{
62+
lru.Count.Should().Be(0);
63+
lru.GetOrAdd(1, valueFactory.Create);
64+
lru.GetOrAdd(2, valueFactory.Create);
65+
lru.Keys.Should().BeEquivalentTo(new[] { 1, 2 });
66+
}
67+
5968
[Fact]
6069
public void WhenItemExistsTryGetReturnsValueAndTrue()
6170
{

BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ public async Task WhenItemIsAddedCountIsCorrectAsync()
108108
lru.Count.Should().Be(1);
109109
}
110110

111+
[Fact]
112+
public void WhenItemsAddedKeysContainsTheKeys()
113+
{
114+
lru.Count.Should().Be(0);
115+
lru.GetOrAdd(1, valueFactory.Create);
116+
lru.GetOrAdd(2, valueFactory.Create);
117+
lru.Keys.Should().BeEquivalentTo(new[] { 1, 2 });
118+
}
119+
111120
[Fact]
112121
public void WhenItemExistsTryGetReturnsValueAndTrue()
113122
{

BitFaster.Caching/Lru/ClassicLru.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,16 @@ public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer<K> compa
4949

5050
public int Count => this.linkedList.Count;
5151

52+
/// <summary>
53+
/// Gets the ratio of hits to misses, where a value of 1 indicates 100% hits.
54+
/// </summary>
5255
public double HitRatio => (double)requestHitCount / (double)requestTotalCount;
5356

57+
/// <summary>
58+
/// Gets a collection containing the keys in the cache.
59+
/// </summary>
60+
public ICollection<K> Keys => this.dictionary.Keys;
61+
5462
///<inheritdoc/>
5563
public bool TryGet(K key, out V value)
5664
{

BitFaster.Caching/Lru/TemplateConcurrentLru.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,11 @@ public TemplateConcurrentLru(
9696

9797
public int ColdCount => this.coldCount;
9898

99+
/// <summary>
100+
/// Gets a collection containing the keys in the cache.
101+
/// </summary>
102+
public ICollection<K> Keys => this.dictionary.Keys;
103+
99104
///<inheritdoc/>
100105
public bool TryGet(K key, out V value)
101106
{

0 commit comments

Comments
 (0)