Redis Cache
RCommon.RedisCache implements ICacheService using StackExchange.Redis through the Microsoft.Extensions.Caching.StackExchangeRedis provider. It is the recommended caching backend for applications deployed across multiple instances or hosts.
Installation
dotnet add package RCommon.RedisCacheConfiguration
Register Redis caching with WithDistributedCaching<RedisCachingBuilder>:
using RCommon;
using RCommon.RedisCache;
builder.Services.AddRCommon()
.WithDistributedCaching<RedisCachingBuilder>(cache =>
{
cache.Configure(options =>
{
options.Configuration = "localhost:6379";
options.InstanceName = "MyApp:";
});
});
Configure calls AddStackExchangeRedisCache on the underlying IServiceCollection, registering IDistributedCache with the Redis provider. RedisCacheService is then available as ICacheService.
Minimal setup
builder.Services.AddRCommon()
.WithDistributedCaching<RedisCachingBuilder>(cache =>
{
cache.Configure(options =>
{
options.Configuration = "localhost:6379";
});
});
Connection configuration
The RedisCacheOptions object (from Microsoft.Extensions.Caching.StackExchangeRedis) exposes the following commonly used settings:
| Option | Description |
|---|---|
Configuration | Redis connection string, e.g. "localhost:6379" or a full StackExchange.Redis connection string |
InstanceName | Optional key prefix applied to all cache keys, e.g. "MyApp:" |
ConfigurationOptions | Full StackExchange.Redis.ConfigurationOptions for advanced scenarios (SSL, auth, retry policy) |
For production use with authentication and TLS:
cache.Configure(options =>
{
options.ConfigurationOptions = new StackExchange.Redis.ConfigurationOptions
{
EndPoints = { "my-redis.cache.windows.net:6380" },
Password = "your-access-key",
Ssl = true,
AbortOnConnectFail = false
};
options.InstanceName = "MyApp:";
});
Expression caching
Enable RCommon's internal expression caching optimization by calling CacheDynamicallyCompiledExpressions:
builder.Services.AddRCommon()
.WithDistributedCaching<RedisCachingBuilder>(cache =>
{
cache.Configure(options =>
{
options.Configuration = "localhost:6379";
});
cache.CacheDynamicallyCompiledExpressions();
});
This registers:
ICacheServiceasRedisCacheServiceCachingOptionswithCachingEnabled = trueandCacheDynamicallyCompiledExpressions = true- A
Func<ExpressionCachingStrategy, ICacheService>factory for strategy-based resolution
Note: the MassTransit documentation recommends using in-memory caching for expression caching because Redis introduces network overhead. Consider using RCommon.MemoryCache for expression caching alongside Redis for application-level caching if performance is critical.
Using ICacheService
Inject ICacheService and use the get-or-create pattern. RedisCacheService serializes values to JSON using IJsonSerializer before storing them in Redis:
public class ProductCatalogService
{
private readonly ICacheService _cache;
private readonly IProductRepository _repository;
public ProductCatalogService(ICacheService cache, IProductRepository repository)
{
_cache = cache;
_repository = repository;
}
public async Task<IReadOnlyList<Product>> GetActiveProductsAsync(CancellationToken ct)
{
var key = CacheKey.With(typeof(ProductCatalogService), "active-products");
return await _cache.GetOrCreateAsync(key, () =>
{
// This runs only on a cache miss
return _repository.FindAsync(p => p.IsActive).Result;
});
}
}
How RedisCacheService works
RedisCacheService implements the get-or-create pattern against IDistributedCache:
- The key is converted to a string via
.ToString(). GetStringAsync(key)is called. If the result is non-null, the JSON string is deserialized and returned.- On a cache miss, the factory delegate is invoked, the result is serialized to JSON via
IJsonSerializer, stored withSetStringAsync, and returned.
There is no built-in expiration set at the ICacheService level; all entries are stored without a TTL by default. For expiration control, wrap IDistributedCache directly and set DistributedCacheEntryOptions.
JSON serialization dependency
RedisCacheService depends on IJsonSerializer from RCommon.Core. Ensure a JSON serialization provider is registered before configuring Redis caching:
builder.Services.AddRCommon()
.WithJsonSerialization<SystemTextJsonSerializer>() // or NewtonsoftJsonSerializer
.WithDistributedCaching<RedisCachingBuilder>(cache =>
{
cache.Configure(options =>
{
options.Configuration = "localhost:6379";
});
});
API summary
| Type | Package | Description |
|---|---|---|
RedisCachingBuilder | RCommon.RedisCache | Concrete builder for Redis-backed distributed caching |
IRedisCachingBuilder | RCommon.RedisCache | Marker interface extending IDistributedCachingBuilder |
RedisCacheService | RCommon.RedisCache | ICacheService implementation backed by IDistributedCache (Redis) with JSON serialization |
Configure(options) | RCommon.RedisCache | Extension on IRedisCachingBuilder; configures RedisCacheOptions via AddStackExchangeRedisCache |
CacheDynamicallyCompiledExpressions() | RCommon.RedisCache | Extension enabling expression caching optimization using RedisCacheService |
ICacheService | RCommon.Caching | Core abstraction for get-or-create caching |
CacheKey | RCommon.Caching | Strongly-typed cache key with factory methods |
WithDistributedCaching<T>() | RCommon.Caching | Extension method on IRCommonBuilder |