I am new to redis. I have an instance of "Azure Cache for Redis" and try to use it in a .NET 8 client.
Here is my initialization:
// Hostname from Azure Cache for Redis
string redisHostname = "<myinstance>.redis.cache.windows.net::6380";
HostApplicationBuilder builder = Host.CreateApplicationBuilder(args);
builder.Services.AddStackExchangeRedisCache(options => {
options.ConfigurationOptions = new ConfigurationOptions {
EndPoints = { redisHostname }
};
options.ConnectionMultiplexerFactory = async () => {
await options.ConfigurationOptions.ConfigureForAzureWithTokenCredentialAsync(new DefaultAzureCredential()).ConfigureAwait(false);
options.InstanceName = "MyApp";
return await ConnectionMultiplexer.ConnectAsync(options.ConfigurationOptions).ConfigureAwait(false);
};
});
And here is the service that uses it:
internal class TestService(IDistributedCache cache, ILogger<TestService> logger) {
public async Task SetItem(string key, string value) {
logger.LogInformation("Set {key}: {value}", key, value);
await cache.SetStringAsync(key, value);
}
public async Task<string?> GetItem(string key) {
var value = await cache.GetStringAsync(key);
logger.LogInformation("Read {key}: {value}", key, value);
return value;
}
}
I would expect that when I use SetItem("MyKey", "MyValue") the keys stored in redis would be like "MyApp-MyKey". But that isn't the case - it's "MyKey" only.
Did I do something wrong, or does it not work in the constellation?