Redis/Valkey
This connector simplifies accessing Redis and Valkey databases.
It supports the following .NET drivers:
- StackExchange.Redis, which provides an
IConnectionMultiplexer
- Microsoft.Extensions.Caching.StackExchangeRedis, which provides an
IDistributedCache
The remainder of this topic assumes that you are familiar with the basic concepts of Steeltoe Connectors. See Overview for more information.
Using the Redis Connector
To use this connector:
- Create a Redis/Valkey server instance or use a Redis docker container or Valkey docker container.
- Add NuGet references to your project.
- Configure your connection string in
appsettings.json
. - Initialize the Steeltoe Connector at startup.
- Use the driver-specific connection/client instance.
Add NuGet References
To use this connector, add a NuGet reference to Steeltoe.Connectors
.
Also add a NuGet reference to one of the .NET drivers listed above, as you would if you were not using Steeltoe.
Configure connection string
The available connection string parameters for Redis are described in StackExchange.Redis.
The following example appsettings.json
works with either docker container referred to earlier:
{
"Steeltoe": {
"Client": {
"Redis": {
"Default": {
"ConnectionString": "localhost"
}
}
}
}
}
Initialize Steeltoe Connector
Update your Program.cs
to initialize the Connector:
using Steeltoe.Connectors.Redis;
var builder = WebApplication.CreateBuilder(args);
builder.AddRedis();
Use IConnectionMultiplexer
To obtain an IConnectionMultiplexer
instance in your application, inject the Steeltoe factory in a controller or view:
using Microsoft.AspNetCore.Mvc;
using StackExchange.Redis;
using Steeltoe.Connectors;
using Steeltoe.Connectors.Redis;
public class HomeController : Controller
{
public async Task<IActionResult> Index(
[FromServices] ConnectorFactory<RedisOptions, IConnectionMultiplexer> connectorFactory)
{
var connector = connectorFactory.Get();
IConnectionMultiplexer client = connector.GetConnection();
IDatabase database = client.GetDatabase();
database.StringSet("myKey", DateTime.UtcNow.ToString());
ViewData["Result"] = database.StringGet("myKey");
return View();
}
}
For a complete sample app that uses IConnectionMultiplexer
, see https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/Redis.
Use IDistributedCache
To obtain an IDistributedCache
instance in your application, inject the Steeltoe factory in a controller or view:
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Steeltoe.Connectors;
using Steeltoe.Connectors.Redis;
public class HomeController : Controller
{
public async Task<IActionResult> Index(
[FromServices] ConnectorFactory<RedisOptions, IDistributedCache> connectorFactory)
{
var connector = connectorFactory.Get();
IDistributedCache client = connector.GetConnection();
await client.SetStringAsync("myKey", DateTime.UtcNow.ToString());
ViewData["Result"] = await client.GetStringAsync("myKey");
return View();
}
}
For a complete sample app that uses IDistributedCache
, see https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/Redis.
Cloud Foundry
This Connector supports the following service brokers:
- Redis for VMware Tanzu Application Service
- Tanzu for Valkey on Cloud Foundry
- VMware Tanzu Cloud Service Broker for Azure
- VMware Tanzu Cloud Service Broker for GCP
You can create and bind an instance to your application by using the Cloud Foundry CLI:
# Create Redis service
cf create-service p-redis shared-vm myRedisService
# Bind service to your app
cf bind-service myApp myRedisService
# Restage the app to pick up change
cf restage myApp
Kubernetes
This Connector supports the Service Binding Specification for Kubernetes. It can be used through the Services Toolkit.
For details on how to use this, see the instructions at https://github.com/SteeltoeOSS/Samples/tree/main/Connectors/src/Redis#running-on-tanzu-platform-for-kubernetes.