Skip to content

Commit 0bb038f

Browse files
authored
docs (#241)
* docs * docs * docs
1 parent a140736 commit 0bb038f

21 files changed

+248
-42
lines changed

BitFaster.Caching/Atomic/AsyncAtomicFactory.cs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Text;
63
using System.Threading;
74
using System.Threading.Tasks;
85

96
namespace BitFaster.Caching.Atomic
107
{
8+
/// <summary>
9+
/// A class that provides simple, lightweight exactly once initialization for values
10+
/// stored in a cache.
11+
/// </summary>
12+
/// <typeparam name="K">The type of the key.</typeparam>
13+
/// <typeparam name="V">The type of the value.</typeparam>
1114
[DebuggerDisplay("IsValueCreated={IsValueCreated}, Value={ValueIfCreated}")]
1215
public sealed class AsyncAtomicFactory<K, V>
1316
{
@@ -16,16 +19,30 @@ public sealed class AsyncAtomicFactory<K, V>
1619
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
1720
private V value;
1821

22+
/// <summary>
23+
/// Initializes a new instance of the <see cref="AsyncAtomicFactory{K, V}"/> class.
24+
/// </summary>
1925
public AsyncAtomicFactory()
2026
{
2127
initializer = new Initializer();
2228
}
2329

30+
/// <summary>
31+
/// Initializes a new instance of the <see cref="AsyncAtomicFactory{K, V}"/> class with the
32+
/// specified value.
33+
/// </summary>
34+
/// <param name="value">The value.</param>
2435
public AsyncAtomicFactory(V value)
2536
{
2637
this.value = value;
2738
}
2839

40+
/// <summary>
41+
/// Gets the value. If <see cref="IsValueCreated"/> is false, calling <see cref="GetValueAsync"/> will force initialization via the <paramref name="valueFactory"/> parameter.
42+
/// </summary>
43+
/// <param name="key">The key associated with the value.</param>
44+
/// <param name="valueFactory">The value factory to use to create the value when it is not initialized.</param>
45+
/// <returns>The value.</returns>
2946
public async ValueTask<V> GetValueAsync(K key, Func<K, Task<V>> valueFactory)
3047
{
3148
if (initializer == null)
@@ -36,8 +53,14 @@ public async ValueTask<V> GetValueAsync(K key, Func<K, Task<V>> valueFactory)
3653
return await CreateValueAsync(key, valueFactory).ConfigureAwait(false);
3754
}
3855

56+
/// <summary>
57+
/// Gets a value indicating whether the value has been initialized.
58+
/// </summary>
3959
public bool IsValueCreated => initializer == null;
4060

61+
/// <summary>
62+
/// Gets the value if it has been initialized, else default.
63+
/// </summary>
4164
public V ValueIfCreated
4265
{
4366
get
@@ -66,7 +89,7 @@ private async ValueTask<V> CreateValueAsync(K key, Func<K, Task<V>> valueFactory
6689

6790
private class Initializer
6891
{
69-
private object syncLock = new object();
92+
private readonly object syncLock = new object();
7093
private bool isInitialized;
7194
private Task<V> valueTask;
7295

BitFaster.Caching/Atomic/AtomicFactory.cs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,15 @@
11
using System;
2-
using System.Collections.Generic;
32
using System.Diagnostics;
4-
using System.Linq;
5-
using System.Text;
63
using System.Threading;
7-
using System.Threading.Tasks;
84

95
namespace BitFaster.Caching.Atomic
106
{
7+
/// <summary>
8+
/// A class that provides simple, lightweight exactly once initialization for values
9+
/// stored in a cache.
10+
/// </summary>
11+
/// <typeparam name="K">The type of the key.</typeparam>
12+
/// <typeparam name="V">The type of the value.</typeparam>
1113
[DebuggerDisplay("IsValueCreated={IsValueCreated}, Value={ValueIfCreated}")]
1214
public sealed class AtomicFactory<K, V>
1315
{
@@ -16,16 +18,30 @@ public sealed class AtomicFactory<K, V>
1618
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
1719
private V value;
1820

21+
/// <summary>
22+
/// Initializes a new instance of the <see cref="AtomicFactory{K, V}"/> class.
23+
/// </summary>
1924
public AtomicFactory()
2025
{
2126
initializer = new Initializer();
2227
}
2328

29+
/// <summary>
30+
/// Initializes a new instance of the <see cref="AtomicFactory{K, V}"/> class with the
31+
/// specified value.
32+
/// </summary>
33+
/// <param name="value">The value.</param>
2434
public AtomicFactory(V value)
2535
{
2636
this.value = value;
2737
}
2838

39+
/// <summary>
40+
/// Gets the value. If <see cref="IsValueCreated"/> is false, calling <see cref="GetValue"/> will force initialization via the <paramref name="valueFactory"/> parameter.
41+
/// </summary>
42+
/// <param name="key">The key associated with the value.</param>
43+
/// <param name="valueFactory">The value factory to use to create the value when it is not initialized.</param>
44+
/// <returns>The value.</returns>
2945
public V GetValue(K key, Func<K, V> valueFactory)
3046
{
3147
if (initializer == null)
@@ -36,8 +52,14 @@ public V GetValue(K key, Func<K, V> valueFactory)
3652
return CreateValue(key, valueFactory);
3753
}
3854

55+
/// <summary>
56+
/// Gets a value indicating whether the value has been initialized.
57+
/// </summary>
3958
public bool IsValueCreated => initializer == null;
4059

60+
/// <summary>
61+
/// Gets the value if it has been initialized, else default.
62+
/// </summary>
4163
public V ValueIfCreated
4264
{
4365
get
@@ -66,7 +88,7 @@ private V CreateValue(K key, Func<K, V> valueFactory)
6688

6789
private class Initializer
6890
{
69-
private object syncLock = new object();
91+
private readonly object syncLock = new object();
7092
private bool isInitialized;
7193
private V value;
7294

BitFaster.Caching/Atomic/AtomicFactoryAsyncCache.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace BitFaster.Caching.Atomic
88
{
9+
/// <summary>
10+
/// A cache decorator for working with <see cref="AsyncAtomicFactory{K, V}"/> wrapped values, giving exactly once initialization.
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
[DebuggerDisplay("Count = {Count}")]
1015
public sealed class AtomicFactoryAsyncCache<K, V> : IAsyncCache<K, V>
1116
{

BitFaster.Caching/Atomic/AtomicFactoryCache.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,21 @@
55

66
namespace BitFaster.Caching.Atomic
77
{
8+
/// <summary>
9+
/// A cache decorator for working with <see cref="AtomicFactory{K, V}"/> wrapped values, giving exactly once initialization.
10+
/// </summary>
11+
/// <typeparam name="K">The type of keys in the cache.</typeparam>
12+
/// <typeparam name="V">The type of values in the cache.</typeparam>
813
[DebuggerDisplay("Count = {Count}")]
914
public sealed class AtomicFactoryCache<K, V> : ICache<K, V>
1015
{
1116
private readonly ICache<K, AtomicFactory<K, V>> cache;
1217
private readonly Optional<ICacheEvents<K, V>> events;
1318

19+
/// <summary>
20+
/// Initializes a new instance of the ScopedCache class with the specified inner cache.
21+
/// </summary>
22+
/// <param name="cache">The decorated cache.</param>
1423
public AtomicFactoryCache(ICache<K, AtomicFactory<K, V>> cache)
1524
{
1625
if (cache == null)

BitFaster.Caching/Atomic/AtomicFactoryScopedAsyncCache.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77

88
namespace BitFaster.Caching.Atomic
99
{
10+
/// <summary>
11+
/// A cache decorator for working with <see cref="ScopedAsyncAtomicFactory{K, V}"/> wrapped values, giving exactly once initialization.
12+
/// </summary>
13+
/// <typeparam name="K">The type of keys in the cache.</typeparam>
14+
/// <typeparam name="V">The type of values in the cache.</typeparam>
1015
[DebuggerDisplay("Count = {Count}")]
1116
public sealed class AtomicFactoryScopedAsyncCache<K, V> : IScopedAsyncCache<K, V> where V : IDisposable
1217
{

BitFaster.Caching/Atomic/AtomicFactoryScopedCache.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
namespace BitFaster.Caching.Atomic
88
{
9+
/// <summary>
10+
/// A cache decorator for working with <see cref="ScopedAtomicFactory{K, V}"/> wrapped values, giving exactly once initialization.
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
[DebuggerDisplay("Count = {Count}")]
1015
public sealed class AtomicFactoryScopedCache<K, V> : IScopedCache<K, V> where V : IDisposable
1116
{

BitFaster.Caching/Atomic/ScopedAsyncAtomicFactory.cs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,22 +5,39 @@
55

66
namespace BitFaster.Caching.Atomic
77
{
8+
/// <summary>
9+
/// A class that provides simple, lightweight exactly once initialization for scoped values
10+
/// stored in a cache.
11+
/// </summary>
12+
/// <typeparam name="K">The type of the key.</typeparam>
13+
/// <typeparam name="V">The type of the value.</typeparam>
814
[DebuggerDisplay("IsValueCreated={initializer == null}, Value={ScopeIfCreated}")]
915
public sealed class ScopedAsyncAtomicFactory<K, V> : IScoped<V>, IDisposable where V : IDisposable
1016
{
1117
private Scoped<V> scope;
1218
private Initializer initializer;
1319

20+
/// <summary>
21+
/// Initializes a new instance of the <see cref="ScopedAsyncAtomicFactory{K, V}"/> class.
22+
/// </summary>
1423
public ScopedAsyncAtomicFactory()
1524
{
1625
initializer = new Initializer();
1726
}
1827

28+
/// <summary>
29+
/// Initializes a new instance of the <see cref="ScopedAsyncAtomicFactory{K, V}"/> class with the
30+
/// specified value.
31+
/// </summary>
32+
/// <param name="value">The value.</param>
1933
public ScopedAsyncAtomicFactory(V value)
2034
{
2135
scope = new Scoped<V>(value);
2236
}
2337

38+
/// <summary>
39+
/// Gets the scope if it has been initialized, else default.
40+
/// </summary>
2441
public Scoped<V> ScopeIfCreated
2542
{
2643
get
@@ -34,6 +51,12 @@ public Scoped<V> ScopeIfCreated
3451
}
3552
}
3653

54+
/// <summary>
55+
/// Attempts to create a lifetime for the scoped value. The lifetime guarantees the value is alive until
56+
/// the lifetime is disposed.
57+
/// </summary>
58+
/// <param name="lifetime">When this method returns, contains the Lifetime that was created, or the default value of the type if the operation failed.</param>
59+
/// <returns>true if the Lifetime was created; otherwise false.</returns>
3760
public bool TryCreateLifetime(out Lifetime<V> lifetime)
3861
{
3962
if (scope?.IsDisposed ?? false || initializer != null)
@@ -45,6 +68,13 @@ public bool TryCreateLifetime(out Lifetime<V> lifetime)
4568
return scope.TryCreateLifetime(out lifetime);
4669
}
4770

71+
/// <summary>
72+
/// Attempts to create a lifetime for the scoped value. The lifetime guarantees the value is alive until
73+
/// the lifetime is disposed.
74+
/// </summary>
75+
/// <param name="key">The key associated with the scoped value.</param>
76+
/// <param name="valueFactory">The value factory to use to create the scoped value when it is not initialized.</param>
77+
/// <returns>true if the Lifetime was created; otherwise false. If the lifetime was created, the new lifetime is also returned.</returns>
4878
public async ValueTask<(bool success, Lifetime<V> lifetime)> TryCreateLifetimeAsync(K key, Func<K, Task<Scoped<V>>> valueFactory)
4979
{
5080
// if disposed, return
@@ -73,6 +103,11 @@ private async ValueTask InitializeScopeAsync(K key, Func<K, Task<Scoped<V>>> val
73103
initializer = null;
74104
}
75105
}
106+
107+
/// <summary>
108+
/// Terminates the scope and disposes the value. Once the scope is terminated, it is no longer
109+
/// possible to create new lifetimes for the value.
110+
/// </summary>
76111
public void Dispose()
77112
{
78113
var init = initializer;
@@ -90,7 +125,7 @@ public void Dispose()
90125

91126
private class Initializer
92127
{
93-
private object syncLock = new object();
128+
private readonly object syncLock = new object();
94129
private bool isTaskInitialized;
95130
private bool isTaskCompleted;
96131
private bool isDisposeRequested;

0 commit comments

Comments
 (0)