通过上一篇博客《Redis Cache 简介》我们已经简单了解了Azure Redis Cache,这里就不过多赘述了。

1、创建Redis Cache

创建Redis Cache之前,我们首先要登录到 Azure Portal上,点击 New, Data + Storage, Redis Cache。

这里有一点需要说明一下,由于国内Azure目前不支持New Portal,因此国内的Azure订阅只能通过Powershell创建Redis Cache

#弹出界面输入用户名密码
Login-AzureRmAccount
#获取当前订阅名称
Get-AzureRmSubscription| Sort SubscriptionName|Select SubscriptionName
#选择当前订阅
Select-AzureRmSubscription -SubscriptionName {SubscriptionName}
#在日本西部创建资源组
New-AzureRmResourceGroup -Name {ResourceGroupName} -Location "Japan West"
#在日本西部创建256MB的Redis Cache,服务级别为Basic
New-AzureRmRedisCache -ResourceGroupName {ResourceGroupName} -Name {RedisCacheName} -Location "Japan West" -Sku Basic -Size C0
#查看已经创建好的Redis Cache
Get-AzureRmRedisCache

 2、使用Redis Cache

创建一个Web Application,打开NuGet控制台输入:Install-Package StackExchange.Redis或者Install-Package StackExchange.Redis.StrongName,添加对程序集的引用。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using StackExchange.Redis;
using System.Net; namespace AspNet5_Owin
{
public class RedisManager
{
private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
{
ConfigurationOptions opt = new ConfigurationOptions
{
Ssl = true,
AbortOnConnectFail = false,
Password = "{Redis Cache Keys}"
};
opt.EndPoints.Add(new DnsEndPoint("{Host Name}", {Ssl Port}));
return ConnectionMultiplexer.Connect(opt);
}); public static ConnectionMultiplexer Connection
{
get
{
return lazyConnection.Value;
}
} private static IDatabase cache = Connection.GetDatabase(); public static void CacheSet(string key, string value)
{
cache.StringSet(key, value, TimeSpan.FromSeconds());
} public static string CacheGet(string key)
{
return cache.StringGet(key);
} }
}

将字符串添加到Redis Cache中并取回

//将字符串存储到Redis Cache中
RedisManager.CacheSet("name", "zhangsan");
//将字符串从Redis Cache中取回
string name = RedisManager.CacheGet("name");

将对象添加到Redis Cache中并取回

[Serializable]
class Employee
{
public int Id { get; set; }
public string Name { get; set; } public Employee(int EmployeeId, string Name)
{
this.Id = EmployeeId;
this.Name = Name;
}
}
RedisManager.CacheSet("employee", JsonConvert.SerializeObject(new Employee(, "Clayton Gragg")))
Employee employee = JsonConvert.DeserializeObject<Employee>(RedisManager.CacheGet("employee"))

使用Azure Redis Cache的更多相关文章

  1. Azure Redis Cache

    将于 2014 年 9 月 1 日停止Azure Shared Cache服务,因此你需要在该日期前迁移到 Azure Redis Cache.Azure Redis Cache包含以下两个层级的产品 ...

  2. Azure Redis Cache (1) 入门

    <Windows Azure Platform 系列文章目录> Microsoft Azure Redis Cache基于流行的开源Redis Cache. 1.功能 Redis 是一种高 ...

  3. Azure Redis Cache (2) 创建和使用Azure Redis Cache

    <Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 注意: 截至今日2015年10月7日,国内由世纪互联运维的Azur ...

  4. Azure Redis Cache (3) 创建和使用P级别的Redis Cache

    <Windows Azure Platform 系列文章目录> 在笔者之前的文档里面已经说明了,Azure Redis Cache分为三个不同的级别: - 基本,Basic,不包含SLA ...

  5. Azure Redis Cache (4) 配置和管理Redis Cache

    <Windows Azure Platform 系列文章目录> 我们在创建完Azure Redis Cache后,经常需要切换Redis Cache的服务级别,这里我简单介绍一下使用Azu ...

  6. Azure Redis Cache作为ASP.NET 缓存输出提供程序

    前一篇文章<Azure Redis Cache作为ASP.NET Session状态提供程序 >我们已经知道如何将ASP.NET应用程序Session存储在Redis Cache中,这里我 ...

  7. Azure Redis Cache作为ASP.NET Session状态提供程序

    从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...

  8. 利用Azure Redis Cache构建百万量级缓存读写

    Redis是一个非常流行的基于内存的,低延迟,高吞吐量的key/value数据存储,被广泛用于数据库缓存,session的管理,热数据高速访问,甚至作为数据库方式提高应用程序可扩展性,吞吐量,和实施处 ...

  9. Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark

    <Windows Azure Platform 系列文章目录> 熟悉Redis环境的读者都知道,我们可以在Linux环境里,使用Redis Benchmark,测试Redis的性能. ht ...

随机推荐

  1. golang安装卸载 linux+windows+raspberryPI 平台

    参考  https://golang.org/doc/install 自ECUG2013洗脑回来,就渴望早点接触Go 听着许式伟和谢孟军的演讲 发现go的网络库的确很强大,高负载利器,语言的一些精简导 ...

  2. sublimelinter-jshinter

    --- ocalhost:~ nihao$ sudo npm install jshint -gPassword:/usr/local/bin/jshint -> /usr/local/lib/ ...

  3. POJ 1661 Help Jimmy DP

    思路:Jimmy 跳到一块板上后,可以有两种选择,向左走或向右走.走到左端和走到右端所需的时间,容易算出. n如果我们能知道,以左端为起点到达地面的最短时间,和以右端为起点到达地面的最短时间,那么向左 ...

  4. SNMP: Simple? Network Management Protocol(转)

    转自:http://www.rane.com/note161.html An SNMP Overview The Message Format The Actual Bytes Introductio ...

  5. 注册表-在IE上永久显示我的名字"www.baidu.com - 朱建强"

    HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\Main\新建字符串 名为:window title值为:"朱建强"

  6. Maintainable JavaScript(编写可维护的JavaScript) PART I Style Guidelines

    “Programs are meant to be read by humans and only incidentally( 顺便:偶然地:附带地) for computers to execute ...

  7. rcp(插件开发)The type XXX cannot be resolved. It is indirectly referenced from required .class files解决办法

    如果你在使用插件开发时遇到这个问题: The type org.eclipse.core.resources.IFile cannot be resolved. It is indirectly re ...

  8. Android Studio更新失败

    解决方案: Windows平台下 如果是运行的Android studio是32位的需要在修改一下文件: 在andriod studio的启动目录下.找到studio.exe.vmoptions这个文 ...

  9. 按需讲解之Supervisor

    Supervisor是一个进程监控程序. 满足的需求是:我现在有一个进程需要每时每刻不断的跑,但是这个进程又有可能由于各种原因有可能中断.当进程中断的时候我希望能自动重新启动它,此时,我就需要使用到了 ...

  10. ubuntu安装软件

    sudo apt-get install gnome-tweak-tool sudo apt-get install gksu 软件数据库损坏 无法安装或删除任何软件.请先使用新立得软件包管理器或在终 ...