Soenneker.Validators.ExpiringKey
11.0.1376
Prefix Reserved
dotnet add package Soenneker.Validators.ExpiringKey --version 11.0.1376
NuGet\Install-Package Soenneker.Validators.ExpiringKey -Version 11.0.1376
<PackageReference Include="Soenneker.Validators.ExpiringKey" Version="11.0.1376" />
<PackageVersion Include="Soenneker.Validators.ExpiringKey" Version="11.0.1376" />
<PackageReference Include="Soenneker.Validators.ExpiringKey" />
paket add Soenneker.Validators.ExpiringKey --version 11.0.1376
#r "nuget: Soenneker.Validators.ExpiringKey, 11.0.1376"
#:package Soenneker.Validators.ExpiringKey@11.0.1376
#addin nuget:?package=Soenneker.Validators.ExpiringKey&version=11.0.1376
#tool nuget:?package=Soenneker.Validators.ExpiringKey&version=11.0.1376
Soenneker.Validators.ExpiringKey
An in-memory, thread-safe expiring-key gate for suppressing duplicate work within a time window.
Installation
dotnet add package Soenneker.Validators.ExpiringKey
Registration
using Soenneker.Validators.ExpiringKey.Registrars;
services.AddExpiringKeyValidatorAsSingleton();
Singleton registration shares keys across all callers in the process. Scoped registration creates an independent key set per dependency-injection scope. The validator requires an ILogger<ExpiringKeyValidator> when constructed manually.
Atomic check-and-add
bool accepted = validator.ValidateAndAdd(
key: $"webhook:{eventId}",
expirationTimeMilliseconds: 30_000);
if (!accepted)
return; // The key already exists in the current window.
ValidateAndAdd is the safest operation for duplicate suppression because the existence check and insertion are atomic. It returns true only to the caller that added the key. The key is removed by its timer after the supplied interval.
Individual operations
validator.Add("job:42", 5_000);
bool available = validator.Validate("job:42");
// false: the key exists
validator.Remove("job:42");
available = validator.Validate("job:42");
// true: the key is absent
Validate has intentionally inverted gate semantics: it returns true when the key does not exist and false while the key is present. It does not reserve an absent key, so a separate Validate followed by Add is racy under concurrency; use ValidateAndAdd when only one caller may proceed.
Add attempts insertion and returns no result. If the key already exists, its original expiration is retained; the call does not refresh or replace its timer. Remove is idempotent and synchronously disposes the removed timer.
Expiration and lifetime
Expiration is an in-process timer duration in milliseconds. Values below -1 throw ArgumentOutOfRangeException; -1 represents an infinite timeout. Expiration callbacks run asynchronously, so a zero-duration key may be observable briefly before its callback removes it.
This is not a distributed lock, durable cache, session store, or cross-process idempotency mechanism. Process restarts lose all keys, and separate validator instances do not share state.
The validator owns one timer per live key. Dispose it to release all timers; dependency injection handles disposal for registered instances. Calls after disposal follow the underlying dictionary's disposed-object behavior.
Keys use the underlying concurrent dictionary's default string comparison. Avoid placing secrets or unbounded attacker-controlled values into a long-lived singleton without applying size and cardinality limits at the caller.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net11.0 is compatible. |
-
net11.0
- Soenneker.Dictionaries.ExpiringKey (>= 11.0.741)
- Soenneker.Validators.Validator (>= 11.0.758)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 11.0.1376 | 54 | 12/2/2026 |
| 11.0.1375 | 96 | 12/1/2026 |
| 11.0.1374 | 100 | 11/30/2026 |
| 11.0.1373 | 107 | 11/18/2026 |
| 11.0.1372 | 107 | 11/18/2026 |
| 11.0.1370 | 130 | 11/11/2026 |
| 11.0.1369 | 102 | 11/11/2026 |
| 11.0.1367 | 244 | 10/27/2026 |
| 11.0.1366 | 113 | 10/27/2026 |
| 11.0.1365 | 111 | 10/26/2026 |
| 11.0.1364 | 101 | 10/26/2026 |
| 11.0.1363 | 114 | 10/26/2026 |
| 11.0.1362 | 108 | 10/26/2026 |
| 11.0.1361 | 111 | 10/26/2026 |
| 4.0.1360 | 110 | 10/25/2026 |
| 4.0.1359 | 141 | 10/14/2026 |
| 4.0.1358 | 127 | 10/14/2026 |
| 4.0.1357 | 113 | 10/14/2026 |
| 4.0.1356 | 120 | 10/14/2026 |
| 4.0.1355 | 110 | 10/14/2026 |
Updated Multiple NuGet packages