diff --git a/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs index a6b8c825..435414ff 100644 --- a/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs @@ -56,6 +56,15 @@ public void WhenItemIsAddedCountIsCorrect() lru.Count.Should().Be(1); } + [Fact] + public void WhenItemsAddedKeysContainsTheKeys() + { + lru.Count.Should().Be(0); + lru.GetOrAdd(1, valueFactory.Create); + lru.GetOrAdd(2, valueFactory.Create); + lru.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + } + [Fact] public void WhenItemExistsTryGetReturnsValueAndTrue() { diff --git a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs index e84008f1..76370822 100644 --- a/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs +++ b/BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs @@ -108,6 +108,15 @@ public async Task WhenItemIsAddedCountIsCorrectAsync() lru.Count.Should().Be(1); } + [Fact] + public void WhenItemsAddedKeysContainsTheKeys() + { + lru.Count.Should().Be(0); + lru.GetOrAdd(1, valueFactory.Create); + lru.GetOrAdd(2, valueFactory.Create); + lru.Keys.Should().BeEquivalentTo(new[] { 1, 2 }); + } + [Fact] public void WhenItemExistsTryGetReturnsValueAndTrue() { diff --git a/BitFaster.Caching/Lru/ClassicLru.cs b/BitFaster.Caching/Lru/ClassicLru.cs index c0f19156..ba14116f 100644 --- a/BitFaster.Caching/Lru/ClassicLru.cs +++ b/BitFaster.Caching/Lru/ClassicLru.cs @@ -49,8 +49,16 @@ public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer compa public int Count => this.linkedList.Count; + /// + /// Gets the ratio of hits to misses, where a value of 1 indicates 100% hits. + /// public double HitRatio => (double)requestHitCount / (double)requestTotalCount; + /// + /// Gets a collection containing the keys in the cache. + /// + public ICollection Keys => this.dictionary.Keys; + /// public bool TryGet(K key, out V value) { diff --git a/BitFaster.Caching/Lru/TemplateConcurrentLru.cs b/BitFaster.Caching/Lru/TemplateConcurrentLru.cs index e5043d4b..691a0e3b 100644 --- a/BitFaster.Caching/Lru/TemplateConcurrentLru.cs +++ b/BitFaster.Caching/Lru/TemplateConcurrentLru.cs @@ -96,6 +96,11 @@ public TemplateConcurrentLru( public int ColdCount => this.coldCount; + /// + /// Gets a collection containing the keys in the cache. + /// + public ICollection Keys => this.dictionary.Keys; + /// public bool TryGet(K key, out V value) {