通过上一篇博客《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. sql server 修改表的默认值, 需要先删除约束条件

    ---------增加是否发布订单 if not exists(select 1 from syscolumns where name='iIsRelease' and id=OBJECT_ID('M ...

  2. 使用 testng.xml 参数化

    1. 创建 Java 测试类 2. 添加测试方法 TestngParameterTest(String name, String age) 3. 为测试方法添加注释 @Parameters({&quo ...

  3. linux内核申请内存函数

    kmap函数:    把某块高端内存映射到页表,然后返回给用户一个填好vitual字段的page结构    建立永久地址映射,不是简单的返回virtual字段的pageioremap:    驱动程序 ...

  4. BestCoder Round #81 (div.2) B Matrix

    B题...水题,记录当前行是由原矩阵哪行变来的. #include<cstdio> #include<cstring> #include<cstdlib> #inc ...

  5. mysql日志清理

    mysql bin-log 日志清理 发现mysql数据库目录中bin-log中日志文件非常大 [root@localhost var]# du -sh mysql-bin* | sort 1020K ...

  6. linux设备驱动----利用mdev(udev)自动创建设备文件节点

    1.mdev的使用方法和原理: mdev是busybox 自带的一个简化版的udev,适合于嵌入式的应用埸合.其具有使用简单的特点.它的作用,就是在系统启动和热插拔或动态加载驱动程序时,自动产生驱动程 ...

  7. ubuntu下使用脚本交叉编译windows下使用的ffmpeg + X264

    这里主要是补充一些遇到的问题和解决方法. 2013-06 下旬 由于项目需要,重新编译ffmpeg+264+其他. 这里使用的环境Ubuntu 13.04,脚本依然是cross_compile_ffm ...

  8. HTTP 缓存控制总结

    引言 通过网络获取内容既缓慢,成本又高:大的响应需要在客户端和服务器之间进行多次往返通信,这拖延了浏览器可以使用和处理内容的时间,同时也增加了访问者的数据成本.因此,缓存和重用以前获取的资源的能力成为 ...

  9. 教程-Delphi调用C# WEBSERVICE(二)

    第二步:将webserivce的WSDL导入到该dll工程中,如何导,方法至少有两种,我说简单的一种:  file->new->other->WebService->WSDL ...

  10. A Tour of Go If

    The if statement looks as it does in C or Java, except that the ( ) are gone and the { } are require ...