Esempio n. 1
0
        public void ReleaseConnection(RedisConnection2 conn)
        {
            lock (_lock)
                FreeConnections.Enqueue(conn);

            bool isAsync = false;

            if (GetConnectionAsyncQueue.Count > 0)
            {
                TaskCompletionSource <RedisConnection2> tcs = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionAsyncQueue.Count > 0)
                    {
                        tcs = GetConnectionAsyncQueue.Dequeue();
                    }
                if (isAsync = (tcs != null))
                {
                    tcs.SetResult(GetConnectionAsync().Result);
                }
            }
            if (isAsync == false && GetConnectionQueue.Count > 0)
            {
                ManualResetEventSlim wait = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionQueue.Count > 0)
                    {
                        wait = GetConnectionQueue.Dequeue();
                    }
                if (wait != null)
                {
                    wait.Set();
                }
            }
        }
Esempio n. 2
0
        private RedisConnection2 GetFreeConnection()
        {
            RedisConnection2 conn = null;

            if (FreeConnections.Count > 0)
            {
                lock (_lock)
                    if (FreeConnections.Count > 0)
                    {
                        conn = FreeConnections.Dequeue();
                    }
            }
            if (conn == null && AllConnections.Count < _poolsize)
            {
                lock (_lock)
                    if (AllConnections.Count < _poolsize)
                    {
                        conn = new RedisConnection2();
                        AllConnections.Add(conn);
                    }
                if (conn != null)
                {
                    conn.Pool = this;
                    var ips = Dns.GetHostAddresses(_ip);
                    if (ips.Length == 0)
                    {
                        throw new Exception($"无法解析“{_ip}”");
                    }
                    conn.Client            = new RedisClient(new IPEndPoint(ips[0], _port));
                    conn.Client.Connected += Connected;
                }
            }
            return(conn);
        }
Esempio n. 3
0
        private RedisConnection2 GetFreeConnection()
        {
            RedisConnection2 conn = null;

            if (FreeConnections.Count > 0)
            {
                lock (_lock)
                    if (FreeConnections.Count > 0)
                    {
                        conn = FreeConnections.Dequeue();
                    }
            }
            if (conn == null && AllConnections.Count < _poolsize)
            {
                lock (_lock)
                    if (AllConnections.Count < _poolsize)
                    {
                        conn = new RedisConnection2();
                        AllConnections.Add(conn);
                    }
                if (conn != null)
                {
                    conn.Pool = this;
                    ResetConnection(conn);
                }
            }
            return(conn);
        }
Esempio n. 4
0
    {/// <summary>
     /// kason 创建Redis 连接
     /// </summary>
     /// <param name="ex_ip"></param>
     /// <param name="ex_port"></param>
     /// <param name="ex_password"></param>
     /// <param name="ex_defaultdatabase"></param>
     /// <param name="ex_writebuffer"></param>
     /// <param name="ex_poolsize"></param>
     /// <param name="ex_ssl"></param>
     /// <param name="ex_Prefix"></param>
        public void ConnectionStringEx(string ex_ip           = "127.0.0.1", int ex_port = 6379, string ex_password = "",
                                       int ex_defaultdatabase = 0, int ex_writebuffer    = 10240,
                                       int ex_poolsize        = 20, bool ex_ssl = false, string ex_Prefix = "")
        {
            _ip          = ex_ip;
            _port        = ex_port;
            _password    = ex_password;
            _database    = ex_defaultdatabase;
            _writebuffer = ex_writebuffer;
            _poolsize    = ex_poolsize;
            _ssl         = ex_ssl;
            Prefix       = ex_Prefix;

            if (_poolsize <= 0)
            {
                _poolsize = 50;
            }
            var initConns = new RedisConnection2[_poolsize];

            for (var a = 0; a < _poolsize; a++)
            {
                initConns[a] = GetFreeConnection();
            }
            foreach (var conn in initConns)
            {
                ReleaseConnection(conn);
            }
        }
Esempio n. 5
0
        public void ReleaseConnection(RedisConnection2 conn, bool isReset = false)
        {
            if (isReset)
            {
                ResetConnection(conn);
            }

            //Console.WriteLine($"ReleaseConnection: {FreeConnections.Count}/{AllConnections.Count}, {GetConnectionQueue.Count}|{GetConnectionAsyncQueue.Count}");

            bool isAsync = false;

            if (GetConnectionAsyncQueue.Count > 0)
            {
                TaskCompletionSource <RedisConnection2> tcs = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionAsyncQueue.Count > 0)
                    {
                        tcs = GetConnectionAsyncQueue.Dequeue();
                    }
                if (isAsync = (tcs != null))
                {
                    tcs.SetResult(conn);
                }
            }
            if (isAsync == false)
            {
                lock (_lock)
                    FreeConnections.Enqueue(conn);

                while (GetConnectionQueue.Count > 0)
                {
                    GetConnectionQueueItem queueItem = null;
                    lock (_lock_GetConnectionQueue)
                        if (GetConnectionQueue.Count > 0)
                        {
                            queueItem = GetConnectionQueue.Dequeue();
                        }
                    if (queueItem != null && queueItem.IsTimeout == false)
                    {
                        queueItem.Wait.Set();
                        queueItem.Dispose();
                        break;
                    }
                    if (queueItem != null)
                    {
                        queueItem.Dispose();
                    }
                }
            }
        }
Esempio n. 6
0
        public RedisConnection2 GetConnection()
        {
            RedisConnection2 conn = null;

            if (FreeConnections.Count > 0)
            {
                lock (_lock)
                    if (FreeConnections.Count > 0)
                    {
                        conn = FreeConnections.Dequeue();
                    }
            }
            if (conn == null && AllConnections.Count < _poolsize)
            {
                lock (_lock)
                    if (AllConnections.Count < _poolsize)
                    {
                        conn = new RedisConnection2();
                        AllConnections.Add(conn);
                    }
                if (conn != null)
                {
                    conn.Pool              = this;
                    conn.Client            = new RedisClient(new IPEndPoint(IPAddress.Parse(_ip), _port));
                    conn.Client.Connected += Connected;
                }
            }
            if (conn == null)
            {
                ManualResetEvent wait = new ManualResetEvent(false);
                lock (_lock_GetConnectionQueue)
                    GetConnectionQueue.Enqueue(wait);
                if (wait.WaitOne(TimeSpan.FromSeconds(10)))
                {
                    return(GetConnection());
                }
                throw new Exception("CSRedis.ConnectionPool.GetConnection 连接池获取超时(10秒)");
            }
            conn.ThreadId   = Thread.CurrentThread.ManagedThreadId;
            conn.LastActive = DateTime.Now;
            Interlocked.Increment(ref conn.UseSum);
            try {
                conn.Client.Ping();
            } catch {
                conn.Client            = new RedisClient(new IPEndPoint(IPAddress.Parse(_ip), _port));
                conn.Client.Connected += Connected;
            }
            return(conn);
        }
Esempio n. 7
0
        public void ReleaseConnection(RedisConnection2 conn, bool isReset = false)
        {
            if (isReset)
            {
                try {
                    conn.Client.Quit();
                } catch { }
                var ips = Dns.GetHostAddresses(_ip);
                if (ips.Length == 0)
                {
                    throw new Exception($"无法解析“{_ip}”");
                }
                conn.Client            = new RedisClient(new IPEndPoint(ips[0], _port), _ssl, 1000, _writebuffer);
                conn.Client.Connected += Connected;
            }
            lock (_lock)
                FreeConnections.Enqueue(conn);

            bool isAsync = false;

            if (GetConnectionAsyncQueue.Count > 0)
            {
                TaskCompletionSource <RedisConnection2> tcs = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionAsyncQueue.Count > 0)
                    {
                        tcs = GetConnectionAsyncQueue.Dequeue();
                    }
                if (isAsync = (tcs != null))
                {
                    tcs.SetResult(GetConnectionAsync().Result);
                }
            }
            if (isAsync == false && GetConnectionQueue.Count > 0)
            {
                ManualResetEventSlim wait = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionQueue.Count > 0)
                    {
                        wait = GetConnectionQueue.Dequeue();
                    }
                if (wait != null)
                {
                    wait.Set();
                }
            }
        }
Esempio n. 8
0
        public void ReleaseConnection(RedisConnection2 conn)
        {
            lock (_lock)
                FreeConnections.Enqueue(conn);

            if (GetConnectionQueue.Count > 0)
            {
                ManualResetEvent wait = null;
                lock (_lock_GetConnectionQueue)
                    if (GetConnectionQueue.Count > 0)
                    {
                        wait = GetConnectionQueue.Dequeue();
                    }
                if (wait != null)
                {
                    wait.Set();
                }
            }
        }
Esempio n. 9
0
        internal void ResetConnection(RedisConnection2 conn)
        {
            if (conn.Client != null && conn.Client.IsConnected)
            {
                try { conn.Client.Quit(); } catch { }
            }
            var ips = Dns.GetHostAddresses(_ip);

            if (ips.Length == 0)
            {
                throw new Exception($"无法解析“{_ip}”");
            }
            if (conn.Client != null)
            {
                try { conn.Client.Dispose(); } catch { }
            }
            conn.Client            = new RedisClient(new IPEndPoint(ips[0], _port), _ssl, 1000, _writebuffer);
            conn.Client.Connected += Connected;
        }