Skip to content

Commit 237b0dd

Browse files
authored
more ctor (#34)
1 parent a78d4cb commit 237b0dd

12 files changed

+81
-4
lines changed

BitFaster.Caching.UnitTests/Lru/ClassicLruTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,14 @@ public void WhenComparerIsNullCtorThrows()
4040
constructor.Should().Throw<ArgumentNullException>();
4141
}
4242

43+
[Fact]
44+
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue()
45+
{
46+
var x = new ConcurrentTLru<int, int>(3);
47+
48+
x.GetOrAdd(1, k => k).Should().Be(1);
49+
}
50+
4351
[Fact]
4452
public void WhenItemIsAddedCountIsCorrect()
4553
{

BitFaster.Caching.UnitTests/Lru/ConcurrentLruTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ public void WhenComparerIsNullCtorThrows()
4949
constructor.Should().Throw<ArgumentNullException>();
5050
}
5151

52+
[Fact]
53+
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue()
54+
{
55+
var x = new ConcurrentLru<int, int>(3);
56+
57+
x.GetOrAdd(1, k => k).Should().Be(1);
58+
}
59+
5260
[Fact]
5361
public void WhenItemIsAddedCountIsCorrect()
5462
{

BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,14 @@ public ConcurrentTLruTests()
2121
lru = new ConcurrentTLru<int, string>(1, capacity, EqualityComparer<int>.Default, timeToLive);
2222
}
2323

24+
[Fact]
25+
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue()
26+
{
27+
var x = new ConcurrentTLru<int, int>(3);
28+
29+
x.GetOrAdd(1, k => k).Should().Be(1);
30+
}
31+
2432
[Fact]
2533
public async Task WhenItemIsExpiredItIsRemoved()
2634
{

BitFaster.Caching.UnitTests/Lru/FastConcurrentLruTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@ public void ConstructAddAndRetrieveWithCustomComparerReturnsValue()
1919
lru.TryGet("FOO", out var value).Should().BeTrue();
2020
value.Should().Be(1);
2121
}
22+
23+
[Fact]
24+
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue()
25+
{
26+
var x = new FastConcurrentLru<int, int>(3);
27+
28+
x.GetOrAdd(1, k => k).Should().Be(1);
29+
}
2230
}
2331
}

BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,5 +19,13 @@ public void ConstructAddAndRetrieveWithCustomComparerReturnsValue()
1919
lru.TryGet("FOO", out var value).Should().BeTrue();
2020
value.Should().Be(1);
2121
}
22+
23+
[Fact]
24+
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue()
25+
{
26+
var x = new FastConcurrentTLru<int, int>(3);
27+
28+
x.GetOrAdd(1, k => k).Should().Be(1);
29+
}
2230
}
2331
}

BitFaster.Caching/Lru/ClassicLru.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ public sealed class ClassicLru<K, V> : ICache<K, V>
2626
private long requestHitCount;
2727
private long requestTotalCount;
2828

29+
public ClassicLru(int capacity)
30+
: this(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default)
31+
{
32+
}
33+
2934
public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer)
3035
{
3136
if (capacity < 3)

BitFaster.Caching/Lru/ConcurrentLru.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace BitFaster.Caching.Lru
88
{
99
public sealed class ConcurrentLru<K, V> : TemplateConcurrentLru<K, V, LruItem<K, V>, LruPolicy<K, V>, HitCounter>
1010
{
11+
public ConcurrentLru(int capacity)
12+
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new LruPolicy<K, V>(), new HitCounter())
13+
{
14+
}
15+
1116
public ConcurrentLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer)
1217
: base(concurrencyLevel, capacity, comparer, new LruPolicy<K, V>(), new HitCounter())
1318
{

BitFaster.Caching/Lru/ConcurrentTLru.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ namespace BitFaster.Caching.Lru
88
{
99
public sealed class ConcurrentTLru<K, V> : TemplateConcurrentLru<K, V, TimeStampedLruItem<K, V>, TLruDateTimePolicy<K, V>, HitCounter>
1010
{
11+
public ConcurrentTLru(int capacity)
12+
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new TLruDateTimePolicy<K, V>(), new HitCounter())
13+
{
14+
}
15+
1116
public ConcurrentTLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer, TimeSpan timeToLive)
1217
: base(concurrencyLevel, capacity, comparer, new TLruDateTimePolicy<K, V>(timeToLive), new HitCounter())
1318
{

BitFaster.Caching/Lru/Defaults.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace BitFaster.Caching.Lru
6+
{
7+
internal static class Defaults
8+
{
9+
public static int ConcurrencyLevel
10+
{
11+
get { return Environment.ProcessorCount; }
12+
}
13+
}
14+
}

BitFaster.Caching/Lru/FastConcurrentLru.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ namespace BitFaster.Caching.Lru
66
{
77
public sealed class FastConcurrentLru<K, V> : TemplateConcurrentLru<K, V, LruItem<K, V>, LruPolicy<K, V>, NullHitCounter>
88
{
9+
public FastConcurrentLru(int capacity)
10+
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new LruPolicy<K, V>(), new NullHitCounter())
11+
{
12+
}
13+
914
public FastConcurrentLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer)
1015
: base(concurrencyLevel, capacity, comparer, new LruPolicy<K, V>(), new NullHitCounter())
1116
{

0 commit comments

Comments
 (0)