Redis

Dosaic.Plugins.Caching.Redis is a Dosaic plugin that provides distributed caching backed by Redisarrow-up-right via StackExchange.Redisarrow-up-right. It registers IDistributedCache in the DI container, adds an OpenTelemetry tracing instrumentation for Redis operations, and wires up a readiness health check. For local development it can transparently fall back to an in-memory cache without any infrastructure dependency.

Installation

dotnet add package Dosaic.Plugins.Caching.Redis

Or add a package reference manually:

<PackageReference Include="Dosaic.Plugins.Caching.Redis" Version="" />

Features

  • Registers IDistributedCache backed by StackExchange.Redis with a single config property

  • In-memory fallback mode (UseInMemory: true) for local development and testing — no Redis instance required

  • Automatic abortConnect=false safety flag appended to the connection string when not already present

  • OpenTelemetry tracing instrumentation for every Redis command via OpenTelemetry.Instrumentation.StackExchangeRedis

  • Readiness health check (/health/readiness) that verifies Redis connectivity (skipped in in-memory mode)

  • Configuration-driven — no boilerplate, just add the plugin and configure the section

Configuration

The plugin binds to the redisCache section via the [Configuration("redisCache")] attribute on RedisCacheConfiguration.

RedisCacheConfiguration

Property
Type
Default
Description

UseInMemory

bool

false

When true, uses an in-memory cache instead of Redis. Useful for local development.

ConnectionString

string

(required)

StackExchange.Redis connection string (e.g. localhost:6379). Required unless UseInMemory is true.

appsettings.yml — Redis (production)

appsettings.yml — in-memory (local development)

appsettings.json equivalent

Note: When UseInMemory is false, the ConnectionString property is mandatory. The plugin will throw an ArgumentException at startup if it is missing or empty.

Note: If abortConnect=false is not present in the connection string, the plugin appends it automatically to prevent the application from crashing when Redis is temporarily unavailable at startup.

Usage

Registering the plugin

The plugin is discovered automatically by the Dosaic source generator — no manual registration is needed. Just make sure the package is referenced and the redisCache section is present in your configuration.

Injecting IDistributedCache

Storing and retrieving complex objects

IDistributedCache works with raw bytes. Serialize with System.Text.Json for structured data:

Injecting RedisCacheConfiguration

The RedisCacheConfiguration singleton is also registered in DI if you need to read the connection string elsewhere:

Health Checks

When UseInMemory is false, the plugin registers a Redis health check tagged with the readiness tag. This means the /health/readiness endpoint (provided by the Dosaic WebHost) will report Unhealthy if Redis is unreachable.

No health check is registered in in-memory mode.

OpenTelemetry

When running in Redis mode, the plugin automatically adds StackExchangeRedis tracing instrumentation to the OpenTelemetry pipeline. Every Redis command (GET, SET, DEL, etc.) will appear as a span in your distributed trace, including the command name and target key.

No additional configuration is required — instrumentation is enabled as part of ConfigureServices.

Dependencies

Package
Purpose

Microsoft.Extensions.Caching.StackExchangeRedis

IDistributedCache backed by Redis

AspNetCore.HealthChecks.Redis

Redis readiness health check

OpenTelemetry.Instrumentation.StackExchangeRedis

Distributed tracing for Redis commands

OpenTelemetry.Extensions.Hosting

OpenTelemetry hosting integration

Dosaic.Hosting.Abstractions

Dosaic plugin interfaces and attributes

Last updated