Orchardcore redis

From CrestApps/CrestApps.AgentSkills · View interactive version

Orchardcore redis is an AI agent skill from the GitHub repository CrestApps/CrestApps.AgentSkills. Skill for configuring Redis in Orchard Core. Covers Redis cache, message bus, distributed locking, data protection, health checks, the OrchardCore Redis configuration section, environment variable and Key Vault secret names, InstancePrefix and AllowAdmin, key storage structure, the OrchardCore.Signa

How to use this skill

Skill file

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

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": {
    "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": {
    "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:

Prerequisites
  1. A running Redis instance with persistence enabled.
  2. OrchardCore.Redis feature enabled and configured.
  3. OrchardCore.Redis.DataProtection feature enabled.
Persistence Warning

Data protection keyrings are not cache files and must be kept in durable storage. Ensure Redis has a backup strategy:

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, 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": []
    }
  ]
}

SignalR Backplane and Media Gallery

{
  "steps": [
    {
      "name": "Feature",
      "enable": [
        "OrchardCore.Redis",
        "OrchardCore.Redis.Cache",
        "OrchardCore.Redis.Bus",
        "OrchardCore.Redis.Lock",
        "OrchardCore.Redis.DataProtection",
        "OrchardCore.SignalR.Redis"
      ],
      "disable": []
    }
  ]
}

Using Redis from a Module

IRedisService (OrchardCore.Redis.Abstractions) exposes ConnectAsync(), Connection, Database and InstancePrefix. Connection and Database stay null until a connection is created, which can fail, and are always null for NullRedisService:

using Microsoft.Extensions.Logging;
using OrchardCore.Environment.Shell;
using OrchardCore.Redis;

public sealed class CounterService
{
    private readonly IRedisService _redis;
    private readonly string _prefix;
    private readonly ILogger _logger;

    public CounterService(IRedisService redis, ShellSettings shellSettings, ILogger<CounterService> logger)
    {
        _redis = redis;
        _prefix = redis.InstancePrefix + shellSettings.Name + ':';
        _logger = logger;
    }

    public async Task<long> IncrementAsync(string key)
    {
        if (_redis.Database is null)
        {
            await _redis.ConnectAsync();

            if (_redis.Database is null)
            {
                return 0;
            }
        }

        try
        {
            return await _redis.Database.StringIncrementAsync(_prefix + key);
        }
        catch (Exception e)
        {
            _logger.LogError(e, "Unable to increment '{KeyName}'.", _prefix + key);

            return 0;
        }
    }
}

Only replace a default implementation with a Redis one when Redis is configured:

public sealed class Startup : StartupBase
{
    public override void ConfigureServices(IServiceCollection services)
    {
        if (services.IsRedisConfigured())
        {
            services.AddSingleton<ICounterStore, RedisCounterStore>();
        }
    }
}

Multi-Instance Deployment Checklist

For load-balanced or multi-instance Orchard Core deployments using Redis:

  1. Enable OrchardCore.Redis and configure the connection string.
  2. Enable OrchardCore.Redis.Cache so all instances share the same distributed cache.
  3. Enable OrchardCore.Redis.Bus so ISignal cache invalidation propagates across all nodes.
  4. Enable OrchardCore.Redis.Lock to coordinate background tasks and prevent duplicate work.
  5. Enable OrchardCore.Redis.DataProtection so all instances share data protection keys.
  6. Set InstancePrefix to namespace keys and prevent collisions with other applications sharing the same Redis instance.
  7. Enable Redis persistence (AOF or RDB) to prevent data loss on Redis restart.
  8. Set AllowAdmin: true in configuration so the module can verify persistence is enabled.
  9. Enable OrchardCore.SignalR.Redis when SignalR features (such as OrchardCore.Media.SignalR) must reach clients on every instance.

Upgrading from 3.x

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.