zoukankan      html  css  js  c++  java
  • Redis连接

    using System;
    using System.Configuration;
    using StackExchange.Redis;
    
    namespace Redis
    {
        public sealed class RedisDbContext
        {
            /// <summary>
            /// Redis连接器
            /// </summary>
            private static Lazy<ConnectionMultiplexer> _lazyConnection;
    
            /// <summary>
            /// Redis单例.
            /// </summary>
            /// <returns></returns>
            public static IDatabase Instance()
            {
                return Instance(null);
            }
    
            /// <summary>
            /// Redis单例.
            /// </summary>
            /// <param name="connectionString">Redis服务器连接地址.</param>
            /// <returns></returns>
            public static IDatabase Instance(string connectionString)
            {
                return Instance(connectionString, 0);
            }
    
            /// <summary>
            ///  Redis单例.
            /// </summary>
            /// <param name="connectionString">Redis服务器连接地址.</param>
            /// <param name="db">Redis服务器数据库索引(0-10).</param>
            /// <returns></returns>
            /// <exception cref="System.ArgumentNullException">Redis服务器单例过程失败</exception>
            public static IDatabase Instance(string connectionString, int db)
            {
                if (_lazyConnection == null)
                {
                    lock (typeof(RedisDbContext))
                    {
                        if (_lazyConnection == null)
                        {
                            if (string.IsNullOrWhiteSpace(connectionString))
                            {
                                InitRedis();
                            }
                            else
                            {
                                InitRedis(connectionString);
                            }
                        }
                    }
                }
    
                if (_lazyConnection == null)
                {
                    throw new Exception("Redis服务器连接失败");
                }
    
                return _lazyConnection.Value.GetDatabase(db);
            }
    
            /// <summary>
            /// 初始化Redis连接器.
            /// </summary>
            private static void InitRedis()
            {
                InitRedis(ConfigurationManager.AppSettings["RedisConnection"]);
            }
    
            /// <summary>
            /// 初始化Redis连接器.
            /// </summary>
            /// <param name="connectionString">Redis服务器连接地址.</param>
            /// <exception cref="System.ArgumentNullException">Redis服务器连接地址ConnectionString未配置</exception>
            private static void InitRedis(string connectionString)
            {
                if (string.IsNullOrEmpty(connectionString))
                {
                    throw new ArgumentNullException("Redis服务器地址未配置");
                }
    
                _lazyConnection = new Lazy<ConnectionMultiplexer>(
                    () => ConnectionMultiplexer.Connect(new ConfigurationOptions()
                    {
                        AbortOnConnectFail = false,
                        EndPoints = { connectionString }
                    }));
            }
        }
    }
  • 相关阅读:
    BZOJ3144 [Hnoi2013]切糕 【最小割】
    BZOJ4196 [Noi2015]软件包管理器 【树剖】
    POJ3422:Kaka's Matrix Travels——题解
    POJ2195:Going Home——题解
    POJ3068:"Shortest" pair of paths——题解
    POJ3686:The Windy's——题解
    POJ2135:Farm Tour——题解
    POJ2987:Firing——题解
    POJ3469:Dual Core CPU——题解
    POJ3281:Dining——题解
  • 原文地址:https://www.cnblogs.com/zhshlimi/p/6076558.html
Copyright © 2011-2022 走看看