Cache your .NET application Data Using Azure Redis Cache

Azure Redis Cache is an in-memory cache based on the popular open source Redis Cache tool that allows web apps to bring data from a database into cache to improve web app performance. It gives you access to a secure, dedicated Redis cache, managed by Microsoft. A cache created using Azure Redis Cache is accessible from any application within Microsoft Azure.

Redis Cache saves data in a Key-value format. Azure Redis Cache provides secure access, low-latency, and high-performance throughput. In this article, I will explain Azure Redis Cache functionalities and how to use it in a .NET app.

Microsoft Azure Redis Cache is available in the following tiers:

  • Basic – Single node. Multiple sizes up to 53 GB.
  • Standard – Two-node Primary/Replica.
  • Premium – Two-node Primary/Replica with up to 10 shards. Multiple sizes from 6 GB to 530 GB.

Read more Microsoft Azure tutorials.

How to Create a Redis Cache Instance

To create a Redis Cache instance, log into the Azure portal and search for the term Redis Cache. Select ‘Azure Cache for Redis’ as shown below:

Azure Cache wiRedis Cache

Next, create a new Redis Cache instance by clicking the + Create button. See below:

Create Azure Cache in Redis

Add the following details, mentioned in the image below. Then click ‘Review + Create’ to add a new Redis Cache Instance:

Azure Redis Cache for web apps

Make sure to choose ’connecting to Redis cache using a public endpoint’ is checked off under the Networking tab. See below:

Azure Cache for Redis Public Endpoint

The next step is to get the Access Keys to connect with the newly created Redis Cache once the resource is successfully created in the Azure portal. These connection strings will be required to connect Redis Cache from .NET application. See the image below for more:

Azure Cache Access Keys

Access Redis Cache from .NET Client

A .NET application can use the StackExchange.Redis Cache client, which can be configured using a NuGet package that simplifies the configuration of cache client applications. Refer to the following image:

StackExchange.Redis NuGet Package

In order to programmatically connect Redis Cache, a developer needs a reference to the cache. Add the following reference to the top of any code file you want to use the StackExchange.Redis client. Refer to the following code snippet:

Using StackExchange.Redis;
 

The ConnectionMultiplexer class manages the connection to the Azure Redis Cache. To connect to an Azure Redis Cache and get a return instance of a connected ConnectionMultiplexer class, call the static Connect method and pass in the cache endpoint and key. Here is how that looks in .Net code:

ConnectionMultiplexer connection = ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
 

One key feature of ConnectionMultiplexer is that it will automatically restore connectivity to the cache once the network issue or other causes are resolved. Refer to the following code snippet:

private static Lazy lazyConnection = new Lazy(() =>
{    return ConnectionMultiplexer.Connect("contoso5.redis.cache.windows.net,abortConnect=false,ssl=true,password=...");
});
public static ConnectionMultiplexer Connection
{
get
    {
        return lazyConnection.Value;
    }
}

After the connection is established, we should return a reference to the Redis Cache database by calling the ConnectionMultiplexer.GetDatabase method. The object returned from the GetDatabase method is lightweight. Items can be stored in and retrieved from a cache by using the StringSet and StringGet methods, as shown in the following code example:

IDatabase cache = Connection.GetDatabase();
cache.StringSet("key1", "value");
cache.StringSet("key2", 25);
string key1 = cache.StringGet("key1");
int key2 = (int)cache.StringGet("key2");

.NET objects must be serialized before caching. Most .NET developers use the JsonConvert serialization methods of Newtonsoft.Json.NET and serialize to and from JSON. The following code snippet example shows a get and set using an Employee object instance:

class Employee
{
    public int Id { get; set; }
    public string Name { get; set; }
    public Employee(int EmployeeId, string Name)
    {
        this.Id = EmployeeId;
        this.Name = Name;
    }
}
cache.StringSet("e25", JsonConvert.SerializeObject(new Employee(25, "Clayton Gragg")));
Employee e25 = JsonConvert.DeserializeObject(cache.StringGet("e25"));

More by Author

Must Read