Skip to content

Fast.Cache

Public members, parameters, returns, and exceptions

Targets: net8.0;net9.0;net10.0.

Contract and constraints

Redis caching through CSRedisCore. AddCache() defaults to the RedisSettings section and registers ICache and discovered ICache<TLocator> singletons. The Action<RedisSettingsOptions> overload configures the options consumed by IOptionsMonitor directly. Named connections are configured through Services. Post-configuration defaults are host 127.0.0.1, port 6379, database 0, pool size 100 and SSL false. Provide credentials through host configuration. Get/GetAsync read values, Set/SetAsync report success, and Del/DelAsync return deletion counts. Expiry overloads accept seconds or TimeSpan. GetAndSetAsync invokes the factory on a miss but does not provide a distributed lock or exactly-once execution. Pattern deletion and key enumeration scan Redis. Network, serialization and factory failures can propagate.

Installation and examples

Install the package

bash
dotnet add package Fast.Cache

Example 1

csharp
using Fast.Cache;

public sealed class LabelReader(ICache cache)
{
    public Task<string> ReadAsync(string key) => cache.GetAsync(key);
}

Example 2

csharp
using Fast.Cache;
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);
builder.Services.AddCache();
builder.Services.AddScoped<CatalogCache>();
var app = builder.Build();
app.MapGet("/catalog/{id:int}", (int id, CatalogCache cache) => cache.ReadAsync(id));
app.Run();

public sealed record CatalogItem(int Id, string Name);

public sealed class CatalogCache(ICache cache)
{
    public Task<CatalogItem> ReadAsync(int id) => cache.GetAndSetAsync(
        $"catalog:{id}", TimeSpan.FromMinutes(5),
        () => Task.FromResult(new CatalogItem(id, "示例商品")));

    public Task<bool> ReplaceAsync(CatalogItem item) =>
        cache.SetAsync($"catalog:{item.Id}", item, TimeSpan.FromMinutes(5));

    public Task<long> InvalidateAsync(int id) => cache.DelAsync($"catalog:{id}");
}

Web-host examples belong in a consumer project using Microsoft.NET.Sdk.Web and a supported .NET target. IaaS examples can be used in an ordinary class library. Registration precedes builder.Build(); configure middleware and endpoints before app.Run(). Samples retain their original code and comments. No host, external service, database operation or client generator was run, and these snippets have not been compiled in this update.

Source reference

Delegate configuration, reload and atomic replay

The configuration callback sets the default connection directly on RedisSettingsOptions. Services holds named connections, whose ServiceName values must be nonempty and unique.

Reload constructs a replacement before atomically publishing a client/prefix snapshot. A construction failure retains the old client and logs a sanitized diagnostic. Each cache operation uses one snapshot. Client is borrowed and must not be disposed by callers. Old generations remain until cache-service disposal because external borrowers are not tracked; frequent reloads can retain multiple pools.

Fast.Cache registers the Redis atomic replay-consumption service. This does not turn ordinary GetAndSet into a distributed lock.