Skip to content

Commit f40280d

Browse files
author
Mykyta Zotov
committed
refactor: simplify variable declarations and update cache builder references
1 parent 125a927 commit f40280d

23 files changed

Lines changed: 1653 additions & 213 deletions

README.md

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -144,23 +144,17 @@ For detailed comparison and guidance, see `docs/storage-strategies.md`.
144144

145145
```csharp
146146
using SlidingWindowCache;
147-
using SlidingWindowCache.Configuration;
147+
using SlidingWindowCache.Public.Cache;
148+
using SlidingWindowCache.Public.Configuration;
148149
using Intervals.NET;
149150
using Intervals.NET.Domain.Default.Numeric;
150151

151-
var options = new WindowCacheOptions(
152-
leftCacheSize: 1.0, // Cache 100% of requested range size to the left
153-
rightCacheSize: 2.0, // Cache 200% of requested range size to the right
154-
leftThreshold: 0.2, // Rebalance if <20% left buffer remains
155-
rightThreshold: 0.2 // Rebalance if <20% right buffer remains
156-
);
157-
158-
var cache = WindowCache<int, string, IntegerFixedStepDomain>.Create(
159-
dataSource: myDataSource,
160-
domain: new IntegerFixedStepDomain(),
161-
options: options,
162-
readMode: UserCacheReadMode.Snapshot
163-
);
152+
await using var cache = WindowCacheBuilder.For(myDataSource, new IntegerFixedStepDomain())
153+
.WithOptions(o => o
154+
.WithCacheSize(left: 1.0, right: 2.0) // 100% left / 200% right of requested range
155+
.WithReadMode(UserCacheReadMode.Snapshot)
156+
.WithThresholds(0.2)) // rebalance if <20% buffer remains
157+
.Build();
164158

165159
var result = await cache.GetDataAsync(Range.Closed(100, 200), cancellationToken);
166160

@@ -495,8 +489,7 @@ This is the per-request programmatic alternative to the `UserRequestFullCacheHit
495489
For workloads with high-latency data sources, you can compose multiple `WindowCache` instances into a layered stack. Each layer uses the layer below it as its data source, allowing you to trade memory for reduced data-source I/O.
496490

497491
```csharp
498-
await using var cache = LayeredWindowCacheBuilder<int, byte[], IntegerFixedStepDomain>
499-
.Create(realDataSource, domain)
492+
await using var cache = WindowCacheBuilder.Layered(realDataSource, domain)
500493
.AddLayer(new WindowCacheOptions( // L2: deep background cache
501494
leftCacheSize: 10.0,
502495
rightCacheSize: 10.0,
@@ -547,8 +540,7 @@ layeredCache.UpdateRuntimeOptions(u => u.WithRightCacheSize(1.0));
547540
548541
**Three-layer example:**
549542
```csharp
550-
await using var cache = LayeredWindowCacheBuilder<int, byte[], IntegerFixedStepDomain>
551-
.Create(realDataSource, domain)
543+
await using var cache = WindowCacheBuilder.Layered(realDataSource, domain)
552544
.AddLayer(l3Options) // L3: 10× CopyOnRead — network/disk absorber
553545
.AddLayer(l2Options) // L2: 2× CopyOnRead — mid-level buffer
554546
.AddLayer(l1Options) // L1: 0.5× Snapshot — user-facing

docs/components/public-api.md

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -227,22 +227,21 @@ Typically created via `LayeredWindowCacheBuilder.Build()` rather than directly.
227227

228228
### LayeredWindowCacheBuilder\<TRange, TData, TDomain\>
229229

230-
**File**: `src/SlidingWindowCache/Public/LayeredWindowCacheBuilder.cs`
230+
**File**: `src/SlidingWindowCache/Public/Cache/LayeredWindowCacheBuilder.cs`
231231

232232
**Type**: `sealed class` — fluent builder
233233

234234
```csharp
235-
await using var cache = LayeredWindowCacheBuilder<int, byte[], IntegerFixedStepDomain>
236-
.Create(realDataSource, domain)
235+
await using var cache = WindowCacheBuilder.Layered(realDataSource, domain)
237236
.AddLayer(deepOptions) // L2: inner layer (CopyOnRead, large buffers)
238237
.AddLayer(userOptions) // L1: outer layer (Snapshot, small buffers)
239238
.Build();
240239
```
241240

242-
- `Create(dataSource, domain)`factory entry point; validates both `dataSource` and `domain` are not null.
243-
- `AddLayer(options, diagnostics?)` — adds a layer on top; first call = innermost layer, last call = outermost (user-facing).
244-
- `Build()` — constructs all `WindowCache` instances, wires them via `WindowCacheDataSourceAdapter`, and wraps them in `LayeredWindowCache`.
245-
- Throws `InvalidOperationException` from `Build()` if no layers were added.
241+
- Obtain an instance via `WindowCacheBuilder.Layered(dataSource, domain)`enables full generic type inference.
242+
- `AddLayer(options, diagnostics?)` — adds a layer on top; first call = innermost layer, last call = outermost (user-facing). Also accepts `Action<WindowCacheOptionsBuilder>` for inline configuration.
243+
- `Build()` — constructs all `WindowCache` instances, wires them via `WindowCacheDataSourceAdapter`, and wraps them in `LayeredWindowCache`. Returns `IWindowCache<TRange, TData, TDomain>`; concrete type is `LayeredWindowCache<>`.
244+
- Throws `InvalidOperationException` from `Build()` if no layers were added, or if an inline delegate fails validation.
246245

247246
**See**: `README.md` (Multi-Layer Cache section) and `docs/storage-strategies.md` for recommended layer configuration patterns.
248247

docs/diagnostics.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -782,8 +782,7 @@ Pass a diagnostics instance as the second argument to `AddLayer`:
782782
var l2Diagnostics = new EventCounterCacheDiagnostics();
783783
var l1Diagnostics = new EventCounterCacheDiagnostics();
784784

785-
await using var cache = LayeredWindowCacheBuilder<int, byte[], IntegerFixedStepDomain>
786-
.Create(realDataSource, domain)
785+
await using var cache = WindowCacheBuilder.Layered(realDataSource, domain)
787786
.AddLayer(deepOptions, l2Diagnostics) // L2: inner / deep layer
788787
.AddLayer(userOptions, l1Diagnostics) // L1: outermost / user-facing layer
789788
.Build();

docs/glossary.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ WindowCacheDataSourceAdapter
163163
- Adapts an `IWindowCache` to the `IDataSource` interface, enabling it to act as the backing store for an outer `WindowCache`. This is the composition point for building layered caches. The adapter does not own the inner cache; ownership is managed by `LayeredWindowCache`. See `src/SlidingWindowCache/Public/WindowCacheDataSourceAdapter.cs`.
164164

165165
LayeredWindowCacheBuilder
166-
- Fluent builder that wires `WindowCache` layers into a `LayeredWindowCache`. Layers are added bottom-up (deepest/innermost first, user-facing last). Each `AddLayer` call adds one `WindowCache` on top of the current stack. `Build()` returns a `LayeredWindowCache` that owns all layers. See `src/SlidingWindowCache/Public/LayeredWindowCacheBuilder.cs`.
166+
- Fluent builder that wires `WindowCache` layers into a `LayeredWindowCache`. Obtain an instance via `WindowCacheBuilder.Layered(dataSource, domain)`. Layers are added bottom-up (deepest/innermost first, user-facing last). Each `AddLayer` call accepts either a pre-built `WindowCacheOptions` or an `Action<WindowCacheOptionsBuilder>` for inline configuration. `Build()` returns `IWindowCache<>` (concrete type: `LayeredWindowCache<>`). See `src/SlidingWindowCache/Public/Cache/LayeredWindowCacheBuilder.cs`.
167167

168168
LayeredWindowCache
169169
- A thin `IWindowCache` wrapper that owns a stack of `WindowCache` layers. Delegates `GetDataAsync` to the outermost layer. `WaitForIdleAsync` awaits all layers sequentially, outermost to innermost, ensuring full-stack convergence (required for correct behavior of `GetDataAndWaitForIdleAsync`). Disposes all layers outermost-first on `DisposeAsync`. Exposes `LayerCount` and `Layers`. See `src/SlidingWindowCache/Public/LayeredWindowCache.cs`.

docs/scenarios.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -438,8 +438,7 @@ from Lₙ's new window, and finally L1 expands from L2.
438438
var l2Diagnostics = new EventCounterCacheDiagnostics();
439439
var l1Diagnostics = new EventCounterCacheDiagnostics();
440440

441-
await using var cache = LayeredWindowCacheBuilder<int, byte[], IntegerFixedStepDomain>
442-
.Create(dataSource, domain)
441+
await using var cache = WindowCacheBuilder.Layered(dataSource, domain)
443442
.AddLayer(deepOptions, l2Diagnostics) // L2
444443
.AddLayer(userOptions, l1Diagnostics) // L1
445444
.Build();

docs/storage-strategies.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ The library provides built-in support for layered cache composition via `Layered
205205

206206
```csharp
207207
// Two-layer cache: L2 (CopyOnRead, large) → L1 (Snapshot, small)
208-
await using var cache = LayeredWindowCacheBuilder<int, byte[], IntegerFixedStepDomain>
209-
.Create(slowDataSource, domain) // real (bottom-most) data source
208+
await using var cache = WindowCacheBuilder.Layered(slowDataSource, domain)
210209
.AddLayer(new WindowCacheOptions( // L2: deep background cache
211210
leftCacheSize: 10.0,
212211
rightCacheSize: 10.0,

src/SlidingWindowCache.WasmValidation/WasmCompilationValidator.cs

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -250,8 +250,8 @@ public static async Task ValidateConfiguration4_CopyOnReadMode_BoundedQueue()
250250
/// <para><strong>Types Validated:</strong></para>
251251
/// <list type="bullet">
252252
/// <item><description>
253-
/// <see cref="WindowCacheConsistencyExtensions.GetDataAndWaitForIdleAsync{TRange,TData,TDomain}"/> —
254-
/// strong consistency extension method; composes GetDataAsync + unconditional WaitForIdleAsync
253+
/// <see cref="WindowCacheConsistencyExtensions.GetDataAndWaitForIdleAsync{TRange,TData,TDomain}"/> —
254+
/// strong consistency extension method; composes GetDataAsync + unconditional WaitForIdleAsync
255255
/// </description></item>
256256
/// <item><description>
257257
/// The <c>try { await WaitForIdleAsync } catch (OperationCanceledException) { }</c> pattern
@@ -309,8 +309,8 @@ public static async Task ValidateStrongConsistencyMode_GetDataAndWaitForIdleAsyn
309309
/// <para><strong>Types Validated:</strong></para>
310310
/// <list type="bullet">
311311
/// <item><description>
312-
/// <see cref="WindowCacheConsistencyExtensions.GetDataAndWaitOnMissAsync{TRange,TData,TDomain}"/> —
313-
/// hybrid consistency extension method; composes GetDataAsync + conditional WaitForIdleAsync
312+
/// <see cref="WindowCacheConsistencyExtensions.GetDataAndWaitOnMissAsync{TRange,TData,TDomain}"/> —
313+
/// hybrid consistency extension method; composes GetDataAsync + conditional WaitForIdleAsync
314314
/// gated on <see cref="CacheInteraction"/>
315315
/// </description></item>
316316
/// <item><description>
@@ -424,19 +424,18 @@ public static async Task ValidateLayeredCache_TwoLayer_RecommendedConfig()
424424

425425
// Build the layered cache — exercises LayeredWindowCacheBuilder,
426426
// WindowCacheDataSourceAdapter, and LayeredWindowCache
427-
await using var cache = LayeredWindowCacheBuilder<int, int, IntegerFixedStepDomain>
428-
.Create(new SimpleDataSource(), domain)
427+
await using var layered = (LayeredWindowCache<int, int, IntegerFixedStepDomain>)WindowCacheBuilder.Layered(new SimpleDataSource(), domain)
429428
.AddLayer(innerOptions)
430429
.AddLayer(outerOptions)
431430
.Build();
432431

433432
var range = Intervals.NET.Factories.Range.Closed<int>(0, 10);
434-
var result = await cache.GetDataAsync(range, CancellationToken.None);
433+
var result = await layered.GetDataAsync(range, CancellationToken.None);
435434

436435
// WaitForIdleAsync on LayeredWindowCache awaits all layers (outermost to innermost)
437-
await cache.WaitForIdleAsync();
436+
await layered.WaitForIdleAsync();
438437

439438
_ = result.Data.Length;
440-
_ = cache.LayerCount;
439+
_ = layered.LayerCount;
441440
}
442441
}

src/SlidingWindowCache/Core/Rebalance/Execution/CacheDataExtensionService.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ CancellationToken cancellationToken
8888
_cacheDiagnostics.DataSourceFetchMissingSegments();
8989

9090
// Step 1: Calculate which ranges are missing (and record the expansion/replacement diagnostic)
91-
var missingRanges = CalculateMissingRanges(currentCache.Range, requested, out bool isCacheExpanded);
91+
var missingRanges = CalculateMissingRanges(currentCache.Range, requested, out var isCacheExpanded);
9292

9393
// Step 2: Record the diagnostic event here (caller context), not inside the pure helper
9494
if (isCacheExpanded)

src/SlidingWindowCache/Core/Rebalance/Intent/IntentController.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ private async Task ProcessIntentsAsync()
185185
{
186186
// Track whether we successfully consumed a semaphore signal
187187
// This prevents activity counter imbalance when disposal cancels WaitAsync
188-
bool consumedSignal = false;
188+
var consumedSignal = false;
189189

190190
try
191191
{

0 commit comments

Comments
 (0)