SimplyWorks.RedisCounter 8.0.1

dotnet add package SimplyWorks.RedisCounter --version 8.0.1
                    
NuGet\Install-Package SimplyWorks.RedisCounter -Version 8.0.1
                    
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="SimplyWorks.RedisCounter" Version="8.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimplyWorks.RedisCounter" Version="8.0.1" />
                    
Directory.Packages.props
<PackageReference Include="SimplyWorks.RedisCounter" />
                    
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 SimplyWorks.RedisCounter --version 8.0.1
                    
#r "nuget: SimplyWorks.RedisCounter, 8.0.1"
                    
#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 SimplyWorks.RedisCounter@8.0.1
                    
#: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=SimplyWorks.RedisCounter&version=8.0.1
                    
Install as a Cake Addin
#tool nuget:?package=SimplyWorks.RedisCounter&version=8.0.1
                    
Install as a Cake Tool

SW.RedisCounter

Build and Publish NuGet Package NuGet version License: 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:

Install-Package SimplyWorks.RedisCounter

Or via .NET CLI:

dotnet add package SimplyWorks.RedisCounter

Configuration

Add Redis configuration to your appsettings.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:

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

[ApiController]
[Route("[controller]")]
public class MyController : ControllerBase
{
    private readonly IAtomicCounter _counter;

    public MyController(IAtomicCounter counter)
    {
        _counter = counter;
    }

    [HttpGet]
    public async Task<IActionResult> 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

public interface IAtomicCounter
{
    Task<long> 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

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.

Contributing

Contributions are welcome! Please feel free to submit a Pull Request.

Repository

Source code is available at: https://github.com/simplify9/SW-RedisCounter

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  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

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
8.0.1 355 9/16/2025
8.0.0 334 9/16/2025
1.0.1 1,978 10/28/2020