50 lines
1.1 KiB
C#
50 lines
1.1 KiB
C#
using System.Collections.Generic;
|
|
|
|
namespace Thousandto.Core.Base
|
|
{
|
|
/// <summary>
|
|
/// 数据列表的一个缓存池,对于一些业务不用重复创建List,减少GC
|
|
/// </summary>
|
|
/// <typeparam name="T"></typeparam>
|
|
public class ListPool<T> where T:class
|
|
{
|
|
//所有回收的列表
|
|
List<List<T>> _items = new List<List<T>>();
|
|
|
|
//返回一个空列表
|
|
public List<T> New()
|
|
{
|
|
if (_items.Count > 0)
|
|
{
|
|
var ret = _items[_items.Count - 1];
|
|
_items.RemoveAt(_items.Count - 1);
|
|
return ret;
|
|
}
|
|
return new List<T>();
|
|
}
|
|
|
|
//移除列表
|
|
public void Free(List<T> obj)
|
|
{
|
|
if (obj != null)
|
|
{
|
|
obj.Clear();
|
|
_items.Add(obj);
|
|
}
|
|
}
|
|
|
|
//清理到所有缓存
|
|
public void Sweep()
|
|
{
|
|
_items.Clear();
|
|
}
|
|
|
|
//获取当前列表的数量
|
|
public int GetCount()
|
|
{
|
|
return _items.Count;
|
|
}
|
|
|
|
}
|
|
}
|