I've recently needed to use Azure Cache for Redis on an Umbraco 9 website for user sessions to store information.
After some research, I went with the following approach:
Install Nuget Package
Install the following nuget package from Microsoft:
Install-Package Microsoft.Extensions.Caching.StackExchangeRedis
Create an "AzureCacheConfig" Model
Create a configuration model, so I can bind to my appsettings section:
public class AzureCacheConfig
{
public string ConnectionString { get; set; }
public string InstanceName {get; set; }
}
Add settings into appsettings.json
Add the following into my appsettings.json and replace the ConnectionString value with my secret one from Azure:
"AzureCache": {
"ConnectionString": "XXXX.redis.cache.windows.net:YYYY,password=ZZZZ,ssl=True,abortConnect=False",
"InstanceName": "Session_"
}
You can get your ConnectionString for "Azure Cache for Redis", within the Azure Portal.
Add Redis to Startup.cs
Then within the Startup.cs file found in the root of the web project, add the following code under ConfigureServices method above the services.AddUmbraco() line:
services.AddStackExchangeRedisCache(o =>
{
AzureCacheConfig azureCacheConfig = new();
_config.GetSection("AzureCache").Bind(azureCacheConfig);
if (!string.IsNullOrWhiteSpace(azureCacheConfig.ConnectionString))
o.Configuration = azureCacheConfig.ConnectionString;
if (!string.IsNullOrWhiteSpace(azureCacheConfig.InstanceName))
o.InstanceName = azureCacheConfig.InstanceName;
});
For the basic setup, that's it!
Restart your application and now Umbraco will be Redis for user sessions.
This only works for IDistributedCache, which I believe Umbraco doesn't use for the backoffice. However user sessions will now be scalable through Redis, which is what's needed on a scalable frontend solution.
A handy tool that I used to view my Redis cache keys to make sure it was working correct: RedisInsight-v2
If you need any help, give me a shout on twitter
Top comments (0)