Kanject.Core.CacheDb

Redis-backed cache with consistent serialization, TTL helpers, and namespace isolation. Lambda-aware — connections are reused across cold-start and warm invocations.

Install

bash
dotnet add package Kanject.Core.CacheDb

Register

csharp
using Kanject.Core.CacheDb.Extensions;

builder.Services.AddRedisCache(options =>
{
    options.Endpoint  = appSettings.RedisEndpoint;
    options.Namespace = appSettings.CacheNamespace;     // tenant / stage isolation
    options.DefaultTtl = TimeSpan.FromMinutes(15);
});

Use it

csharp
public class ProductReadModel(ICache cache, IProductRepository repo)
{
    public Task<Product?> GetAsync(Guid id) =>
        cache.GetOrSetAsync(
            key:  $"product:{id}",
            ttl:  TimeSpan.FromMinutes(5),
            load: () => repo.FindAsync(id));

    public Task InvalidateAsync(Guid id) =>
        cache.RemoveAsync($"product:{id}");
}

GetOrSetAsync is the cache-aside pattern in one call: returns the cached value if present, runs load, stores the result, and returns it.

What ships with it

  • ICache.GetOrSetAsync(key, ttl, load) — cache-aside in one line
  • ICache.RemoveAsync(key) / pattern-based bulk invalidation
  • Namespace prefix on every key for tenant or stage isolation
  • JSON serialization with custom converters honoured
  • Connection multiplexer reuse across Lambda invocations