初始化CRICS
This commit is contained in:
30
Common/SimpleMemoryStreamPool.cs
Normal file
30
Common/SimpleMemoryStreamPool.cs
Normal file
@@ -0,0 +1,30 @@
|
||||
// 简易对象池
|
||||
using System.Collections.Concurrent;
|
||||
using System.IO;
|
||||
public class SimpleMemoryStreamPool
|
||||
{
|
||||
private static readonly ConcurrentBag<MemoryStream> _pool = new ConcurrentBag<MemoryStream>();
|
||||
private const int MaxPoolSize = 50;
|
||||
|
||||
public static MemoryStream Rent(int capacity = 1024)
|
||||
{
|
||||
MemoryStream stream;
|
||||
if (_pool.TryTake(out stream))
|
||||
{
|
||||
stream.SetLength(0);
|
||||
stream.Position = 0;
|
||||
return stream;
|
||||
}
|
||||
return new MemoryStream(capacity);
|
||||
}
|
||||
|
||||
public static void Return(MemoryStream stream)
|
||||
{
|
||||
if (stream != null && _pool.Count < MaxPoolSize)
|
||||
{
|
||||
stream.SetLength(0);
|
||||
stream.Position = 0;
|
||||
_pool.Add(stream);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user