Microsoft.Extensions.ObjectPool
減少初始化/資源分配,提高效能。這一條與執行緒池同理,有些物件的初始化或資源分配耗時長,複用這些物件減少初始化和資源分配。比如:我有一個執行耗時約500毫秒,記憶體空間 2KB的任務為此建立一個新執行緒非同步執行,而建立執行緒耗時1秒,記憶體空間佔用1MB則得不償失。
builder.Services.TryAddSingleton<ObjectPoolProvider, DefaultObjectPoolProvider>();
IPooledObjectPolicy介面有兩個方法,
T Create()負責建立複用物件。
Return負責將複用的物件釋放回物件池中。如果不呼叫Return,表示該物件在物件池被移除。
//物件池框架介面 public interface IPooledObjectPolicy<T> where T : notnull { T Create(); bool Return(T obj); } //我的複用物件的介面實現 public class ReuseObjectPolicy : IPooledObjectPolicy<ReuseObject> { public ReuseObject Create() => new(DateTime.Now); public bool Return(ReuseObject obj) => true; }
builder.Services.TryAddSingleton(serviceProvider => { var provider = serviceProvider.GetRequiredService<ObjectPoolProvider>(); var policy = new ReuseObjectPolicy(); return provider.Create(policy); });
T Get()負責獲取複用物件。
Return負責將複用的物件釋放回物件池中。如果不呼叫Return,表示該物件在物件池被移除。
public abstract class ObjectPool<T> where T : class { public abstract T Get(); public abstract void Return(T obj); }
public class ReuseObject { private static int _counter = 0; public ReuseObject(DateTime time) { Time = time; Interlocked.Increment(ref _counter); Console.WriteLine($"{Time}被建立了{_counter}次"); } public DateTime Time { get; set; } } public class ObjectPoolController : ControllerBase { private readonly ReuseObject _reuseObject; public ObjectPoolController(ObjectPool<ReuseObject> objectPool) { _reuseObject = objectPool.Get(); } [HttpGet] public IActionResult Get() { var reuseObject = _objectPool.Get(); try { Console.WriteLine($"建立時間是:{reuseObject.Time}"); } finally { _objectPool.Return(reuseObject); } return Ok(); } }