c#Redis配置RedisConfig
2022/3/27 19:23:01
本文主要是介绍c#Redis配置RedisConfig,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
<configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration" /> <section name="RedisConfig" type="XXX.RedisConfigInfo, XXX"/><!--RedisConfigInfo的命名空间,xxx所在DLL的名字,或者所在exe全名--> </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> </configuration>
public sealed class RedisConfigInfo : ConfigurationSection { 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; } /// <summary> /// 可写的Redis链接地址 /// </summary> [ConfigurationProperty("WriteServerList", IsRequired = false)] public string WriteServerList { get { return (string)base["WriteServerList"]; } set { base["WriteServerList"] = value; } } /// <summary> /// 可读的Redis链接地址 /// </summary> [ConfigurationProperty("ReadServerList", IsRequired = false)] public string ReadServerList { get { return (string)base["ReadServerList"]; } set { base["ReadServerList"] = value; } } /// <summary> /// 最大写链接数 /// </summary> [ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)] public int MaxWritePoolSize { get { int _maxWritePoolSize = (int)base["MaxWritePoolSize"]; return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5; } set { base["MaxWritePoolSize"] = value; } } /// <summary> /// 最大读链接数 /// </summary> [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)] public int MaxReadPoolSize { get { int _maxReadPoolSize = (int)base["MaxReadPoolSize"]; return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5; } set { base["MaxReadPoolSize"] = value; } } /// <summary> /// 自动重启 /// </summary> [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)] public bool AutoStart { get { return (bool)base["AutoStart"]; } set { base["AutoStart"] = value; } } /// <summary> /// 本地缓存到期时间,单位:秒 /// </summary> [ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)] public int LocalCacheTime { get { return (int)base["LocalCacheTime"]; } set { base["LocalCacheTime"] = value; } } /// <summary> /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项 /// </summary> [ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)] public bool RecordeLog { get { return (bool)base["RecordeLog"]; } set { base["RecordeLog"] = value; } } }
public class RedisManager { /// <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(); } }
这篇关于c#Redis配置RedisConfig的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2022-03-01沐雪多租宝商城源码从.NetCore3.1升级到.Net6的步骤
- 2024-12-06使用Microsoft.Extensions.AI在.NET中生成嵌入向量
- 2024-11-18微软研究:RAG系统的四个层次提升理解与回答能力
- 2024-11-15C#中怎么从PEM格式的证书中提取公钥?-icode9专业技术文章分享
- 2024-11-14云架构设计——如何用diagrams.net绘制专业的AWS架构图?
- 2024-05-08首个适配Visual Studio平台的国产智能编程助手CodeGeeX正式上线!C#程序员必备效率神器!
- 2024-03-30C#设计模式之十六迭代器模式(Iterator Pattern)【行为型】
- 2024-03-29c# datetime tryparse
- 2024-02-21list find index c#
- 2024-01-24convert toint32 c#