zoukankan      html  css  js  c++  java
  • Azure技术系列之Redis篇---第一章数据缓存

      嘈杂和忙碌的生活占据占据了生活的每一天,好久没有静下心来对自己喜欢的技术进行归纳总结了。痛定思痛,今天开始开荒,把之前研究的技术进行归纳总结,先从Azure的Redis的开发技术开始。

      Azure 的Redis是一个PaaS服务,它已经对Redis的高可用和集群进行了完美的封装,我们就不需要对Redis的搭建和维护投人太多的精力,如果自己搭建,网上也是有很多的资料,这不是本篇要讲的。但是我简单描述一下Azure 的Redis 提供了三个档次的服务,即基本层、Standard层,Premium层,把Azure Redis的服务从吞吐量、延迟性最大连接数进行了服务级别的划分。生产环境强烈建议采用Standard层或Premium层。具体参数可以参考Azure的标准文档 https://docs.azure.cn/zh-cn/azure-cache-for-redis/cache-overview。

      Azure 的Redis 的单个实例默认是包含16个Redis库,我们可以通过Redis Desktop Manager 小工具进行查看,如下图。不同的业务可以存放到不同的Redis仓库里面。

      回归主题,Redis是进行云项目开发最常用的组件服务之一,使用Redis之前,需要在项目里面引用StackExchange.Redis Nuget包,然后就可以使用StackExchange.Redis组件库封装的各种方法了。Redis 主要提供以下数据类型的缓存,String、Hash、List,Set等。下面的示例代码讲对每种数据类型进行演示。

    private Lazy<ConnectionMultiplexer> lazyConnection = new Lazy<ConnectionMultiplexer>(() =>
            {
                return ConnectionMultiplexer.Connect(ConfigurationManager.ConnectionStrings["redis"].ConnectionString);
            });
            private ConnectionMultiplexer Connection => lazyConnection.Value;
            /// <summary>
            /// 缓存章节
            /// </summary>
            public void CacheSection()
            {
                IDatabase cacheDatabase = this.Connection.GetDatabase();
                //String 类型缓存,最基础结构,也是最底层结构
                cacheDatabase.StringSet("test_string", "hello vincent,this is string test.");
                Console.WriteLine("Cache StringSet : " + cacheDatabase.StringGet("test_string"));
                cacheDatabase.KeyDelete("test_string");
                Console.WriteLine("Cache StringSet : " + cacheDatabase.StringGet("test_string"));
    
                //Hash 类型缓存,用于对象的键值对的场景
                Dictionary<string, string> dictHash = new Dictionary<string, string>();
                dictHash.Add("Name", "Vincent");
                dictHash.Add("Gender", "Man");
                dictHash.Add("Age", "35");
                dictHash.Add("Address", "Tianjin");
    
                cacheDatabase.HashSet("Vincent_Hash", dictHash.Select(i => new HashEntry(i.Key, i.Value)).ToArray());
                Console.WriteLine("Cache Hash :My Address:" + cacheDatabase.HashGet("Vincent_Hash", "Address"));
    
                var lstHash = cacheDatabase.HashGetAll("Vincent_Hash");
                foreach (var item in lstHash)
                {
                    Console.WriteLine("Cache Hash :" + item.Name + ":" + item.Value);
                }
                cacheDatabase.KeyDelete("Vincent_Hash");
    
                //List 类型缓存,可以按照一定的顺序进行缓存对象管理场景
                for (int i = 1; i < 10; i++)
                {
                    cacheDatabase.ListRightPush("test_List", "ListValue_" + i.ToString("D4"));
                }
                for (int i = 1; i < 10; i++)
                {
                    Console.WriteLine("Cache List :Right_" + cacheDatabase.ListGetByIndex("test_List", i - 1));
                }
                for (int i = 1; i < 10; i++)
                {
                    Console.WriteLine("Cache List :Left_" + cacheDatabase.ListLeftPop("test_List"));
                }
                cacheDatabase.KeyDelete("test_List");
    
                //Set 类型缓存,满足不重复元素的集合场景,进行全局数据去重
                cacheDatabase.SetAdd("test_Set", "Hello Vincent");
                cacheDatabase.SetAdd("test_Set", "Hello Vincent");
                Console.WriteLine("Cache Set :" + cacheDatabase.SetPop("test_Set"));
                Console.WriteLine("Cache Set :" + cacheDatabase.SetPop("test_Set"));
            }

      

  • 相关阅读:
    Ubuntu的shell之bash和dash
    Linux下烧写工具DNW和USB驱动安装(一)
    make -C M=
    uname -r和uname -a了解
    如何添加Samba用户
    Ubuntu下配置samba实现文件夹共享
    [Jenkins]运行shell报错:寻找匹配的 `"' 是遇到了未预期的文件结束符
    [Shell] 调试shell脚本的技巧 | 校验shell脚本语法 |寻找匹配的 `"' 是遇到了未预期的文件结束符
    [Python]通过python-jenkins操作jenkins slave启动job | 通过python-jenkins实现ios自动化打包接口
    pod: command not found
  • 原文地址:https://www.cnblogs.com/luking/p/13653225.html
Copyright © 2011-2022 走看看