如何连接到Azure Redis缓存

Fra*_*non 2 asp.net redis azure-caching

我正在尝试从Visual Studio Web项目连接到Azure Redis缓存(预览)的实例.

我收到以下错误:"无法加载文件或程序集'用户传递的授权令牌无效".

我已完成以下操作:1)登录Azure门户并创建新的Redis缓存预览 2)打开Visual Studio并创建新项目(Web MVC)3)管理Nuget包 - 全部更新4)安装包 - "Windows Azure缓存版本2.2.0.0"5)打开Web.Config,在dataCacheClients部分中执行以下操作:

<dataCacheClients>
<dataCacheClient name="default">
  <autoDiscover isEnabled="true" identifier="mycache.redis.cache.windows.net"  />
    <securityProperties mode="Message" sslEnabled="false">
    <messageSecurity authorizationInfo="xxxxxxxmykeyxxxxxxxx"/>
  </securityProperties>
</dataCacheClient>
</dataCacheClients>
Run Code Online (Sandbox Code Playgroud)

6)将HomeController.cs更改为以下内容:

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();
    }
}
}
Run Code Online (Sandbox Code Playgroud)

我是否需要一些特定于Redis的其他nuget包?另外,我在哪里放置Azure控制台中提到的端口号?

谢谢阅读

Mik*_*der 9

对于Azure Redis缓存,您需要使用RedEx客户端库,如StackExchange.Redis."Windows Azure缓存"客户端库特定于Azure托管缓存.

将StackExchange.Redis NuGet包添加到项目后,使用ConnectionMultiplexer对象连接到Redis:

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");
Run Code Online (Sandbox Code Playgroud)

链接更多信息:

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