DEV Community

Cover image for Umbraco 10 - Azure Cache for Redis
Gareth Wright
Gareth Wright

Posted on • Updated on

Umbraco 10 - Azure Cache for Redis

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
Enter fullscreen mode Exit fullscreen mode

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; }
Enter fullscreen mode Exit fullscreen mode

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": ",password=ZZZZ,ssl=True,abortConnect=False",
    "InstanceName": "Session_"
Enter fullscreen mode Exit fullscreen mode

You can get your ConnectionString for "Azure Cache for Redis", within the Azure Portal.

Azure Cache For Redis Settings

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();

    if (!string.IsNullOrWhiteSpace(azureCacheConfig.ConnectionString))
        o.Configuration = azureCacheConfig.ConnectionString;

    if (!string.IsNullOrWhiteSpace(azureCacheConfig.InstanceName))
        o.InstanceName = azureCacheConfig.InstanceName;
Enter fullscreen mode Exit fullscreen mode

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)