Configuring and Using the Redis Azure Cache

In this blog post I will show you how to configure and use a cloud caching service called the Azure Redis Cache. This caching service makes it easy to store information in the cloud to save temporary data, or share data with other services using nothing more than a shared Internet address.

Creating the Azure Redis Cache Service

The first step is to create the Redis cache service itself. For this, you will need an Azure account, and login to the Azure management portal (https://portal.azure.com). To create a new Redis cache, simply browse from the available list of services (click on Browse), and find the Redis Cache service. Provide the name for your cache, which will be used as part of the Internet address of your service (the DNS name). In the example below, I am creating the cache as “bsctest”, and as a result the DNS address for my service will be “bsctest.redis.cache.windows.net”. Also note that you will need to select a pricing tier; I picked the C0 Basic pricing tier. WARNING: Make sure to click on the “Select” button at the bottom of the screen for the pricing tier change to take effect. Then click the “Create” button to create the service; this may take a few minutes.

image

You will soon need the Access Key from the sample code below; to find your access key, go back to the Redis Cache (you can either go back to Browse All, or use the shortcut that was created for you on the main dashboard). From there, click on the newly created cache, and in the Settings window, click on Access Keys. Only will only need one of those keys; click on the icon next to the access key and save it somewhere handy.

image

Using the Azure Redis Cache Service

Once the cache service has been created, using it is a breeze. All we have to do is create a .NET application, reference the Redis client libraries, and interact with the cache data.

First, let’s create a sample Console application using Visual Studio 2013 using C#.

image

The right-click on the project name itself (from Solution Explorer window), and click Manage NuGet Packages. From there, click on the Online link on the left, and type Redis in the search box.  We will use the StackExchange.Redis package; select it and click on Install. You can now close the NuGet window and return to your project.

Note that you may get an exception if the cache hasn’t been created just yet, so make sure the cache service is running before proceeding.

Let’s connect to the cache, and store a simple string for our test. First, you need to connect to the Redis Cache using the Connect method on the ConnectionMultiplexer object; then, use the StringGet and StringGet methods to write/read data.

Here is the complete code for this simple test:

using StackExchange.Redis;

static void Main(string[] args)
        {
            string name = "THIS IS YOUR CACHE NAME, WHICH IS THE FIRST PART OF THE DNS NAME OF THE CACHE";
            string key = "THIS IS YOUR SECRET KEY THAT YOUR COPIED PREVIOUSLY";

            Console.WriteLine("Connecting...");

            var connection = ConnectionMultiplexer.Connect(
                string.Format(
                "{0}.redis.cache.windows.net,connectTimeout=5000,abortConnect=false,ssl=true,password={1}", name, key));

            var db = connection.GetDatabase();
           
            Console.WriteLine("Setting a cache value");
            db.StringSet("firstname", "James");

            Console.WriteLine("Reading from the cache now...");
            var val = db.StringGet("firstname");

            Console.WriteLine("  value: " + val);

            Console.Read();
        }

When you run the above code, you should see something like this.

image

Et voila! The Redis cache offers many features; the example above is only a test, so I invite you to investigate the caching service more in depth before jumping in production with it.

About Herve Roggero

Herve Roggero, Microsoft Azure MVP, @hroggero, is the founder of Blue Syntax Consulting (http://www.bluesyntaxconsulting.com). Herve's experience includes software development, architecture, database administration and senior management with both global corporations and startup companies. Herve holds multiple certifications, including an MCDBA, MCSE, MCSD. He also holds a Master's degree in Business Administration from Indiana University. Herve is the co-author of "PRO SQL Azure" and “PRO SQL Server 2012 Practices” from Apress, a PluralSight author, and runs the Azure Florida Association.

Print | posted @ Friday, December 11, 2015 3:16 PM

Comments on this entry:

Gravatar # re: Configuring and Using the Redis Azure Cache
by Redsmin {Redis GUI} at 12/18/2015 2:35 AM

Nice article Herve! Readers may also be interested on how to configure Redis GUI Redsmin to setup Azure Redis Cache to administrate and monitor in real-time Redis http://bit.ly/1IIe31j :)
Comments have been closed on this topic.