https://github.com/soenneker/soenneker.validators.expiringkey
A validation module that checks for keys, stores them, expires them after an amount of time
https://github.com/soenneker/soenneker.validators.expiringkey
csharp dotnet expiration expiring expiringkey expiringkeyvalidator key validator validators
Last synced: 12 days ago
JSON representation
A validation module that checks for keys, stores them, expires them after an amount of time
- Host: GitHub
- URL: https://github.com/soenneker/soenneker.validators.expiringkey
- Owner: soenneker
- License: mit
- Created: 2024-06-11T23:47:47.000Z (about 2 years ago)
- Default Branch: main
- Last Pushed: 2026-06-06T16:42:23.000Z (15 days ago)
- Last Synced: 2026-06-06T17:15:55.258Z (15 days ago)
- Topics: csharp, dotnet, expiration, expiring, expiringkey, expiringkeyvalidator, key, validator, validators
- Language: C#
- Homepage: https://soenneker.com
- Size: 1.7 MB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Security: .github/SECURITY.md
Awesome Lists containing this project
README
[](https://www.nuget.org/packages/soenneker.validators.expiringkey/)
[](https://github.com/soenneker/soenneker.validators.expiringkey/actions/workflows/publish-package.yml)
[](https://www.nuget.org/packages/soenneker.validators.expiringkey/)
[](https://github.com/soenneker/soenneker.validators.expiringkey/actions/workflows/codeql.yml)
#  Soenneker.Validators.ExpiringKey
### A validation module that checks for keys, stores them, expires them after an amount of time
Ideal for caching, session management, and more.
## ?? Features
- **Validate Key**: Check if a key exists.
- **Add Key**: Add a key with an expiration time.
- **Validate and Add**: Validate if a key exists and add it if not.
- **Remove Key**: Remove a key.
## Installation
```
dotnet add package Soenneker.Validators.ExpiringKey
```
## ?? Usage
`IExpiringKeyValidator` can be registered within DI, and injected:
```csharp
public static async Task Main(string[] args)
{
...
builder.Services.AddExpiringKeyValidatorAsSingleton();
}
```
or it can be initialized manually: `new ExpiringKeyValidator()`.
### Validate Key
Check if a key is present.
```csharp
bool Validate(string key)
```
### Add Key
Add a key with an expiration time.
```csharp
void Add(string key, int expirationTimeMilliseconds)
```
### Validate and Add Key
Validate a key and add it if it doesn't exist.
```csharp
bool ValidateAndAdd(string key, int expirationTimeMilliseconds) // true if doesn't exist, false if it does
```
### Remove Key
Remove a key.
```csharp
void Remove(string key)
```
## Example
```csharp
var validator = new ExpiringKeyValidator();
validator.Add("key1", 5000); // 5 seconds
var invalid = validator.Validate("key1"); // false, key exists
await Task.Delay(7000); // wait 7 seconds
var validAfterTime = validator.Validate("key1"); // true, key does not exist
```