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

本文涉及的产品
Redis 开源版,标准版 2GB
推荐场景:
搭建游戏排行榜
云数据库 Tair(兼容Redis),内存型 2GB
简介:

  《Windows Azure Platform 系列文章目录

 

  本文介绍的是国内由世纪互联运维的Azure China。

  

  注意:

  截至今日2015年10月7日,国内由世纪互联运维的Azure China的Redis Cache功能还只是预览版本(Preview)。

  2016年1月4日,国内由世纪互联运维的Azure China提供的Redis Cache已经正式商用(GA)。

  因为在Global Azure (www.windowsazure.com)可以通过新的Portal(https://portal.azure.com)来创建Azure Redis Cache。

  但是这个新的Portal目前在Azure China无法使用,所以目前只能通过Azure PowerShell来创建Redis Cache服务。

  在开始本章内容之前,请读者熟悉Azure PowerShell基础知识。

  http://www.cnblogs.com/threestone/category/616633.html

 

 

  请注意:这里笔者分别用Azure Powershell 0.98和1.02版本,分别创建Redis Cache。

  请根据自己的PowerShell版本,选择不同的PowerShell命令,谢谢!

 

 

 

  注意: 以下文章的内容是通过Azure PowerShell 0.98版本来提供的。

  如何查看Azure PowerShell版本,请参考这篇文章:

  Azure PowerShell (1) PowerShell入门

 

 

  总体介绍:

  1.Azure China目前提供基本(Basic)标准(Standard)两种级别

  1.Azure China目前提供三种级别:基本(Basic),标准(Standard)和高级(Premium)

  2.Azure China目前提供C0-C6不同服务类型

 

 

  第一部分,创建Azure Redis Cache

  我们以管理员身份,运行PowerShell,执行以下命令。实现创建Standard类型,大小为13GB的Redis Cache

复制代码
#弹出界面输入用户名密码
Add-AzureAccount -Environment AzureChinaCloud

#设置当前订阅名称
Select-AzureSubscription '[SubscriptionName]' –Current

Switch-AzureMode -name AzureResourceManager

#在中国东部数据中心,创建新的资源组
New-AzureResourceGroup -name [YourResourceGroupName] -Location 'China East'

#在中国东部数据中心,申请13GB的Redis Cache,类型为Standard,有SLA保证
New-AzureRedisCache -ResourceGroupName [YourResourceGroupName] -Name [RedisCacheName] -Location 'China East' -sku 'Standard' -Size '13GB'
复制代码

  注意:有关资源组的相关概念,我会在以后的文章中做详细说明。

  

  以笔者环境为例:

  -  我们在中国东部创建Redis Cache

  -  我们创建资源组名称为LeiResourceGroup

  -  我们创建Redis Name为LeiRedisCache

  该PowerShell命令为:

复制代码
Add-AzureAccount -Environment AzureChinaCloud
#弹出界面输入用户名密码

Select-AzureSubscription 'Internal Billing' –Current
#设置当前订阅名称

Switch-AzureMode -name AzureResourceManager

New-AzureResourceGroup -name 'LeiResourceGroup' -Location 'China East'
#在中国东部数据中心,创建新的资源组

New-AzureRedisCache -ResourceGroupName 'LeiResourceGroup' -Name 'LeiRedisCache' -Location 'China East' -sku 'Standard' -Size '13GB'
#在中国东部数据中心,申请13GB的Redis Cache,类型为Standard,有SLA保证
复制代码

  执行结果如下图:

  

  注意:上图中EnableNonSslPort为False,就要求客户端强制使用SSL连接。

  上图中,PrimaryKey和SecondayKey就是访问Redis Cache的访问密钥。

 

 

  第二部分,使用Azure Redis Cache

  接下来就需要使用这个Redis Cache了,笔者的开发环境为:

  1.Visual Studio 2013 Update 4

  2.Azure SDK 2.7

  3.同时为了安全性要求,我们会使用SSL访问

 

  1.首先我们以管理员身份,运行Visual Studio

  2.创建一个Cloud Project,增加Web Role

  3.在项目文件中,选择这个Web Role,右键点击Nuget,如下图:

  

   4.下载StackExchange.Redis

  

   5.在Web Form中增加以下代码:

复制代码
 public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        
        }
      
        private static Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
        {
         //按照具体的Redis Name和Redis Key,修改以下内容
            return ConnectionMultiplexer.Connect("[YourRedisCacheName].redis.cache.chinacloudapi.cn,abortConnect=false,ssl=true,password=[YourRedisCacheKey]");
        });

        public static ConnectionMultiplexer Connection
        {
            get
            {
                return lazyConnection.Value;
            }
        }

        protected void btnSet_Click(object sender, EventArgs e)
        {
            IDatabase cache = Connection.GetDatabase();

            // Perform cache operations using the cache object...
            // Simple put of integral data types into the cache
            cache.StringSet("key1", txbSet.Text.Trim());
        }

        protected void btnGet_Click(object sender, EventArgs e)
        {
            IDatabase cache = Connection.GetDatabase();

            // Perform cache operations using the cache object...
            // Simple put of integral data types into the cache
            txbGet.Text = cache.StringGet("key1");
        }
    }
复制代码

 

  另外我们还可以通过以下代码,设置Redis Cache的过期时间:

cache.StringSet("key1", "value1", TimeSpan.FromMinutes(90));

  更多内容,请参考:https://msdn.microsoft.com/en-us/library/azure/dn690521.aspx

 

  如果读者是非.NET环境,请参考MSDN文章:https://msdn.microsoft.com/en-us/library/azure/dn690470.aspx

 

 

  第三部分,删除Azure Redis Cache

  请读者注意,一旦Redis Cache创建好以后就立即收费。

  

  如果我们不需要使用Redis Cache,请使用以下Azure PowerShell删除:

Remove-AzureRedisCache -Name [RedisCacheName] -ResourceGroupName [YourResourceGroupName]  -Force

  

 

==============================分隔符============================

  以下的内容,我们通过Azure PowerShell 1.02来配置。

  我们以管理员身份,运行PowerShell,执行以下命令。实现创建Standard类型,大小为13GB的Redis Cache

复制代码
Add-AzureRmAccount -EnvironmentName AzureChinaCloud
#弹出界面输入用户名密码

Select-AzureRmSubscription –SubscriptionName '[SubscriptionName]'| Select-AzureRmSubscription
#设置当前订阅名称

#在中国东部数据中心,创建新的资源组
New-AzureRmResourceGroup -Name '[YourResourceGroupName]' -Location 'China East'

New-AzureRmRedisCache -ResourceGroupName '[YourResourceGroupName]' -Name '[RedisCacheName]' -Location 'China East' -sku 'Standard' -Size '13GB'
#在中国东部数据中心,申请13GB的Redis Cache,类型为Standard,有SLA保证
复制代码

 

  截图如下:

 

  如果删除Azure Redis Cache,请执行以下命令:

Remove-AzureRmRedisCache -Name [RedisCacheName] -ResourceGroupName [YourResourceGroupName]  -Force

 


本文转自Lei Zhang博客园博客,原文链接:http://www.cnblogs.com/threestone/p/4858462.html,如需转载请自行联系原作者

目录
相关文章
|
5月前
|
云安全 NoSQL 安全
【Azure Redis】关于Redis的两个安全漏洞在Azure Redis是否修复问题:CVE-2024-51741 和 CVE-2024-46981
本文探讨了两个 Redis 漏洞(CVE-2024-51741 和 CVE-2024-46981)在 Azure Redis 上是否存在安全风险。CVE-2024-51741 可能因格式错误的 ACL 触发拒绝服务,而 CVE-2024-46981 或因恶意 Lua 脚本导致远程代码执行。目前 Azure Redis 使用版本 6.0,不受上述漏洞影响,且 Azure 云服务会及时修复漏洞以确保安全。文章强调 Azure 遵循严格的安全标准,为用户提供可靠保障。
216 4
|
10月前
|
监控 NoSQL 网络协议
【Azure Redis】部署在AKS中的应用,连接Redis高频率出现timeout问题
查看Redis状态,没有任何异常,服务没有更新,Service Load, CPU, Memory, Connect等指标均正常。在排除Redis端问题后,转向了AKS中。 开始调查AKS的网络状态。最终发现每次Redis客户端出现超时问题时,几乎都对应了AKS NAT Gateway的更新事件,而Redis服务端没有任何异常。因此,超时问题很可能是由于NAT Gateway更新事件导致TCP连接被重置。
166 7
|
10月前
|
缓存 NoSQL 网络协议
【Azure Redis】因为Redis升级引发了故障转移后的问题讨论
3:对于Redis的Server Load指标,每秒创建连接数的并发值,是否有建议呢? 【答】:为了避免将缓存推到 100% 服务器负载,建议将连接创建速率保持在每秒 30 个以下。
101 0
|
NoSQL 网络协议 Redis
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
208 1
【Azure Redis】AKS中使用Lettuce连接Redis Cache出现 timed out 问题的解决思路
|
存储 NoSQL Redis
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
【Azure Developer】一个复制Redis Key到另一个Redis服务的工具(redis_copy_net8)
|
Kubernetes NoSQL Redis
【Azure Redis】部署在AKS中的应用连接Redis时候出现Unable to connect to Redis server
【Azure Redis】部署在AKS中的应用连接Redis时候出现Unable to connect to Redis server
204 0
【Azure Redis】部署在AKS中的应用连接Redis时候出现Unable to connect to Redis server
|
NoSQL 网络协议 Linux
【AKS+Redis】AKS中客户端(ioredis)遇见Azure Redis服务Failover后链接中断的可能性
【AKS+Redis】AKS中客户端(ioredis)遇见Azure Redis服务Failover后链接中断的可能性
|
NoSQL Redis C++
【Azure Redis】对Azure Redis服务指标解释(Connections Created/Closed Per Second VS Connected Clinents)
【Azure Redis】对Azure Redis服务指标解释(Connections Created/Closed Per Second VS Connected Clinents)
|
NoSQL 网络协议 Linux
【Azure Redis】Lettuce客户端遇见连接Azure Redis长达15分钟的超时
【Azure Redis】Lettuce客户端遇见连接Azure Redis长达15分钟的超时
147 0