Forecast Cloudy – Using Microsoft Azure Redis Cache

 

In my previous posts I introduced Redis , in particular Microsoft port of that open source technology to Windows by MsOpenTech. In this post I want to show how you can use Azure Cache  version of Redis based on that port that went to general availability around time of Microsoft TechEd 2014 Conference in May 2014.

Creating Azure Redis Cache:

First you will login with your credentials to new Azure Portal at https://portal.azure.com. Pick Browse on home page and New button in lower left corner: Pick Redis Cache

redis_azure2

 

First enter DNS name, that would be subdomain name to use for the cache endpoint. The endpoint must be a string between six and twenty characters, contain only lowercase numbers and letters, and must start with a letter.

Use Pricing Tier to select the desired cache size and features. Azure Redis Cache is available in the following two tiers.

  • Basic – single node, multiple sizes up to 53 GB.
  • Standard – Two node Master/Slave, 99.9% SLA, multiple sizes up to 53 GB.

For Subscription, select the Azure subscription that you want to use for the cache. In Resource group, select or create a resource group for your cache.

Use Geolocation to specify the geographic location in which your cache is hosted. For the best performance, Microsoft strongly recommends that you create the cache in the same region as the cache client application.

redis_azure3

At this time I will hit create and my cache will be created.  After a bit here is what you see on a portal:

redis_azure4

 

Using Azure Redis Cache

We start by getting the connection details, this requires 2 steps:

1. Get the URI, which is easy to get using the properties window, as shown below

azure_redis12

2. Once you have copied the host name url, we need to copy the password, which you can grab from the keys area

Now that we have these two important bits that we need to connect, lets start using our cache service.

Just like in my on premise demo previously  I will use StackExchange.Redis package from NuGet in my client.  Below is very simple basic way to put\retrieve strings from Azure Redis:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis; 

namespace RedisTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect(",ssl=true,password=");
            IDatabase db = redis.GetDatabase();

            // Perform cache operations using the cache object...
            // Simple put of integral data types into the cache
            db.StringSet("GM", "GENERAL MOTORS");
            db.StringSet("F", "FORD");
            db.StringSet("MSFT", "MICROSOFT");

      
        // Retrieve keys\values from Redis
          
               string value = db.StringGet("GM");
               
                System.Console.Out.WriteLine("GM" + "," + value);
            
            System.Console.ReadLine();




        }
    }

And yet above demo is a bit boring as you usually will not store cache items like this in real time. Therefore a snippet below will use complex classes with properties and magic of JSON serialization to put these in Redis Cache:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using StackExchange.Redis;
using Newtonsoft.Json; 
namespace RedisTest
{
    class Program
    {
        static void Main(string[] args)
        {
            ConnectionMultiplexer redis = ConnectionMultiplexer.Connect("gennadykredis.redis.cache.windows.net,ssl=true,password=nsq/4GhGUKu8OidX15Eo6raWhC/Z5KefSnu5uwy1IRo=");
            IDatabase db = redis.GetDatabase();

            // Perform cache operations using the cache object...
            // Simple put of OBJECT data types into the cache
            Product myProd = new Product() { Price=127.99, Description = "Bycicle" };
            var serializedmyProd = JsonConvert.SerializeObject(myProd);
            db.StringSet("serializedmyProd", serializedmyProd);


            Product myProd2 = new Product() { Price = 4999.99, Description = "Motorcycle" };
            var serializedmyProd2 = JsonConvert.SerializeObject(myProd2);
            db.StringSet("serializedmyProd2", serializedmyProd2);

      
        // Retrieve keys\values from Redis

            var myProd3 = JsonConvert.DeserializeObject(db.StringGet("serializedmyProd"));
            var myProd4 = JsonConvert.DeserializeObject(db.StringGet("serializedmyProd2"));
          
            System.Console.Out.WriteLine(myProd3.Description.ToString() + " " + myProd4.Description.ToString()); 
            System.Console.ReadLine();




        }
    }

    class Product
    {
        public string Description  { get; set; }
        public double Price { get; set; }


       

    }

So as with previous blogs on Redis demo is pretty simple, however it shows you foundation to move deeper as necessary. For more on Redis Cache on Microsoft Azure see – http://azure.microsoft.com/en-us/documentation/services/cache/, http://msdn.microsoft.com/en-us/library/azure/dn690516.aspx, http://msdn.microsoft.com/en-us/library/azure/dn690523.aspx and https://sachabarbs.wordpress.com/2014/11/25/azure-redis-cache/ 

Hope this is helpful as it has been different and fun for myself.

Advertisements

One thought on “Forecast Cloudy – Using Microsoft Azure Redis Cache

  1. Pingback: Meet Redis in the Clouds – Azure PaaS Introduces Premium Public Preview with Cluster and Persistence | A posteriori

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s