Skip to content

Commit 4782d2e

Browse files
authored
LRU metrics and events should be optional (#465)
1 parent 532db75 commit 4782d2e

File tree

3 files changed

+54
-3
lines changed

3 files changed

+54
-3
lines changed

BitFaster.Caching.UnitTests/Lru/FastConcurrentLruTests.cs

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using BitFaster.Caching.Lru;
33
using System;
44
using System.Collections.Generic;
5-
using System.Text;
65
using Xunit;
76

87
namespace BitFaster.Caching.UnitTests.Lru
@@ -35,5 +34,21 @@ public void ConstructPartitionCtorReturnsCapacity()
3534

3635
x.Capacity.Should().Be(3);
3736
}
37+
38+
[Fact]
39+
public void MetricsHasValueIsFalse()
40+
{
41+
var x = new FastConcurrentLru<int, int>(3);
42+
43+
x.Metrics.HasValue.Should().BeFalse();
44+
}
45+
46+
[Fact]
47+
public void EventsHasValueIsFalse()
48+
{
49+
var x = new FastConcurrentLru<int, int>(3);
50+
51+
x.Events.HasValue.Should().BeFalse();
52+
}
3853
}
3954
}

BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,21 @@ public async Task WhenItemsAreExpiredExpireRemovesExpiredItems()
5353

5454
lru.Count.Should().Be(0);
5555
}
56+
57+
[Fact]
58+
public void MetricsHasValueIsFalse()
59+
{
60+
var x = new FastConcurrentTLru<int, int>(3, TimeSpan.FromSeconds(1));
61+
62+
x.Metrics.HasValue.Should().BeFalse();
63+
}
64+
65+
[Fact]
66+
public void EventsHasValueIsFalse()
67+
{
68+
var x = new FastConcurrentTLru<int, int>(3, TimeSpan.FromSeconds(1));
69+
70+
x.Events.HasValue.Should().BeFalse();
71+
}
5672
}
5773
}

BitFaster.Caching/Lru/ConcurrentLruCore.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,10 @@ public ConcurrentLruCore(
104104
public int Capacity => this.capacity.Hot + this.capacity.Warm + this.capacity.Cold;
105105

106106
///<inheritdoc/>
107-
public Optional<ICacheMetrics> Metrics => new(new Proxy(this));
107+
public Optional<ICacheMetrics> Metrics => CreateMetrics(this);
108108

109109
///<inheritdoc/>
110-
public Optional<ICacheEvents<K, V>> Events => new(new Proxy(this));
110+
public Optional<ICacheEvents<K, V>> Events => CreateEvents(this);
111111

112112
///<inheritdoc/>
113113
public CachePolicy Policy => CreatePolicy(this);
@@ -782,6 +782,26 @@ private static CachePolicy CreatePolicy(ConcurrentLruCore<K, V, I, P, T> lru)
782782
return new CachePolicy(new Optional<IBoundedPolicy>(p), lru.itemPolicy.CanDiscard() ? new Optional<ITimePolicy>(p) : Optional<ITimePolicy>.None());
783783
}
784784

785+
private static Optional<ICacheMetrics> CreateMetrics(ConcurrentLruCore<K, V, I, P, T> lru)
786+
{
787+
if (typeof(T) == typeof(NoTelemetryPolicy<K, V>))
788+
{
789+
return Optional<ICacheMetrics>.None();
790+
}
791+
792+
return new(new Proxy(lru));
793+
}
794+
795+
private static Optional<ICacheEvents<K, V>> CreateEvents(ConcurrentLruCore<K, V, I, P, T> lru)
796+
{
797+
if (typeof(T) == typeof(NoTelemetryPolicy<K, V>))
798+
{
799+
return Optional<ICacheEvents<K, V>>.None();
800+
}
801+
802+
return new(new Proxy(lru));
803+
}
804+
785805
// To get JIT optimizations, policies must be structs.
786806
// If the structs are returned directly via properties, they will be copied. Since
787807
// telemetryPolicy is a mutable struct, copy is bad. One workaround is to store the

0 commit comments

Comments
 (0)