Skip to content

Commit 3defd73

Browse files
authored
basic docs (#45)
1 parent df3e09f commit 3defd73

File tree

14 files changed

+183
-10
lines changed

14 files changed

+183
-10
lines changed

BitFaster.Caching.UnitTests/Lru/ConcurrentTLruTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public ConcurrentTLruTests()
2424
[Fact]
2525
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue()
2626
{
27-
var x = new ConcurrentTLru<int, int>(3);
27+
var x = new ConcurrentTLru<int, int>(3, TimeSpan.FromSeconds(1));
2828

2929
x.GetOrAdd(1, k => k).Should().Be(1);
3030
}

BitFaster.Caching.UnitTests/Lru/FastConcurrentTLruTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public void ConstructAddAndRetrieveWithCustomComparerReturnsValue()
2323
[Fact]
2424
public void ConstructAddAndRetrieveWithDefaultCtorReturnsValue()
2525
{
26-
var x = new FastConcurrentTLru<int, int>(3);
26+
var x = new FastConcurrentTLru<int, int>(3, TimeSpan.FromSeconds(1));
2727

2828
x.GetOrAdd(1, k => k).Should().Be(1);
2929
}

BitFaster.Caching/ICache.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,45 @@
66

77
namespace BitFaster.Caching
88
{
9+
/// <summary>
10+
/// Represents a generic cache of key/value pairs.
11+
/// </summary>
12+
/// <typeparam name="K">The type of keys in the cache.</typeparam>
13+
/// <typeparam name="V">The type of values in the cache.</typeparam>
914
public interface ICache<K, V>
1015
{
16+
/// <summary>
17+
/// Attempts to get the value associated with the specified key from the cache.
18+
/// </summary>
19+
/// <param name="key">The key of the value to get.</param>
20+
/// <param name="value">When this method returns, contains the object from the cache that has the specified key, or the default value of the type if the operation failed.</param>
21+
/// <returns>true if the key was found in the cache; otherwise, false.</returns>
1122
bool TryGet(K key, out V value);
1223

24+
/// <summary>
25+
/// Adds a key/value pair to the cache if the key does not already exist. Returns the new value, or the
26+
/// existing value if the key already exists.
27+
/// </summary>
28+
/// <param name="key">The key of the element to add.</param>
29+
/// <param name="valueFactory">The factory function used to generate a value for the key.</param>
30+
/// <returns>The value for the key. This will be either the existing value for the key if the key is already
31+
/// in the cache, or the new value if the key was not in the dictionary.</returns>
1332
V GetOrAdd(K key, Func<K, V> valueFactory);
1433

34+
/// <summary>
35+
/// Adds a key/value pair to the cache if the key does not already exist. Returns the new value, or the
36+
/// existing value if the key already exists.
37+
/// </summary>
38+
/// <param name="key">The key of the element to add.</param>
39+
/// <param name="valueFactory">The factory function used to asynchronously generate a value for the key.</param>
40+
/// <returns>A task that represents the asynchronous GetOrAdd operation.</returns>
1541
Task<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory);
1642

43+
/// <summary>
44+
/// Attempts to remove and return the value that has the specified key from the cache.
45+
/// </summary>
46+
/// <param name="key">The key of the element to remove.</param>
47+
/// <returns>true if the object was removed successfully; otherwise, false.</returns>
1748
bool TryRemove(K key);
1849
}
1950
}

BitFaster.Caching/Lifetime.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,35 @@
44

55
namespace BitFaster.Caching
66
{
7+
/// <summary>
8+
/// Represents the lifetime of a value. The value is alive and valid for use until the
9+
/// lifetime is disposed.
10+
/// </summary>
11+
/// <typeparam name="T">The type of value</typeparam>
712
public class Lifetime<T> : IDisposable
813
{
914
private readonly Action onDisposeAction;
1015
private bool isDisposed;
1116

17+
/// <summary>
18+
/// Initializes a new instance of the Lifetime class.
19+
/// </summary>
20+
/// <param name="value">The value to keep alive.</param>
21+
/// <param name="onDisposeAction">The action to perform when the lifetime is terminated.</param>
1222
public Lifetime(T value, Action onDisposeAction)
1323
{
1424
this.Value = value;
1525
this.onDisposeAction = onDisposeAction;
1626
}
1727

28+
/// <summary>
29+
/// Gets the value.
30+
/// </summary>
1831
public T Value { get; }
1932

33+
/// <summary>
34+
/// Terminates the lifetime and performs any cleanup required to release the value.
35+
/// </summary>
2036
public void Dispose()
2137
{
2238
if (!this.isDisposed)

BitFaster.Caching/Lru/ClassicLru.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ public ClassicLru(int concurrencyLevel, int capacity, IEqualityComparer<K> compa
5151

5252
public double HitRatio => (double)requestHitCount / (double)requestTotalCount;
5353

54+
///<inheritdoc/>
5455
public bool TryGet(K key, out V value)
5556
{
5657
Interlocked.Increment(ref requestTotalCount);
@@ -68,6 +69,7 @@ public bool TryGet(K key, out V value)
6869
return false;
6970
}
7071

72+
///<inheritdoc/>
7173
public V GetOrAdd(K key, Func<K, V> valueFactory)
7274
{
7375
if (this.TryGet(key, out var value))
@@ -114,6 +116,7 @@ public V GetOrAdd(K key, Func<K, V> valueFactory)
114116
return this.GetOrAdd(key, valueFactory);
115117
}
116118

119+
///<inheritdoc/>
117120
public async Task<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory)
118121
{
119122
if (this.TryGet(key, out var value))
@@ -160,6 +163,7 @@ public async Task<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory)
160163
return await this.GetOrAddAsync(key, valueFactory);
161164
}
162165

166+
///<inheritdoc/>
163167
public bool TryRemove(K key)
164168
{
165169
if (dictionary.TryRemove(key, out var node))

BitFaster.Caching/Lru/ConcurrentLru.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,34 @@
66

77
namespace BitFaster.Caching.Lru
88
{
9+
///<inheritdoc/>
910
public sealed class ConcurrentLru<K, V> : TemplateConcurrentLru<K, V, LruItem<K, V>, LruPolicy<K, V>, HitCounter>
1011
{
12+
/// <summary>
13+
/// Initializes a new instance of the ConcurrentLru class with the specified capacity that has the default
14+
/// concurrency level, and uses the default comparer for the key type.
15+
/// </summary>
16+
/// <param name="capacity">The maximum number of elements that the ConcurrentLru can contain.</param>
1117
public ConcurrentLru(int capacity)
1218
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new LruPolicy<K, V>(), new HitCounter())
1319
{
1420
}
1521

22+
/// <summary>
23+
/// Initializes a new instance of the ConcurrentLru class that has the specified concurrency level, has the
24+
/// specified initial capacity, and uses the specified IEqualityComparer<T>.
25+
/// </summary>
26+
/// <param name="concurrencyLevel">The estimated number of threads that will update the ConcurrentLru concurrently.</param>
27+
/// <param name="capacity">The maximum number of elements that the ConcurrentLru can contain.</param>
28+
/// <param name="comparer">The IEqualityComparer<T> implementation to use when comparing keys.</param>
1629
public ConcurrentLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer)
1730
: base(concurrencyLevel, capacity, comparer, new LruPolicy<K, V>(), new HitCounter())
1831
{
1932
}
2033

34+
/// <summary>
35+
/// Gets the ratio of hits to misses, where a value of 1 indicates 100% hits.
36+
/// </summary>
2137
public double HitRatio => this.hitCounter.HitRatio;
2238
}
2339
}

BitFaster.Caching/Lru/ConcurrentTLru.cs

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,36 @@
66

77
namespace BitFaster.Caching.Lru
88
{
9+
///<inheritdoc/>
910
public sealed class ConcurrentTLru<K, V> : TemplateConcurrentLru<K, V, LongTickCountLruItem<K, V>, TLruLongTicksPolicy<K, V>, HitCounter>
1011
{
11-
public ConcurrentTLru(int capacity)
12-
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new TLruLongTicksPolicy<K, V>(), new HitCounter())
12+
/// <summary>
13+
/// Initializes a new instance of the ConcurrentTLru class with the specified capacity and time to live that has the default
14+
/// concurrency level, and uses the default comparer for the key type.
15+
/// </summary>
16+
/// <param name="capacity">The maximum number of elements that the ConcurrentTLru can contain.</param>
17+
/// <param name="timeToLive">The time to live for cached values.</param>
18+
public ConcurrentTLru(int capacity, TimeSpan timeToLive)
19+
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new TLruLongTicksPolicy<K, V>(timeToLive), new HitCounter())
1320
{
1421
}
1522

23+
/// <summary>
24+
/// Initializes a new instance of the ConcurrentTLru class that has the specified concurrency level, has the
25+
/// specified initial capacity, uses the specified IEqualityComparer<T>, and has the specified time to live.
26+
/// </summary>
27+
/// <param name="concurrencyLevel">The estimated number of threads that will update the ConcurrentTLru concurrently.</param>
28+
/// <param name="capacity">The maximum number of elements that the ConcurrentTLru can contain.</param>
29+
/// <param name="comparer">The IEqualityComparer<T> implementation to use when comparing keys.</param>
30+
/// <param name="timeToLive">The time to live for cached values.</param>
1631
public ConcurrentTLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer, TimeSpan timeToLive)
1732
: base(concurrencyLevel, capacity, comparer, new TLruLongTicksPolicy<K, V>(timeToLive), new HitCounter())
1833
{
1934
}
2035

36+
/// <summary>
37+
/// Gets the ratio of hits to misses, where a value of 1 indicates 100% hits.
38+
/// </summary>
2139
public double HitRatio => this.hitCounter.HitRatio;
2240
}
2341
}

BitFaster.Caching/Lru/FastConcurrentLru.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,26 @@
44

55
namespace BitFaster.Caching.Lru
66
{
7+
///<inheritdoc/>
78
public sealed class FastConcurrentLru<K, V> : TemplateConcurrentLru<K, V, LruItem<K, V>, LruPolicy<K, V>, NullHitCounter>
89
{
10+
/// <summary>
11+
/// Initializes a new instance of the FastConcurrentLru class with the specified capacity that has the default
12+
/// concurrency level, and uses the default comparer for the key type.
13+
/// </summary>
14+
/// <param name="capacity">The maximum number of elements that the FastConcurrentLru can contain.</param>
915
public FastConcurrentLru(int capacity)
1016
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new LruPolicy<K, V>(), new NullHitCounter())
1117
{
1218
}
1319

20+
/// <summary>
21+
/// Initializes a new instance of the FastConcurrentLru class that has the specified concurrency level, has the
22+
/// specified initial capacity, and uses the specified IEqualityComparer<T>.
23+
/// </summary>
24+
/// <param name="concurrencyLevel">The estimated number of threads that will update the FastConcurrentLru concurrently.</param>
25+
/// <param name="capacity">The maximum number of elements that the FastConcurrentLru can contain.</param>
26+
/// <param name="comparer">The IEqualityComparer<T> implementation to use when comparing keys.</param>
1427
public FastConcurrentLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer)
1528
: base(concurrencyLevel, capacity, comparer, new LruPolicy<K, V>(), new NullHitCounter())
1629
{

BitFaster.Caching/Lru/FastConcurrentTLru.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,28 @@
44

55
namespace BitFaster.Caching.Lru
66
{
7+
///<inheritdoc/>
78
public sealed class FastConcurrentTLru<K, V> : TemplateConcurrentLru<K, V, LongTickCountLruItem<K, V>, TLruLongTicksPolicy<K, V>, NullHitCounter>
89
{
9-
public FastConcurrentTLru(int capacity)
10-
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new TLruLongTicksPolicy<K, V>(), new NullHitCounter())
10+
/// <summary>
11+
/// Initializes a new instance of the FastConcurrentTLru class with the specified capacity and time to live that has the default
12+
/// concurrency level, and uses the default comparer for the key type.
13+
/// </summary>
14+
/// <param name="capacity">The maximum number of elements that the FastConcurrentTLru can contain.</param>
15+
/// <param name="timeToLive">The time to live for cached values.</param>
16+
public FastConcurrentTLru(int capacity, TimeSpan timeToLive)
17+
: base(Defaults.ConcurrencyLevel, capacity, EqualityComparer<K>.Default, new TLruLongTicksPolicy<K, V>(timeToLive), new NullHitCounter())
1118
{
1219
}
1320

21+
/// <summary>
22+
/// Initializes a new instance of the FastConcurrentTLru class that has the specified concurrency level, has the
23+
/// specified initial capacity, uses the specified IEqualityComparer<T>, and has the specified time to live.
24+
/// </summary>
25+
/// <param name="concurrencyLevel">The estimated number of threads that will update the FastConcurrentTLru concurrently.</param>
26+
/// <param name="capacity">The maximum number of elements that the FastConcurrentTLru can contain.</param>
27+
/// <param name="comparer">The IEqualityComparer<T> implementation to use when comparing keys.</param>
28+
/// <param name="timeToLive">The time to live for cached values.</param>
1429
public FastConcurrentTLru(int concurrencyLevel, int capacity, IEqualityComparer<K> comparer, TimeSpan timeToLive)
1530
: base(concurrencyLevel, capacity, comparer, new TLruLongTicksPolicy<K, V>(timeToLive), new NullHitCounter())
1631
{

BitFaster.Caching/Lru/TemplateConcurrentLru.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public TemplateConcurrentLru(
9292

9393
public int ColdCount => this.coldCount;
9494

95+
///<inheritdoc/>
9596
public bool TryGet(K key, out V value)
9697
{
9798
I item;
@@ -115,6 +116,7 @@ public bool TryGet(K key, out V value)
115116
return false;
116117
}
117118

119+
///<inheritdoc/>
118120
public V GetOrAdd(K key, Func<K, V> valueFactory)
119121
{
120122
if (this.TryGet(key, out var value))
@@ -137,6 +139,7 @@ public V GetOrAdd(K key, Func<K, V> valueFactory)
137139
return this.GetOrAdd(key, valueFactory);
138140
}
139141

142+
///<inheritdoc/>
140143
public async Task<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory)
141144
{
142145
if (this.TryGet(key, out var value))
@@ -159,6 +162,7 @@ public async Task<V> GetOrAddAsync(K key, Func<K, Task<V>> valueFactory)
159162
return await this.GetOrAddAsync(key, valueFactory).ConfigureAwait(false);
160163
}
161164

165+
///<inheritdoc/>
162166
public bool TryRemove(K key)
163167
{
164168
// Possible race condition:

0 commit comments

Comments
 (0)