public ConnectionMultiplexerPool(string connectionString, int poolsize = 50)
 {
     _connectionString = connectionString;
     _poolsize         = poolsize;
     conn = new RedisConnectionMultiplexer2 {
         Connection = ConnectionMultiplexer.Connect(_connectionString)
     };
     conn.Database = conn.Connection.GetDatabase();
 }
Пример #2
0
		public void ReleaseConnectionMultiplexer(RedisConnectionMultiplexer2 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();
			}
		}
Пример #3
0
		public RedisConnectionMultiplexer2 GetConnection() {
			RedisConnectionMultiplexer2 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 RedisConnectionMultiplexer2();
						AllConnections.Add(conn);
					}
				if (conn != null) {
					conn.Pool = this;
					conn.ThreadId = Thread.CurrentThread.ManagedThreadId;
				}
			}
			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("StackExchange.Redis.ConnectionMultiplexerPool.GetConnection 连接池获取超时(10秒)");
			}
			conn.ThreadId = Thread.CurrentThread.ManagedThreadId;
			conn.LastActive = DateTime.Now;
			Interlocked.Increment(ref conn.UseSum);
			try {
				if (conn.Database == null || !conn.Database.IsConnected("test")) {
					conn.Connection = ConnectionMultiplexer.Connect(_connectionString);
					conn.Database = conn.Connection.GetDatabase();
				}
			} catch (Exception ex) {
				throw new Exception("GetConnection/GetDatabase 出错", ex);
			}
			return conn;
		}