使用Azure Redis Cache
通过上一篇博客《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的更多相关文章
- Azure Redis Cache
将于 2014 年 9 月 1 日停止Azure Shared Cache服务,因此你需要在该日期前迁移到 Azure Redis Cache.Azure Redis Cache包含以下两个层级的产品 ...
- Azure Redis Cache (1) 入门
<Windows Azure Platform 系列文章目录> Microsoft Azure Redis Cache基于流行的开源Redis Cache. 1.功能 Redis 是一种高 ...
- Azure Redis Cache (2) 创建和使用Azure Redis Cache
<Windows Azure Platform 系列文章目录> 本文介绍的是国内由世纪互联运维的Azure China. 注意: 截至今日2015年10月7日,国内由世纪互联运维的Azur ...
- Azure Redis Cache (3) 创建和使用P级别的Redis Cache
<Windows Azure Platform 系列文章目录> 在笔者之前的文档里面已经说明了,Azure Redis Cache分为三个不同的级别: - 基本,Basic,不包含SLA ...
- Azure Redis Cache (4) 配置和管理Redis Cache
<Windows Azure Platform 系列文章目录> 我们在创建完Azure Redis Cache后,经常需要切换Redis Cache的服务级别,这里我简单介绍一下使用Azu ...
- Azure Redis Cache作为ASP.NET 缓存输出提供程序
前一篇文章<Azure Redis Cache作为ASP.NET Session状态提供程序 >我们已经知道如何将ASP.NET应用程序Session存储在Redis Cache中,这里我 ...
- Azure Redis Cache作为ASP.NET Session状态提供程序
从上一篇博客<使用Azure Redis Cache>我们已经可以创建并使用Redis Cache为我们服务了. 作为Web开发者,我们都知道Session状态默认是保存在内存中的,它的优 ...
- 利用Azure Redis Cache构建百万量级缓存读写
Redis是一个非常流行的基于内存的,低延迟,高吞吐量的key/value数据存储,被广泛用于数据库缓存,session的管理,热数据高速访问,甚至作为数据库方式提高应用程序可扩展性,吞吐量,和实施处 ...
- Azure Redis Cache (3) 在Windows 环境下使用Redis Benchmark
<Windows Azure Platform 系列文章目录> 熟悉Redis环境的读者都知道,我们可以在Linux环境里,使用Redis Benchmark,测试Redis的性能. ht ...
随机推荐
- JavaScript window.location对象
JavaScript window.location对象 示例 注意 方法 经常使用window.location,它的结构总是记不住,简单梳理下,方便以后查询. 示例 URL:http://b. ...
- MEF学习小结 z
1.什么是MEF. MEF,全称是Managed Extensibility Framework.它是.NET Framework4.0的一个类库,其主要目的是为了创建可扩展的应用程序.按照官方说法就 ...
- SQL SERVER 2008查询其他数据库
1.访问本地的其他数据库 --启用Ad Hoc Distributed Queries-- reconfigure reconfigure -- 使用完成后,关闭Ad Hoc Distributed ...
- 【原】Spark中Job如何划分为Stage
版权声明:本文为原创文章,未经允许不得转载. 复习内容: Spark中Job的提交 http://www.cnblogs.com/yourarebest/p/5342404.html 1.Spark中 ...
- BestCoder Round #81 (div.1)A
水题...就是n的三进制后m位 #include<cstdio> #include<cstring> #include<cstdlib> #include<i ...
- POJ2299: Ultra-QuickSort-合并排序解决逆序数问题
#include<iostream> #include<malloc.h> using namespace std; long long ans; void merge(int ...
- autoSvn
#!/bin/bash dir="/svndata" name="puppet" user="test" passwd="t ...
- OpenSSH 高级运用两则
00×0.相关介绍 OpenSSH(OpenBSD Secure Shell)使用 SSH 通过计算机网络加密通信的实现. 它是替换由 SSH Communications Security 所提供的 ...
- URAL 1994 The Emperor's plan 求组合数 大数用log+exp处理
URAL 1994 The Emperor's plan 求组合数 大数用log #include<functional> #include<algorithm> #inclu ...
- spring + springmvc + jdbc + quartz + maven整合
个人搭建框架: pom.xml: <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="htt ...