using System.Collections.Concurrent;
using System.Text;
namespace YLErp.Commons
{
///
/// StringBuilder对象池
///
public static class StringBuilderPool
{
static readonly ConcurrentQueue _Pool;
static StringBuilderPool()
{
_Pool = new ConcurrentQueue();
}
static int _Max = 100;
///
/// 对象池最大数量
///
public static int MaxPoolCount
{
get { return _Max; }
set { _Max = value < 10 ? 10 : value; }
}
///
/// 请求一个StringBuilder对象
///
public static StringBuilder Acquire()
{
return _Pool.TryDequeue(out var sb) ? sb : new StringBuilder();
}
///
/// 释放StringBuilder对象到对象池中并返回StringBuilder中的值
///
///
///
public static string Release(this StringBuilder sb)
{
var str = sb.ToString();
if (_Pool.Count < _Max && sb.Length < 300)
{
sb.Clear();
_Pool.Enqueue(sb);
}
return str;
}
}
}