https://github.com/simplify9/sw-rediscounter
https://github.com/simplify9/sw-rediscounter
Last synced: 4 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/simplify9/sw-rediscounter
- Owner: simplify9
- License: mit
- Created: 2020-10-28T13:54:36.000Z (over 5 years ago)
- Default Branch: main
- Last Pushed: 2025-09-16T12:37:27.000Z (9 months ago)
- Last Synced: 2025-09-16T14:44:13.517Z (9 months ago)
- Language: C#
- Size: 19.5 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: .github/CONTRIBUTING.md
- License: LICENSE
- Code of conduct: .github/CODE_OF_CONDUCT.md
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# SW.RedisCounter
[](https://github.com/simplify9/SW-RedisCounter/actions/workflows/nuget-publish.yml)
[](https://badge.fury.io/nu/SimplyWorks.RedisCounter)
[](https://opensource.org/licenses/MIT)
A .NET Core library that provides Redis-based atomic counters with environment-aware key namespacing for distributed applications.
## Features
- **Atomic Operations**: Thread-safe increment and reset operations using Redis
- **Environment Aware**: Automatically namespaces keys with application name and environment
- **Easy Integration**: Simple dependency injection setup for ASP.NET Core applications
- **SSL Support**: Built-in SSL support for secure Redis connections
- **Multiple Server Support**: Supports comma-separated Redis server endpoints
## Installation
Install the package via NuGet Package Manager:
```bash
Install-Package SimplyWorks.RedisCounter
```
Or via .NET CLI:
```bash
dotnet add package SimplyWorks.RedisCounter
```
## Configuration
Add Redis configuration to your `appsettings.json`:
```json
{
"Redis": {
"ApplicationName": "MyApp",
"Password": "your-redis-password",
"Server": "redis-server1.com:6380,redis-server2.com:6380"
}
}
```
## Usage
### 1. Register the Service
In your `Startup.cs` or `Program.cs`:
```csharp
public void ConfigureServices(IServiceCollection services)
{
// Register Redis Counter with configuration from appsettings.json
services.AddRedisCounter();
// Or configure programmatically
services.AddRedisCounter(options =>
{
options.ApplicationName = "MyApp";
options.Password = "your-redis-password";
options.Server = "redis-server.com:6380";
});
}
```
### 2. Use in Your Controllers/Services
```csharp
[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase
{
private readonly IAtomicCounter _counter;
public MyController(IAtomicCounter counter)
{
_counter = counter;
}
[HttpGet]
public async Task Get()
{
// Increment a counter by 1
var count = await _counter.IncrementAsync("api-calls");
// Increment by a specific amount
var pageViews = await _counter.IncrementAsync("page-views", 5);
// Reset a counter
await _counter.ResetAsync("api-calls");
return Ok(new { ApiCalls = count, PageViews = pageViews });
}
}
```
## Key Naming Convention
Keys are automatically namespaced using the pattern:
```
{ApplicationName}:{EnvironmentName}:{CounterName}
```
For example, with:
- ApplicationName: "MyApp"
- Environment: "Production"
- CounterName: "api-calls"
The Redis key becomes: `myapp:production:api-calls`
## API Reference
### IAtomicCounter Interface
```csharp
public interface IAtomicCounter
{
Task IncrementAsync(string counterName, long increment = 1);
Task ResetAsync(string counterName);
}
```
#### Methods
- **IncrementAsync(string counterName, long increment = 1)**
- Atomically increments the counter by the specified amount
- Returns the new counter value
- Default increment is 1
- **ResetAsync(string counterName)**
- Deletes the counter (resets to 0)
- Returns a Task for async completion
### RedisOptions Class
```csharp
public class RedisOptions
{
public string ApplicationName { get; set; }
public string Password { get; set; }
public string Server { get; set; }
}
```
## Dependencies
- **StackExchange.Redis**: Redis client for .NET
- **SimplyWorks.PrimitiveTypes**: Provides the IAtomicCounter interface
- **Microsoft.AspNetCore.App**: ASP.NET Core framework
## Target Framework
- .NET 8
## License
This project is licensed under the [MIT License](LICENSE).
## Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
## Repository
Source code is available at: https://github.com/simplify9/SW-RedisCounter