Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion BitFaster.Caching.UnitTests/Lru/FastConcurrentLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
using BitFaster.Caching.Lru;
using System;
using System.Collections.Generic;
using System.Text;
using Xunit;

namespace BitFaster.Caching.UnitTests.Lru
Expand Down Expand Up @@ -35,5 +34,21 @@ public void ConstructPartitionCtorReturnsCapacity()

x.Capacity.Should().Be(3);
}

[Fact]
public void MetricsHasValueIsFalse()
{
var x = new FastConcurrentLru<int, int>(3);

x.Metrics.HasValue.Should().BeFalse();
}

[Fact]
public void EventsHasValueIsFalse()
{
var x = new FastConcurrentLru<int, int>(3);

x.Events.HasValue.Should().BeFalse();
}
}
}
16 changes: 16 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,21 @@ public async Task WhenItemsAreExpiredExpireRemovesExpiredItems()

lru.Count.Should().Be(0);
}

[Fact]
public void MetricsHasValueIsFalse()
{
var x = new FastConcurrentTLru<int, int>(3, TimeSpan.FromSeconds(1));

x.Metrics.HasValue.Should().BeFalse();
}

[Fact]
public void EventsHasValueIsFalse()
{
var x = new FastConcurrentTLru<int, int>(3, TimeSpan.FromSeconds(1));

x.Events.HasValue.Should().BeFalse();
}
}
}
24 changes: 22 additions & 2 deletions BitFaster.Caching/Lru/ConcurrentLruCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,10 +104,10 @@ public ConcurrentLruCore(
public int Capacity => this.capacity.Hot + this.capacity.Warm + this.capacity.Cold;

///<inheritdoc/>
public Optional<ICacheMetrics> Metrics => new(new Proxy(this));
public Optional<ICacheMetrics> Metrics => CreateMetrics(this);

///<inheritdoc/>
public Optional<ICacheEvents<K, V>> Events => new(new Proxy(this));
public Optional<ICacheEvents<K, V>> Events => CreateEvents(this);

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

private static Optional<ICacheMetrics> CreateMetrics(ConcurrentLruCore<K, V, I, P, T> lru)
{
if (typeof(T) == typeof(NoTelemetryPolicy<K, V>))
{
return Optional<ICacheMetrics>.None();
}

return new(new Proxy(lru));
}

private static Optional<ICacheEvents<K, V>> CreateEvents(ConcurrentLruCore<K, V, I, P, T> lru)
{
if (typeof(T) == typeof(NoTelemetryPolicy<K, V>))
{
return Optional<ICacheEvents<K, V>>.None();
}

return new(new Proxy(lru));
}

// To get JIT optimizations, policies must be structs.
// If the structs are returned directly via properties, they will be copied. Since
// telemetryPolicy is a mutable struct, copy is bad. One workaround is to store the
Expand Down