RetailSolutions.Shared.Cache
1.3.1400
dotnet add package RetailSolutions.Shared.Cache --version 1.3.1400
NuGet\Install-Package RetailSolutions.Shared.Cache -Version 1.3.1400
<PackageReference Include="RetailSolutions.Shared.Cache" Version="1.3.1400" />
<PackageVersion Include="RetailSolutions.Shared.Cache" Version="1.3.1400" />
<PackageReference Include="RetailSolutions.Shared.Cache" />
paket add RetailSolutions.Shared.Cache --version 1.3.1400
#r "nuget: RetailSolutions.Shared.Cache, 1.3.1400"
#:package RetailSolutions.Shared.Cache@1.3.1400
#addin nuget:?package=RetailSolutions.Shared.Cache&version=1.3.1400
#tool nuget:?package=RetailSolutions.Shared.Cache&version=1.3.1400
RetailSolutions.Shared.Cache
Unified caching abstraction for .NET 10 applications with support for Redis and In-Memory backends, automatic environment detection, and distributed locks via RedLock.net.
Installation
dotnet add package RetailSolutions.Shared.Cache
Or via Package Manager Console:
Install-Package RetailSolutions.Shared.Cache
Requirements
- .NET 10.0+
- Redis instance (only required when using Redis backend)
Quick Start
Register the cache service in your application startup:
var builder = WebApplication.CreateBuilder(args);
var cacheSettings = builder.Configuration.GetSection("Cache").Get<CacheSettings>()
?? new CacheSettings();
builder.Services.AddCache(cacheSettings);
var app = builder.Build();
app.UseCache(); // validates Redis connectivity before startup
app.Run();
Inject ICacheService wherever caching is needed:
public class ProductService(ICacheService cache)
{
public async Task<Product> GetProductAsync(string id) =>
await cache.GetOrSetAsync(
$"product:{id}",
() => repository.GetByIdAsync(id),
TimeSpan.FromMinutes(30)
);
}
Configuration
appsettings.json
{
"Cache": {
"CacheType": "Auto",
"Redis": {
"ConnectionString": "localhost:6379",
"InstanceName": "myapp"
},
"Memory": {
"SizeLimit": 10240,
"DefaultExpiration": "00:30:00"
},
"Expirations": [
{ "Key": "product:*", "Expiration": "01:00:00" },
{ "Key": "session:*", "Expiration": "00:15:00" }
]
}
}
CacheType |
Behavior |
|---|---|
Auto (default) |
Detects environment automatically |
Redis |
Forces Redis backend |
Memory |
Forces In-Memory backend |
Environment Detection (Auto mode)
| Environment | Selected Backend |
|---|---|
| Kubernetes + Redis available | Redis |
| IIS | Memory Cache |
| Other | Memory Cache |
If Redis is configured but unreachable, the library automatically falls back to Memory Cache.
ICacheService API
| Method | Description |
|---|---|
GetAsync<T>(key) |
Retrieve a cached value |
SetAsync<T>(key, value, expiration?) |
Store a value with optional TTL |
RemoveAsync(key) |
Remove a specific cache entry |
ExistsAsync(key) |
Check if a key exists |
GetOrSetAsync<T>(key, factory, expiration?) |
Get or compute and cache |
RemoveByPatternAsync(pattern) |
Bulk-remove entries matching a regex pattern |
Startup Validation
UseCache() performs a write/read/delete health check against Redis before the application starts. It throws if the check fails, preventing startup with a broken cache connection. Ignored when using Memory Cache.
app.UseCache(); // throws on Redis connectivity failure
Distributed Locks
Register RedLock.net support separately:
builder.AddRedisLock(
builder.Configuration.GetConnectionString("Redis"),
"my-application-name"
);
Use IDistributedLockFactory to acquire distributed locks:
public class OrderService(IDistributedLockFactory lockFactory)
{
public async Task ProcessOrderAsync(string orderId)
{
await using var redLock = await lockFactory.CreateLockAsync(
$"order:{orderId}",
expiryTime: TimeSpan.FromSeconds(30)
);
if (!redLock.IsAcquired)
throw new InvalidOperationException("Order is already being processed.");
await ProcessAsync(orderId);
}
}
Dependencies
| Package | Version |
|---|---|
| Microsoft.Extensions.Configuration | 10.0.1 |
| Microsoft.Extensions.DependencyInjection | 10.0.1 |
| Microsoft.Extensions.Hosting | 10.0.1 |
| Microsoft.Extensions.Caching.Memory | 10.0.1 |
| StackExchange.Redis | 2.9.25 |
| RedLock.net | 2.3.2 |
| Serilog | 4.1.0 |
License
Copyright © Retail Solutions
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- Microsoft.Extensions.Caching.Memory (>= 10.0.1)
- Microsoft.Extensions.Configuration (>= 10.0.1)
- Microsoft.Extensions.DependencyInjection (>= 10.0.1)
- Microsoft.Extensions.Hosting (>= 10.0.1)
- RedLock.net (>= 2.3.2)
- Serilog (>= 4.1.0)
- StackExchange.Redis (>= 2.9.25)
NuGet packages (3)
Showing the top 3 NuGet packages that depend on RetailSolutions.Shared.Cache:
| Package | Downloads |
|---|---|
|
RetailSolutions.Shared.Jobs
Controle de execução de cronjobs |
|
|
RetailSolutions.Shared.Max
Abstrações da Max Sistemas |
|
|
RetailSolutions.Shared.Ifood
Abstrações do Ifood |
GitHub repositories
This package is not used by any popular GitHub repositories.