Redis Caching

When your application grows beyond a single server, Redis is the gold standard for distributed caching. It’s fast, scalable, and accessible from anywhere—perfect for microservices, cloud deployments, and high-availability scenarios.

REN.Kit.CacheKit makes using Redis cache in your .NET 8 or .NET 9 projects not only straightforward, but enterprise-ready. You get high performance, reliability, and all the power of Redis, with a developer-friendly interface.


1. Configuration: Define Redis Settings in appsettings.json

Before using Redis, add the following configuration block to your appsettings.json:

"CacheConfiguration": {
  "RedisConfiguration": {
    "Url": "localhost:6379",
    "TimeConfiguration": {
      "AbsoluteExpirationInHours": 12
    },
    "DatabaseId": 4,
    "Username": "default",
    "Password": "ofqa9YpQ6iw5tmDsoH5EW1OTMJtKs2Gs",
    "AbortOnConnectFail": false,
    "IsAdmin":  false
  }
}
  • Url: Address of your Redis server (can be remote or localhost)

  • AbsoluteExpirationInHours: The max lifetime for a cache entry

  • DatabaseId: Redis logical database (usually 0)

  • Username/Password: For secured Redis clusters

  • AbortOnConnectFail: Should connections be retried or fail fast

  • IsAdmin: (Optional) Set to true if you want to allow administrative operations like FlushDb.

⚠️ Security Tip:


2. Service Registration

After configuring your Redis cache, register REN.Kit for Redis in your Program.cs:

builder.Services.AddRENCaching(RegisterRENCaching.CacheType.Redis);

You can now inject IRENCacheService into your classes and use Redis for high-performance, distributed caching.

Here’s how the core registration works under the hood:

public static void AddRENCaching(this IServiceCollection services, CacheType cacheType)
{
    switch (cacheType)
    {
        case CacheType.InMemory:
            AddInMemoryRENCache<RENInMemoryCacheService>(services);
            break;
        case CacheType.Redis:
            AddRedisRENCache<RENRedisCacheService>(services);
            break;
        default:
            throw new ArgumentOutOfRangeException(nameof(cacheType), cacheType, null);
    }
}

Why Choose Redis Cache?

  • Scalable: Multiple app instances can share the same cache instantly.

  • High Availability: Use with Redis Sentinel or clustering for failover.

  • Lightning Fast: Network lookups in microseconds, with optional persistence.

  • Perfect for:

    • Distributed session storage

    • User authentication tokens

    • Large data objects shared across services

    • Rate limiting, leaderboards, and more

Pro Tips:

Why is Clear() supported for Redis, but not for In-Memory?

Last updated