Docs
.NET
StackExchange.Redis and ASP.NET Core distributed cache.
Each example reads the connection URL from VALKEY_URL; copy it from your instance page in the dashboard. See connection details and limits for what applies to every client.
On this page: StackExchange.Redis · ASP.NET Core cache
StackExchange.Redis
StackExchange.Redis takes a configuration string, not a URL. Set ssl=true and sslHost to the instance hostname. Create one ConnectionMultiplexer and reuse it.
Install
dotnet add package StackExchange.RedisProgram.cs
using StackExchange.Redis; // "inst-<id>.eu.kvnest.com:6380,user=default,password=<password>,ssl=true,sslHost=inst-<id>.eu.kvnest.com,abortConnect=false"var config = Environment.GetEnvironmentVariable("VALKEY_CONFIGURATION")!; using var valkey = await ConnectionMultiplexer.ConnectAsync(config);var db = valkey.GetDatabase(); await db.StringSetAsync("greeting", "hello", TimeSpan.FromMinutes(1));Console.WriteLine(await db.StringGetAsync("greeting"));ASP.NET Core cache
Register your instance as the IDistributedCache, which output caching and sessions can also use.
Install
dotnet add package Microsoft.Extensions.Caching.StackExchangeRedisProgram.cs
using Microsoft.Extensions.Caching.Distributed; var builder = WebApplication.CreateBuilder(args); builder.Services.AddStackExchangeRedisCache(options =>{ // Same configuration string as above, e.g. from ConnectionStrings:Valkey. options.Configuration = builder.Configuration.GetConnectionString("Valkey"); options.InstanceName = "myapp:";}); var app = builder.Build(); app.MapGet("/", async (IDistributedCache cache) =>{ var value = await cache.GetStringAsync("greeting"); if (value is null) { value = "hello"; await cache.SetStringAsync("greeting", value, new DistributedCacheEntryOptions { AbsoluteExpirationRelativeToNow = TimeSpan.FromMinutes(1), }); } return value;}); app.Run();