Example #1
0
        /// <summary>
        ///     <para>初始化一个新的池实例</para>
        /// </summary>
        /// <param name="itemFactory">
        ///	<para>当池中没有可以重用的项时,用其创建一个项.</para>
        /// </param>
        /// <param name="itemFilter">
        ///	<para>
        ///	当项离开或者回到池中时,会调用这个方法。
        ///	如果项可以继续重用,返回true
        ///	如果项需要被抛弃并从池中被移除,那么返回false
        ///	当项是返回池中时, phase 是 PoolItemPhase.Returning
        ///	当项是离开池中时,phase 是PoolItemPhase.Leaving,离开是指从池中移除,而不是借出去
        ///	</para>
        /// </param>
        /// <param name="config">
        ///	<para>池行为的配置</para>
        /// </param>
        /// <example>
        ///	<code>
        ///		public class MyClass
        ///		{
        ///			PoolConfig _config;
        ///			Pool&lt;MyConnection&gt; _pool;
        ///
        ///			public MyClass()
        ///			{
        ///				_config = new PoolConfig()
        ///				{
        ///					FetchOrder = PoolFetchOrder.Fifo; // 先进先出
        ///					LoanCapacity = 10; // 同一时间内,不会有超过10个连接被借出
        ///					PoolCapacity = 0; // 池内会有无限个连接可以被使用 (不过实际不会有无限个,因为在同一时间不会有超过10个连接会被借出)
        ///					MaxUses = 0; // 无限
        ///					MaxLifespan = 60 * 5 // 5分钟后过期
        ///				};
        ///				_pool = new Pool&lt;MyConnection&gt;(
        ///
        ///				//当池中没有有效的连接被使用时,该方法会被调用
        ///				() =>
        ///				{
        ///					var conn = new MyConnection();
        ///					conn.Open();
        ///					return conn;
        ///				},
        ///
        ///				//当项离开或回到池中时,该方法被调用
        ///				(connection, phase) =>
        ///				{
        ///					//返回false将告诉池,该项要被抛弃
        ///					if(!connection.IsOpen) return false;
        ///					/连接是返回池中时,清理连接
        ///					if(phase == PoolItemPhase.Returning) connection.ClearBuffer();
        ///					return true;
        ///				},
        ///
        ///				_config);
        ///			}
        ///
        ///			public void SendSomeData(byte[] data)
        ///			{
        ///				using(var conn = _pool.Borrow())
        ///				{
        ///					try
        ///					{
        ///						conn.Item.SendSomeDataTo("127.0.0.1", data);
        ///					}
        ///					catch(MyException)
        ///					{
        ///						//在出错的情况下,通知池,该连接返回到池中时,应该被抛弃
        ///						conn.IsCorrupted = true;
        ///						throw;
        ///					}
        ///				}
        ///			}
        ///		}
        ///	</code>
        /// </example>
        public Pool(
            Func <T> itemFactory,
            Func <T, PoolItemPhase, bool> itemFilter,
            Action <T> itemDestroyer,
            PoolConfig config)
        {
            if (itemFactory == null)
            {
                throw new ArgumentNullException("itemFactory");
            }
            if (config == null)
            {
                throw new ArgumentNullException("config");
            }

            _itemFactory   = itemFactory;
            _itemFilter    = itemFilter;
            _itemDestroyer = itemDestroyer;

            _loanCapacity  = config.LoanCapacity;
            _poolCapacity  = config.PoolCapacity;
            _maxRemainTime = config.MaxRemainTime;
            _maxLifespan   = config.MaxLifespan;

            _maxUses    = config.MaxUses;
            _fetchOrder = config.FetchOrder;

            _container = _createContainer(config.PoolCapacity > 0 ? config.PoolCapacity : 10);
        }
Example #2
0
 public static void RemoveContainer(IPoolContainer container)
 {
     if (container != null)
     {
         _containers.Remove(container);
     }
 }
Example #3
0
 /// <summary>
 /// Добавление нового пула
 /// </summary>
 public static void AddContainer(IPoolContainer container)
 {
     if (container != null)
     {
         _containers.Add(container);
     }
 }
Example #4
0
 /// <summary>
 /// 实现真正清理池的动作
 /// </summary>
 /// <param name="container"></param>
 private void ClearPool(IPoolContainer <ResidentItem> container)
 {
     while (container.Count > 0)
     {
         var item = container.Take();
         DiscardItem(item);
     }
 }
Example #5
0
        /// <summary>
        /// 清理池
        /// </summary>
        public void Clear()
        {
            IPoolContainer <ResidentItem> oldContainer;

            lock (_syncRoot)
            {
                if (_isDisposed)
                {
                    throw new ObjectDisposedException(this.GetType().Name);
                }

                _poolVersion++;                 //更改池版本号,因此,借出的项在归还时,都会被清理

                oldContainer = _container;
                _container   = _createContainer(_container.Count);
            }

            ClearPool(oldContainer);
        }
 public void SetContainer(IPoolContainer poolContainer)
 {
     this.poolContainer = poolContainer;
 }