agentleFS
Sorcha-PlatformSorcha-Platform/Sorcha/.claude/skills/redis/SKILL.md

redis

Skill2 starsChanged 10 months agoMITView on GitHub

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.

5 other projects use a version of this.

Adopted by

Most recently taken up by

The file

We split this skill into the rules it gives an agent

4 rules. 0 are common, 1 is distinctive, and 3 are very specific to this project.

  • 0

    Common

    Most of the 4 edited versions we read keep these.

  • 1

    Distinctive

    This author's own choices. Worth a look.

  • 3

    Very specific

    Tied to their project's names, files or tools. Probably skip.

  • Common
  • Distinctive
  • Very specific
  1. Distinctive:

    Add the distributed-cache overlay explicitly, sharing the same connection name:

    line 106
  2. Very specific:

    Use mcp__context7__resolve-library-id to search for "StackExchange.Redis" or "redis"

    line 134
  3. Very specific:

    Prefer website documentation (/websites/redis_io) for concepts

    line 135
  4. Very specific:

    Use /stackexchange/stackexchange.redis for .NET-specific patterns

    line 136

“Common” is measured: kept by at least 6 in 10 of the 4 edited versions of this file we could read. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.

Keep what fits and drop the rest. Your version, ready for your agents.
Files
SKILL.md

Redis Skill

Sorcha uses Redis via StackExchange.Redis for caching, token revocation tracking, rate limiting, and distributed coordination. All services share a single Redis instance managed by .NET Aspire with circuit breaker resilience.

Quick Start

Aspire Configuration

// 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);

Cache Store Usage

// 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));
    }
}

Direct IConnectionMultiplexer

// 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);
    }
}

Key Concepts

ConceptUsageExample
Key prefixNamespace isolationsorcha:, auth:
TTLAutomatic expirationTimeSpan.FromMinutes(15)
Circuit breakerGraceful degradationBreaks after 5 failures
SetsToken trackingSetAddAsync, SetMembersAsync
CountersRate limitingStringIncrementAsync

Common Patterns

Rate Limiting

When: Protecting auth endpoints from brute force.

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;
}

Token Revocation

When: Invalidating JWTs before expiration.

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);
}

IDistributedCache is a separate registration from IConnectionMultiplexer

builder.AddRedisClient("redis") wires up IConnectionMultiplexer and IDatabase — sufficient for direct StackExchange.Redis use. It does NOT register Microsoft.Extensions.Caching.Distributed.IDistributedCache. Code that depends on IDistributedCache will throw InvalidOperationException: Unable to resolve service for type 'IDistributedCache' at the first request — not at boot.

Add the distributed-cache overlay explicitly, sharing the same connection name:

builder.AddRedisClient("redis");
builder.AddRedisDistributedCache("redis");  // adds IDistributedCache → Redis

Requires the Aspire.StackExchange.Redis.DistributedCaching package alongside the base Aspire.StackExchange.Redis.

Unit-test trap that masks this: tests that construct the SUT directly with new MemoryDistributedCache(Options.Create(new MemoryDistributedCacheOptions())) bypass the DI graph entirely. The test passes; the container fails at runtime. If a service under test depends on IDistributedCache, also add a thin DI-startup test (or an integration test against a TestHost) that asserts the registration exists — not just one that mocks past it. Feature 124 PR #697 was the cautionary tale: 6 unit tests with MemoryDistributedCache green, production 500'd on every endpoint call.

See Also

  • patterns - Caching, resilience, key naming
  • workflows - Setup, testing, debugging

Related Skills

  • aspire - Redis resource configuration and service discovery
  • jwt - Token revocation integration
  • signalr - Redis backplane for scale-out
  • docker - Redis container configuration

Documentation Resources

Fetch latest Redis and StackExchange.Redis documentation with Context7.

How to use Context7:

  1. Use mcp__context7__resolve-library-id to search for "StackExchange.Redis" or "redis"
  2. Prefer website documentation (/websites/redis_io) for concepts
  3. Use /stackexchange/stackexchange.redis for .NET-specific patterns

Library IDs:

  • /stackexchange/stackexchange.redis - .NET client (344 snippets)
  • /websites/redis_io - Redis concepts (29k+ snippets)

Recommended Queries:

  • "Connection pooling multiplexer patterns best practices"
  • "Caching patterns TTL expiration strategies"
  • "Pub/Sub patterns distributed systems"

Download now (ZIP)

Does it work?

Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.

No reviews yet

People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.

  • It worked
  • Partly
  • It didn't
What did you use it for? e.g. Reviewing pull requests in a Rails app

Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.

Your trail

Files you open will collect here, so you can find them again.

Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.

Now do this for your own.

Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.