C# Redis实战(三)

三、程序配置

C# Redis实战(二)中我们安装好了Redis的系统服务,此时Redis服务已经运行。
现在我们需要让我们的程序能正确读取到Redis服务地址等一系列的配置信息,首先,需要在Web.config文件中添加如下信息:
[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--  
  3.   有关如何配置 ASP.NET 应用程序的详细信息,请访问  
  4.   http://go.microsoft.com/fwlink/?LinkId=169433  
  5.   -->  
  6. <configuration>  
  7.   <configSections>  
  8.     <!-- For more information on Entity Framework configuration, visit http://go.microsoft.com/fwlink/?LinkID=237468 -->  
  9.     <section name="entityFramework" type="System.Data.Entity.Internal.ConfigFile.EntityFrameworkSection, EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" requirePermission="false" />  
  10.     <section name="RedisConfig" type="RedisDemo.RedisConfigInfo, RedisDemo"/>  
  11.   </configSections>  
  12.   <RedisConfig WriteServerList="127.0.0.1:6379" ReadServerList="127.0.0.1:6379" MaxWritePoolSize="60"  
  13.         MaxReadPoolSize="60" AutoStart="true" LocalCacheTime="180" RecordeLog="false">  
  14.   </RedisConfig>  
  15.   <connectionStrings>  
  16.     <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" />  
  17.   </connectionStrings>  
  18.   
  19. </configuration>  
有了以上信息还不够,还需要用C#代码来读取并且操作,获取Redis配置的程序如下:
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. public static RedisConfigInfo GetConfig()  
  2.        {  
  3.            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");  
  4.            return section;  
  5.        }  
  6.   
  7.        public static RedisConfigInfo GetConfig(string sectionName)  
  8.        {  
  9.            RedisConfigInfo section = (RedisConfigInfo)ConfigurationManager.GetSection("RedisConfig");  
  10.            if (section == null)  
  11.                throw new ConfigurationErrorsException("Section " + sectionName + " is not found.");  
  12.            return section;  
  13.        }  
Redis管理类代码:
[csharp]  view plain copy 在CODE上查看代码片 派生到我的代码片
 
  1. /// <summary>  
  2.        /// redis配置文件信息  
  3.        /// </summary>  
  4.        private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();  
  5.   
  6.        private static PooledRedisClientManager prcm;  
  7.   
  8.        /// <summary>  
  9.        /// 静态构造方法,初始化链接池管理对象  
  10.        /// </summary>  
  11.        static RedisManager()  
  12.        {  
  13.            CreateManager();  
  14.        }  
  15.   
  16.   
  17.        /// <summary>  
  18.        /// 创建链接池管理对象  
  19.        /// </summary>  
  20.        private static void CreateManager()  
  21.        {  
  22.            string[] writeServerList = SplitString(redisConfigInfo.WriteServerList, ",");  
  23.            string[] readServerList = SplitString(redisConfigInfo.ReadServerList, ",");  
  24.   
  25.            prcm = new PooledRedisClientManager(readServerList, writeServerList,  
  26.                             new RedisClientManagerConfig  
  27.                             {  
  28.                                 MaxWritePoolSize = redisConfigInfo.MaxWritePoolSize,  
  29.                                 MaxReadPoolSize = redisConfigInfo.MaxReadPoolSize,  
  30.                                 AutoStart = redisConfigInfo.AutoStart,  
  31.                             });  
  32.        }  
  33.   
  34.        private static string[] SplitString(string strSource, string split)  
  35.        {  
  36.            return strSource.Split(split.ToArray());  
  37.        }  
  38.   
  39.        /// <summary>  
  40.        /// 客户端缓存操作对象  
  41.        /// </summary>  
  42.        public static IRedisClient GetClient()  
  43.        {  
  44.            if (prcm == null)  
  45.                CreateManager();  
  46.   
  47.            return prcm.GetClient();  
  48.        }  
 
如需转载,请注明出处, 本系列博文示例程序下载地址

转载于:https://www.cnblogs.com/Drama/p/4159364.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值