通过上一篇博客《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. 分别应用include指令和include动作标识在一个jsp页面中包含一个文件。

    分别应用include指令和include动作标识在一个jsp页面中包含一个文件. hello.jsp <%@ page language="java" import=&qu ...

  2. 多线程模式之MasterWorker模式

    多线程模式之MasterWorker模式 Master-Worker模式的核心思想是,系统由两类进程协作工作:Master进程和Worker进程.Master进程负责接收和分配任务,Worker进程负 ...

  3. yarn环境的搭建

    1.首先,在zookeeper搭建成功,服务运行的基础上搭建yarn,其次,保证时间一致 2.在 /home/install/hadoop-2.5/etc/hadoop目录下配置一下几个配置文件: 第 ...

  4. [CODEVS1258]关路灯

    题目描述 Description 多瑞卡得到了一份有趣而高薪的工作.每天早晨他必须关掉他所在村庄的街灯.所有的街灯都被设置在一条直路的同一侧. 多瑞卡每晚到早晨5点钟都在晚会上,然后他开始关灯.开始时 ...

  5. ceph-deploy 在下面ceph-nodes执行的命令

    1,Running command: env DEBIAN_FRONTEND=noninteractive apt-get -q install --assume-yes ca-certificate ...

  6. poj 1149 PIGS【最大流经典建图】

    PIGS Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18727   Accepted: 8508 Description ...

  7. PHP 判断用户是否手机访问

    $agent = check_wap(); if( $agent ) { header('Location: http://www.lewanau.com'); exit; } // check if ...

  8. weapon制作武器

    weapon制作武器 (weapon.pas/c/cpp) 解题报告 制作武器weapon.pas/c/cpp) 背景 WZland的紧急避难所很快就建好了,WZland的居民们陆续地来到这个紧急避难 ...

  9. Dom4J对XML的创建、修改、删除等操作

    Dom4j也可以很方便完成XML文档的创建.元素的修改.文档的查询遍历等,但dom4j稍比jdom复杂一点,不过在大片文档的情况下dom4j的性能要不jdom好. # 准备 首先,提供相关的jar包 ...

  10. Shell脚本编程的常识

    (这些往往是经常用到,但是各种网络上的材料都语焉不详的东西,个人认为比较有用) 七种文件类型 d            目录                                       ...