zoukankan      html  css  js  c++  java
  • Azure Redis Cache (2) 创建和使用Azure Redis Cache

      《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
  • 相关阅读:
    odoo service error 更改端口号
    unique 唯一约束
    odoo 注册失败
    odoo 某个字段权限设置
    linux 定时任务
    odoo webside theme
    js 杂记
    vue3入门总结
    vue输出空格
    css等比例缩放图片
  • 原文地址:https://www.cnblogs.com/threestone/p/4858462.html
Copyright © 2011-2022 走看看