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(); } }