# 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`:

```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`.

{% hint style="warning" %}
## ⚠️ **Security Tip:**&#x20;

Only enable `IsAdmin` if your use case requires it—such as calling `Clear()` to flush all keys.
{% endhint %}

***

## 2. Service Registration

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

```csharp
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:

```csharp
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

{% hint style="success" %}
## **Pro Tips:**

* **Use Redis when your application runs on more than one server, container, or instance.**
* **For cloud or Kubernetes deployments, Redis is almost always the right choice for distributed caching.**
* Don’t forget: You can override or extend any REN.Kit cache service for custom logic!
{% endhint %}

{% hint style="warning" %}
#### Why is Clear() supported for Redis, but not for In-Memory?

Because Redis supports the `FLUSHDB` operation natively, while IMemoryCache does **not** support clearing all entries at once.\
In distributed environments, this allows you to quickly reset the cache for all connected app instances. Remember to enable `"IsAdmin": true` if you plan to use this feature.
{% endhint %}
