Orchard Core Redis - Prompt Templates
Configure Redis Integration
You are an Orchard Core expert. Generate code and configuration for Redis integration including Redis cache, message bus, distributed locking, data protection, health checks, configuration, key storage, and multi-instance deployment.
Guidelines
- Enable
OrchardCore.Redisas the base feature for Redis configuration support. - Enable
OrchardCore.Redis.Cachefor distributed caching using Redis as theIDistributedCachebackend. - Enable
OrchardCore.Redis.Busfor distributedISignalservice, making cache invalidation work across multiple application instances. - Enable
OrchardCore.Redis.Lockfor distributed locking using Redis. - Enable
OrchardCore.Redis.DataProtectionfor storing ASP.NET Core Data Protection keys in Redis. - Configure the Redis connection string via
OrchardCore_Redis:Configurationinappsettings.json. - Use
InstancePrefixto namespace all Redis keys and prevent collisions between environments or applications. - Data Protection keys must be stored in durable storage; ensure Redis persistence (AOF or RDB) is enabled.
- The Redis module provides a health check for monitoring Redis server status.
- For multi-instance deployments, enable Redis Bus so that
ISignalcache invalidation propagates across all nodes. - All recipe JSON must be wrapped in
{ "steps": [...] }. - All C# classes must use the
sealedmodifier.
Enabling Redis Features
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.Redis",
"OrchardCore.Redis.Cache",
"OrchardCore.Redis.Bus",
"OrchardCore.Redis.Lock",
"OrchardCore.Redis.DataProtection"
],
"disable": []
}
]
}
Redis Features Summary
| Feature | Module ID | Description |
|---|---|---|
| Redis | OrchardCore.Redis |
Base Redis configuration support |
| Redis Cache | OrchardCore.Redis.Cache |
Distributed cache using Redis as IDistributedCache backend |
| Redis Bus | OrchardCore.Redis.Bus |
Distributed ISignal service for cross-instance cache invalidation |
| Redis Lock | OrchardCore.Redis.Lock |
Distributed locking for coordinating work across instances |
| Redis DataProtection | OrchardCore.Redis.DataProtection |
Stores ASP.NET Core Data Protection keys in Redis |
Redis Configuration
Configure the Redis connection in appsettings.json:
{
"OrchardCore": {
"OrchardCore_Redis": {
"Configuration": "localhost:6379,abortConnect=false,connectTimeout=5000",
"InstancePrefix": "MyApp:",
"AllowAdmin": true
}
}
}
Configuration Options
| Option | Description | Required |
|---|---|---|
Configuration |
The Redis connection string | Yes |
InstancePrefix |
A prefix added to all Redis keys to prevent collisions | No |
AllowAdmin |
Whether to allow admin commands (enables persistence check) | No (defaults to false) |
Production Configuration Example
For a production multi-instance deployment with SSL:
{
"OrchardCore": {
"OrchardCore_Redis": {
"Configuration": "myredis.example.com:6380,password=MySecurePassword,ssl=true,abortConnect=false,connectTimeout=5000,syncTimeout=5000",
"InstancePrefix": "Production:",
"AllowAdmin": true
}
}
}
Key Storage Structure
Redis Data Protection stores keys using the following pattern:
{InstancePrefix}{TenantName}:DataProtection-Keys
Examples:
| InstancePrefix | Tenant | Redis Key |
|---|---|---|
MyApp: |
Default |
MyApp:Default:DataProtection-Keys |
MyApp: |
Tenant1 |
MyApp:Tenant1:DataProtection-Keys |
| (none) | Default |
Default:DataProtection-Keys |
Data Protection
The OrchardCore.Redis.DataProtection feature stores ASP.NET Core Data Protection keys in Redis. This is essential for:
- Load-balanced environments: All instances share the same data protection keys.
- Multi-tenant deployments: Each tenant gets its own key ring in Redis.
- Protecting sensitive data: Authentication cookies, anti-forgery tokens, and persisted secrets (e.g., SMTP passwords).
Prerequisites
- A running Redis instance with persistence enabled.
OrchardCore.Redisfeature enabled and configured.OrchardCore.Redis.DataProtectionfeature enabled.
Persistence Warning
Data protection keyrings are not cache files and must be kept in durable storage. Ensure Redis has a backup strategy:
- AOF (Append-Only File): Logs every write operation for point-in-time recovery.
- RDB (Redis Database): Creates periodic snapshots.
The module automatically checks if persistence is enabled (when AllowAdmin is true) and logs a warning if it is not configured.
Redis Cache
When OrchardCore.Redis.Cache is enabled, Redis replaces the default in-memory IDistributedCache. All Orchard Core features that use IDistributedCache automatically benefit from distributed caching.
Use IDistributedCache to store and retrieve cached data:
using Microsoft.Extensions.Caching.Distributed;
public sealed class CachedProductService
{
private readonly IDistributedCache _cache;
public CachedProductService(IDistributedCache cache)
{
_cache = cache;
}
public async Task<string?> GetProductAsync(string productId)
{
return await _cache.GetStringAsync($"product:{productId}");
}
public async Task SetProductAsync(string productId, string data)
{
var options = new DistributedCacheEntryOptions
{
AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(30),
SlidingExpiration = TimeSpan.FromMinutes(10),
};
await _cache.SetStringAsync($"product:{productId}", data, options);
}
}
Redis Bus (Distributed Signal)
When OrchardCore.Redis.Bus is enabled, the ISignal service becomes distributed. Cache invalidation signals propagate across all application instances via Redis Pub/Sub.
This is critical for multi-instance deployments. Without Redis Bus, signaling cache invalidation on one instance does not affect other instances.
using OrchardCore.Environment.Cache;
public sealed class ProductCacheInvalidator
{
private readonly ISignal _signal;
public ProductCacheInvalidator(ISignal signal)
{
_signal = signal;
}
public async Task InvalidateAsync()
{
// This signal propagates to ALL instances via Redis Bus.
await _signal.SignalTokenAsync("productcatalog");
}
}
Redis Lock (Distributed Locking)
When OrchardCore.Redis.Lock is enabled, Orchard Core uses Redis for distributed locking via IDistributedLock. This ensures only one instance executes a critical section at a time:
using OrchardCore.Locking.Distributed;
public sealed class ImportService
{
private readonly IDistributedLock _distributedLock;
public ImportService(IDistributedLock distributedLock)
{
_distributedLock = distributedLock;
}
public async Task RunImportAsync()
{
(var locker, var locked) = await _distributedLock.TryAcquireLockAsync(
"IMPORT_LOCK",
TimeSpan.FromMinutes(5),
TimeSpan.FromSeconds(30));
if (!locked)
{
return; // Another instance is already running the import.
}
await using var _ = locker;
// Perform the import safely knowing no other instance is doing it.
await ExecuteImportAsync();
}
private Task ExecuteImportAsync()
{
// Import logic here.
return Task.CompletedTask;
}
}
Health Checks
The Redis module provides a health check to report the status of the Redis server. This integrates with ASP.NET Core health checks and can be used for load balancer probes.
Enable the health check feature:
{
"steps": [
{
"name": "Feature",
"enable": [
"OrchardCore.Redis",
"OrchardCore.HealthChecks"
],
"disable": []
}
]
}
Multi-Instance Deployment Checklist
For load-balanced or multi-instance Orchard Core deployments using Redis:
- Enable
OrchardCore.Redisand configure the connection string. - Enable
OrchardCore.Redis.Cacheso all instances share the same distributed cache. - Enable
OrchardCore.Redis.BussoISignalcache invalidation propagates across all nodes. - Enable
OrchardCore.Redis.Lockto coordinate background tasks and prevent duplicate work. - Enable
OrchardCore.Redis.DataProtectionso all instances share data protection keys. - Set
InstancePrefixto namespace keys and prevent collisions with other applications sharing the same Redis instance. - Enable Redis persistence (AOF or RDB) to prevent data loss on Redis restart.
- Set
AllowAdmin: truein configuration so the module can verify persistence is enabled.