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
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BitFaster.Caching.Lru;
using FluentAssertions;
using Xunit;

namespace BitFaster.Caching.UnitTests.Lru
{
public class CapacityPartitionExtensionsTests
{
[Fact]
public void WhenCapacityIsValidDoesNotThrow()
{
var p = new TestCapacityPartition { Cold = 2, Warm = 2, Hot = 2 };

Action validate = () => { p.Validate(); };

validate.Should().NotThrow();
}

[Fact]
public void WhenColdIsZeroThrows()
{
var p = new TestCapacityPartition { Cold = 0, Warm = 2, Hot = 2 };

Action validate = () => { p.Validate(); };

validate.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void WhenWarmIsZeroThrows()
{
var p = new TestCapacityPartition { Cold = 2, Warm = 0, Hot = 2 };

Action validate = () => { p.Validate(); };

validate.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void WhenHotIsZeroThrows()
{
var p = new TestCapacityPartition { Cold = 2, Warm = 2, Hot = 0 };

Action validate = () => { p.Validate(); };

validate.Should().Throw<ArgumentOutOfRangeException>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

namespace BitFaster.Caching.UnitTests.Lru
{
public class LruBuilderTests
public class ConcurrentLruBuilderTests
{
[Fact]
public void TestFastLru()
Expand Down Expand Up @@ -86,5 +86,25 @@ public void TestConcurrencyLevel()

constructor.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void TestIntCapacity()
{
var lru = new ConcurrentLruBuilder<int, Disposable>()
.WithCapacity(3)
.Build();

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

[Fact]
public void TestPartitionCapacity()
{
var lru = new ConcurrentLruBuilder<int, Disposable>()
.WithCapacity(new FavorFrequencyPartition(6))
.Build();

lru.Capacity.Should().Be(6);
}
}
}
9 changes: 9 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,15 @@ public void WhenPartitionIsNullCtorThrows()
constructor.Should().Throw<ArgumentNullException>();
}

[Fact]
public void WhenPartitionIsInvalidThrows()
{
var p = new TestCapacityPartition { Cold = 2, Warm = 0, Hot = 2 };
Action constructor = () => { var x = new ConcurrentLru<int, string>(1, p, EqualityComparer<int>.Default); };

constructor.Should().Throw<ArgumentOutOfRangeException>();
}

[Fact]
public void WhenComparerIsNullCtorThrows()
{
Expand Down
18 changes: 18 additions & 0 deletions BitFaster.Caching.UnitTests/Lru/TestCapacityPartition.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using BitFaster.Caching.Lru;

namespace BitFaster.Caching.UnitTests.Lru
{
public class TestCapacityPartition : ICapacityPartition
{
public int Cold { get; set; }

public int Warm { get; set; }

public int Hot { get; set; }
}
}
12 changes: 12 additions & 0 deletions BitFaster.Caching/Lru/Builder/LruBuilderBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,18 @@ protected LruBuilderBase(LruInfo<K> info)
/// <param name="capacity">The maximum number of values to keep in the cache.</param>
/// <returns>A ConcurrentLruBuilder</returns>
public TBuilder WithCapacity(int capacity)
{
this.info.Capacity = new FavorFrequencyPartition(capacity);
return this as TBuilder;
}

/// <summary>
/// Set the maximum number of values to keep in the cache. If more items than this are added,
/// the cache eviction policy will determine which values to remove.
/// </summary>
/// <param name="capacity">The capacity partition scheme to use.</param>
/// <returns>A ConcurrentLruBuilder</returns>
public TBuilder WithCapacity(ICapacityPartition capacity)
{
this.info.Capacity = capacity;
return this as TBuilder;
Expand Down
2 changes: 1 addition & 1 deletion BitFaster.Caching/Lru/Builder/LruInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ namespace BitFaster.Caching.Lru.Builder
{
public sealed class LruInfo<K>
{
public int Capacity { get; set; } = 128;
public ICapacityPartition Capacity { get; set; } = new FavorFrequencyPartition(128);

public int ConcurrencyLevel { get; set; } = Defaults.ConcurrencyLevel;

Expand Down
30 changes: 30 additions & 0 deletions BitFaster.Caching/Lru/CapacityPartitionExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;

namespace BitFaster.Caching.Lru
{
public static class CapacityPartitionExtensions
{
public static void Validate(this ICapacityPartition capacity)
{
if (capacity.Cold < 1)
{
throw new ArgumentOutOfRangeException(nameof(capacity.Cold));
}

if (capacity.Warm < 1)
{
throw new ArgumentOutOfRangeException(nameof(capacity.Warm));
}

if (capacity.Hot < 1)
{
throw new ArgumentOutOfRangeException(nameof(capacity.Hot));
}
}
}
}
1 change: 1 addition & 0 deletions BitFaster.Caching/Lru/TemplateConcurrentLru.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public TemplateConcurrentLru(
throw new ArgumentNullException(nameof(comparer));
}

capacity.Validate();
this.capacity = capacity;

this.hotQueue = new ConcurrentQueue<I>();
Expand Down