Soenneker.Sets.Concurrent.SlidingWindow 4.0.9

Prefix Reserved
dotnet add package Soenneker.Sets.Concurrent.SlidingWindow --version 4.0.9
                    
NuGet\Install-Package Soenneker.Sets.Concurrent.SlidingWindow -Version 4.0.9
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Soenneker.Sets.Concurrent.SlidingWindow" Version="4.0.9" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Soenneker.Sets.Concurrent.SlidingWindow" Version="4.0.9" />
                    
Directory.Packages.props
<PackageReference Include="Soenneker.Sets.Concurrent.SlidingWindow" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add Soenneker.Sets.Concurrent.SlidingWindow --version 4.0.9
                    
#r "nuget: Soenneker.Sets.Concurrent.SlidingWindow, 4.0.9"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package Soenneker.Sets.Concurrent.SlidingWindow@4.0.9
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=Soenneker.Sets.Concurrent.SlidingWindow&version=4.0.9
                    
Install as a Cake Addin
#tool nuget:?package=Soenneker.Sets.Concurrent.SlidingWindow&version=4.0.9
                    
Install as a Cake Tool

alternate text is missing from this package README image alternate text is missing from this package README image alternate text is missing from this package README image

alternate text is missing from this package README image Soenneker.Sets.Concurrent.SlidingWindow

A high-throughput, thread-safe set whose bucketed entries automatically expire after a fixed time window.

Soenneker.Sets.Concurrent.SlidingWindow provides a concurrent sliding-window set for .NET. Items added to the set automatically expire after a configurable time window without requiring manual cleanup.

The implementation is optimized for high-concurrency workloads and avoids expensive per-item timers by using a bucketed time-slice rotation system.

This makes it ideal for deduplication, rate limiting, and recent activity tracking.


Installation

dotnet add package Soenneker.Sets.Concurrent.SlidingWindow

Why this library exists

Many systems need to track items that should only exist for a limited period of time, such as:

  • recently processed messages
  • request IDs
  • phone numbers
  • event IDs
  • authentication tokens

Traditional options have downsides:

Approach Problem
ConcurrentDictionary Requires manual expiration
MemoryCache Heavy and feature-rich for simple tracking
Per-item timers Extremely expensive at scale
Background cleanup scans High CPU cost

SlidingWindowConcurrentSet solves this by using bucketed time slices where items automatically expire when their time window passes.


Key Features

✔ High-throughput concurrent operations

✔ Automatic expiration of entries

✔ Sliding window time-based retention

✔ Lock-minimized design

✔ Low allocation footprint

✔ No per-item timers

✔ Safe for heavy multi-threaded workloads


Example

using Soenneker.Sets.Concurrent.SlidingWindow;

var set = new SlidingWindowConcurrentSet<string>(
    window: TimeSpan.FromMinutes(5),
    rotationInterval: TimeSpan.FromSeconds(30)
);

set.TryAdd("alpha");

bool exists = set.Contains("alpha");

await Task.Delay(TimeSpan.FromMinutes(6));

bool expired = set.Contains("alpha"); // false

Configuration

var set = new SlidingWindowConcurrentSet<string>(
    window: TimeSpan.FromMinutes(10),
    rotationInterval: TimeSpan.FromSeconds(15)
);
Parameter Description
window Total time items remain valid
rotationInterval Time slice size used for bucket rotation
capacityHint Initial capacity hint for the internal dictionary
comparer Optional equality comparer

The window is internally divided into time buckets:

window / rotationInterval = number of buckets

Example:

window = 5 minutes
rotationInterval = 30 seconds
bucket count = 10

Each rotation advances the window and expires the oldest bucket.


How it works

The set maintains:

  • a ConcurrentDictionary for fast lookup
  • a ring buffer of queues representing time buckets

When an item is added:

  1. It is assigned the current time bucket
  2. The value is queued in that bucket
  3. The dictionary records the bucket index

A background rotation process periodically:

  1. Advances the active bucket
  2. Processes the expired bucket
  3. Removes items whose last activity falls outside the sliding window

This avoids scanning the entire collection.


Performance Characteristics

Operation Complexity
TryAdd O(1)
Contains O(1)
TryRemove O(1)
Expiration O(n) only for items in expiring bucket

The design ensures:

  • predictable memory usage
  • minimal locking
  • bounded cleanup work

Thread Safety

All operations are thread-safe.

The set is designed for high-concurrency environments and does not require external synchronization.


Disposal

The set uses an internal rotation task driven by PeriodicTimer.

When the set is no longer needed, it should be disposed:

set.Dispose();

or

await set.DisposeAsync();

This stops the internal rotation loop.

Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Soenneker.Sets.Concurrent.SlidingWindow:

Package Downloads
Soenneker.Deduplication.SlidingWindow

High-performance sliding-window deduplication for .NET.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
4.0.9 90 3/5/2026
4.0.8 32 3/5/2026
4.0.7 37 3/5/2026
4.0.6 48 3/5/2026
4.0.5 36 3/5/2026
4.0.4 33 3/5/2026
4.0.3 50 3/4/2026
4.0.2 35 3/4/2026
4.0.1 57 3/3/2026