Redis Config

复制代码
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;
    }
    /// 
    /// 可写的Redis链接地址
    /// 
    [ConfigurationProperty("WriteServerList", IsRequired = false)]
    public string WriteServerList
    {
        get
        {
            return (string)base["WriteServerList"]; 
        }
        set
        {
            base["WriteServerList"] = value;
        }
    }

    
    /// 
    /// 可读的Redis链接地址
    /// 
    [ConfigurationProperty("ReadServerList", IsRequired = false)]
    public string ReadServerList
    {
        get
        {
            return (string)base["ReadServerList"]; 
        }
        set
        {
            base["ReadServerList"] = value;
        }
    }

    
    /// 
    /// 最大写链接数
    /// 
    [ConfigurationProperty("MaxWritePoolSize", IsRequired = false, DefaultValue = 5)]
    public int MaxWritePoolSize
    {
        get
        {
            int _maxWritePoolSize = (int)base["MaxWritePoolSize"];
            return _maxWritePoolSize > 0 ? _maxWritePoolSize : 5;
        }
        set
        {
            base["MaxWritePoolSize"] = value;
        }
    }

   
    /// 
    /// 最大读链接数
    /// 
    [ConfigurationProperty("MaxReadPoolSize", IsRequired = false, DefaultValue = 5)]
    public int MaxReadPoolSize
    {
        get
        {
            int _maxReadPoolSize = (int)base["MaxReadPoolSize"];
            return _maxReadPoolSize > 0 ? _maxReadPoolSize : 5;
        }
        set
        {
            base["MaxReadPoolSize"] = value;
        }
    }

     
    /// 
    /// 自动重启
    /// 
    [ConfigurationProperty("AutoStart", IsRequired = false, DefaultValue = true)]
    public bool AutoStart
    {
        get
        {
            return (bool)base["AutoStart"];
        }
        set
        {
            base["AutoStart"] = value;
        }
    }


    
    /// 
    /// 本地缓存到期时间,单位:秒
    /// 
    [ConfigurationProperty("LocalCacheTime", IsRequired = false, DefaultValue = 36000)]
    public int LocalCacheTime
    {
        get
        {
            return (int)base["LocalCacheTime"];
        }
        set
        {
            base["LocalCacheTime"] = value;
        }
    }

   
    /// 
    /// 是否记录日志,该设置仅用于排查redis运行时出现的问题,如redis工作正常,请关闭该项
    /// 
    [ConfigurationProperty("RecordeLog", IsRequired = false, DefaultValue = false)]
    public bool RecordeLog
    {
        get
        {
            return (bool)base["RecordeLog"];
        }
        set
        {
            base["RecordeLog"] = value;
        }
    }        

}

复制代码

复制代码
public class RedisManager
{
///
/// redis配置文件信息
///
private static RedisConfigInfo redisConfigInfo = RedisConfigInfo.GetConfig();

    private static PooledRedisClientManager prcm;

    /// 
    /// 静态构造方法,初始化链接池管理对象
    /// 
    static RedisManager()
    {
        CreateManager();
    }


    /// 
    /// 创建链接池管理对象
    /// 
    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());
    }

    /// 
    /// 客户端缓存操作对象
    /// 
    public static IRedisClient GetClient()
    {
        if (prcm == null)
            CreateManager();

        return prcm.GetClient();
    }

}

你可能感兴趣的:(C#,Redis)