Loading...
Loading...
Implements Redis caching, session management, and distributed coordination for the Sorcha platform. Use when: Adding caching layers, token revocation, rate limiting, SignalR backplane, or distributed state.
npx skill4agent add stuartf303/sorcha redis// src/Apps/Sorcha.AppHost/AppHost.cs
var redis = builder.AddRedis("redis")
.WithRedisCommander();
// Reference from any service
var blueprintService = builder.AddProject<Projects.Sorcha_Blueprint_Service>()
.WithReference(redis);// Inject ICacheStore from Sorcha.Storage.Redis
public class MyService(ICacheStore cache)
{
public async Task<User?> GetUserAsync(string id)
{
return await cache.GetAsync<User>($"user:{id}");
}
public async Task SetUserAsync(User user)
{
await cache.SetAsync($"user:{user.Id}", user, TimeSpan.FromMinutes(15));
}
}// For operations beyond ICacheStore (Sets, rate limiting, etc.)
public class TokenService(IConnectionMultiplexer redis)
{
private readonly IDatabase _db = redis.GetDatabase();
public async Task TrackTokenAsync(string userId, string jti)
{
await _db.SetAddAsync($"auth:user_tokens:{userId}", jti);
}
}| Concept | Usage | Example |
|---|---|---|
| Key prefix | Namespace isolation | |
| TTL | Automatic expiration | |
| Circuit breaker | Graceful degradation | Breaks after 5 failures |
| Sets | Token tracking | |
| Counters | Rate limiting | |
public async Task<bool> IsRateLimitedAsync(string identifier)
{
var key = $"auth:failed:{identifier}";
var count = await _db.StringIncrementAsync(key);
if (count == 1)
await _db.KeyExpireAsync(key, TimeSpan.FromSeconds(60));
return count >= MaxFailedAttempts;
}public async Task RevokeTokenAsync(string jti, DateTimeOffset expiresAt)
{
var ttl = expiresAt - DateTimeOffset.UtcNow;
if (ttl > TimeSpan.Zero)
await _db.StringSetAsync($"auth:revoked:{jti}", "revoked", ttl);
}Fetch latest Redis and StackExchange.Redis documentation with Context7.
mcp__context7__resolve-library-id/websites/redis_io/stackexchange/stackexchange.redis/stackexchange/stackexchange.redis/websites/redis_io