zoukankan      html  css  js  c++  java
  • C# Redis实战(三)

    三、程序配置

    C# Redis实战(二)中我们安装好了Redis的系统服务,此时Redis服务已经运行。
    现在我们需要让我们的程序能正确读取到Redis服务地址等一系列的配置信息,首先,需要在Web.config文件中添加如下信息:
    <?xml version="1.0" encoding="utf-8"?>
    <!--
      有关如何配置 ASP.NET 应用程序的详细信息,请访问
      http://go.microsoft.com/fwlink/?LinkId=169433
      -->
    <configuration>
      <configSections>
        <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->
        <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />
        <section name="RedisConfig" type="RedisDemo.RedisConfigInfo, RedisDemo"/>
      </configSections>
      <RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60"
            MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false">
      </RedisConfig>
      <connectionStrings>
        <add name="DefaultConnection" providerName="System.Data.SqlClient" connectionString="Data Source=(LocalDb)\v11.0;Initial Catalog=aspnet-RedisDemo-20131125110945;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\aspnet-RedisDemo-20131125110945.mdf" />
      </connectionStrings>
    
    </configuration>
    有了以上信息还不够,还需要用C#代码来读取并且操作,获取Redis配置的程序如下:
     public static RedisConfigInfo GetConfig()
            {
                RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
                return section;
            }
    
            public static RedisConfigInfo GetConfig(string sectionName)
            {
                RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");
                if (section == null)
                    throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");
                return section;
            }
    Redis管理类代码:
     /// <summary>
            /// redis配置文件信息
            /// </summary>
            private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();
    
            private static PooledRedisClientManager prcm;
    
            /// <summary>
            /// 静态构造方法,初始化链接池管理对象
            /// </summary>
            static RedisManager()
            {
                CreateManager();
            }
    
    
            /// <summary>
            /// 创建链接池管理对象
            /// </summary>
            private static void CreateManager()
            {
                string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");
                string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");
    
                prcm = new PooledRedisClientManager(readServerList, writeServerList,
                                 new RedisClientManagerConfig
                                 {
                                     MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,
                                     MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,
                                     AutoStart = redisConfigInfo.AutoStart,
                                 });
            }
    
            private static string[] SplitString(string strSource, string split)
            {
                return strSource.Split(split.ToArray());
            }
    
            /// <summary>
            /// 客户端缓存操作对象
            /// </summary>
            public static IRedisClient GetClient()
            {
                if (prcm == null)
                    CreateManager();
    
                return prcm.GetClient();
            }
     
    如需转载,请注明出处,http://download.csdn.net/detail/qiujialongjjj/6613377
  • 相关阅读:
    VBA基础四:数据库链接(WPS2019)
    VBA基础三:循环(DO...LOOP,)
    随机多人红包
    概率抽奖
    七步轻松实现大数据库表的数据转储
    SQL Server中的行列倒置技巧
    把对应表的字段跨表赋值
    sql内日期格式化输出
    事务的用法
    女孩,你为什么不沉住气奋斗
  • 原文地址:https://www.cnblogs.com/lzjsky/p/15769903.html
Copyright © 2011-2022 走看看