Skip to content

Commit

Permalink
remove metrics/events from core
Browse files Browse the repository at this point in the history
  • Loading branch information
Alex Peck committed Dec 3, 2023
1 parent e361650 commit 9c5d849
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 26 deletions.
8 changes: 5 additions & 3 deletions BitFaster.Caching.UnitTests/Lfu/ConcurrentLfuTests.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;
using BitFaster.Caching.Buffers;
using BitFaster.Caching.Lfu;
using BitFaster.Caching.Scheduler;
using BitFaster.Caching.UnitTests.Lru;
Expand Down Expand Up @@ -572,7 +570,11 @@ public void WhenItemsAddedGenericEnumerateContainsKvps()
cache.GetOrAdd(1, k => k + 1);
cache.GetOrAdd(2, k => k + 1);

cache.Should().BeEquivalentTo(new[] { new KeyValuePair<int, int>(1, 2), new KeyValuePair<int, int>(2, 3) });
var enumerator = cache.GetEnumerator();
enumerator.MoveNext().Should().BeTrue();
enumerator.Current.Should().Be(new KeyValuePair<int, int>(1, 2));
enumerator.MoveNext().Should().BeTrue();
enumerator.Current.Should().Be(new KeyValuePair<int, int>(2, 3));
}

[Fact]
Expand Down
36 changes: 13 additions & 23 deletions BitFaster.Caching/Lfu/ConcurrentLfu.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,13 +85,13 @@ private void DrainBuffers()
public int Count => core.Count;

///<inheritdoc/>
public Optional<ICacheMetrics> Metrics => core.Metrics;
public Optional<ICacheMetrics> Metrics => new(this.core.metrics);

///<inheritdoc/>
public Optional<ICacheEvents<K, V>> Events => core.Events;
public Optional<ICacheEvents<K, V>> Events => Optional<ICacheEvents<K, V>>.None();

///<inheritdoc/>
public CachePolicy Policy => core.Policy;
public CachePolicy Policy => new(new Optional<IBoundedPolicy>(this), Optional<ITimePolicy>.None());

///<inheritdoc/>
public ICollection<K> Keys => core.Keys;
Expand Down Expand Up @@ -127,12 +127,6 @@ public void Clear()
core.Clear();
}

///<inheritdoc/>
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
return core.GetEnumerator();
}

///<inheritdoc/>
public V GetOrAdd(K key, Func<K, V> valueFactory)
{
Expand Down Expand Up @@ -202,10 +196,17 @@ public bool TryUpdate(K key, V value)
return core.TryUpdate(key, value);
}

///<inheritdoc/>
public IEnumerator<KeyValuePair<K, V>> GetEnumerator()
{
return core.GetEnumerator();
}

///<inheritdoc/>
IEnumerator IEnumerable.GetEnumerator()
{
return ((ConcurrentLfuCore<K, V, AccessOrderNode<K, V>, AccessOrderPolicy<K, V>>)core).GetEnumerator();
return core.GetEnumerator();
//return ((ConcurrentLfuCore<K, V, AccessOrderNode<K, V>, AccessOrderPolicy<K, V>>)core).GetEnumerator();
}

#if DEBUG
Expand Down Expand Up @@ -276,7 +277,7 @@ public KeyValuePair<K, V>[] Items
/// Based on the Caffeine library by [email protected] (Ben Manes).
/// https://github.com/ben-manes/caffeine

internal struct ConcurrentLfuCore<K, V, N, P> : ICache<K, V>, IAsyncCache<K, V>, IBoundedPolicy
internal struct ConcurrentLfuCore<K, V, N, P> : IBoundedPolicy
where N : LfuNode<K, V>
where P : struct, INodePolicy<K, V, N>
{
Expand All @@ -289,7 +290,7 @@ internal struct ConcurrentLfuCore<K, V, N, P> : ICache<K, V>, IAsyncCache<K, V>,
internal readonly StripedMpscBuffer<N> readBuffer;
internal readonly MpscBoundedBuffer<N> writeBuffer;

private readonly CacheMetrics metrics = new();
internal readonly CacheMetrics metrics = new();

private readonly CmSketch<K> cmSketch;

Expand Down Expand Up @@ -342,12 +343,6 @@ public ConcurrentLfuCore(int concurrencyLevel, int capacity, IScheduler schedule

public int Capacity => this.capacity.Capacity;

public Optional<ICacheMetrics> Metrics => new(this.metrics);

public Optional<ICacheEvents<K, V>> Events => Optional<ICacheEvents<K, V>>.None();

public CachePolicy Policy => new(new Optional<IBoundedPolicy>(this), Optional<ITimePolicy>.None());

public ICollection<K> Keys => this.dictionary.Keys;

public IScheduler Scheduler => scheduler;
Expand Down Expand Up @@ -662,11 +657,6 @@ private void ScheduleAfterWrite()
}
}

IEnumerator IEnumerable.GetEnumerator()
{
return ((ConcurrentLfuCore<K, V, N, P>)this).GetEnumerator();
}

private void TryScheduleDrain()
{
if (this.drainStatus.VolatileRead() >= DrainStatus.ProcessingToIdle)
Expand Down

0 comments on commit 9c5d849

Please sign in to comment.