How to connect to Azure Redis Cache

Frank Cannon picture Frank Cannon · May 11, 2014 · Viewed 8k times · Source

I am trying to connect to an instance of the Azure Redis Cache (Preview) from a Visual Studio Web Project.

I am getting the following error: "Could not load file or assembly 'Authorization token passed by user Invalid".

I have done the following: 1) Logged into Azure Portal and created a new Redis Cache PREVIEW 2) Opened Visual Studio and created New Project (Web MVC) 3) Manage Nuget Packages - Update All 4) Install Package - "Windows Azure Cache Version 2.2.0.0" 5) Open Web.Config, in dataCacheClients section to the following:

<dataCacheClients>
<dataCacheClient name="default">
  <autoDiscover isEnabled="true" identifier="mycache.redis.cache.windows.net"  />
    <securityProperties mode="Message" sslEnabled="false">
    <messageSecurity authorizationInfo="xxxxxxxmykeyxxxxxxxx"/>
  </securityProperties>
</dataCacheClient>
</dataCacheClients>

6) Changed HomeController.cs to the following:

using Microsoft.ApplicationServer.Caching;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace CacheRedisTest.Controllers
{
public class HomeController : Controller
{
    static DataCacheFactory myFactory;
    static DataCache myCache;
    public ActionResult Index()
    {
        if (myCache == null)
        {
            myFactory = new DataCacheFactory();
            myCache = myFactory.GetDefaultCache();
        }

        return View();
    }
}
}

Do I need some other nuget packages specific to Redis? Also where do I put the port number which is mentioned in the Azure console?

Thanks for reading

Answer

Mike Harder picture Mike Harder · May 13, 2014

For Azure Redis Cache, you need to use a Redis client library like StackExchange.Redis. The "Windows Azure Cache" client library is specific to the Azure Managed Cache.

After adding the StackExchange.Redis NuGet package to your project, connect to Redis using the ConnectionMultiplexer object:

var cm  = ConnectionMultiplexer.Connect("mycache.redis.cache.windows.net,ssl=true,password=<password>");
var db = connection.GetDatabase();

db.StringSet("key", "value");
var key = db.StringGet("key");

Links with more information:

http://azure.microsoft.com/en-us/documentation/articles/cache-dotnet-how-to-use-azure-redis-cache/ https://github.com/StackExchange/StackExchange.Redis