ConcurrentLimitedQueue.cs 764 B

1234567891011121314151617181920212223242526272829303132333435
  1. using System.Collections.Concurrent;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. namespace Masuit.Tools.NoSQL.MongoDBClient
  5. {
  6. /// <summary>
  7. /// 定长队列
  8. /// </summary>
  9. /// <typeparam name="T"></typeparam>
  10. class ConcurrentLimitedQueue<T> : ConcurrentQueue<T>
  11. {
  12. public int Limit { get; set; }
  13. public ConcurrentLimitedQueue(int limit)
  14. {
  15. Limit = limit;
  16. }
  17. public ConcurrentLimitedQueue(IEnumerable<T> list) : base(list)
  18. {
  19. Limit = list.Count();
  20. }
  21. public new void Enqueue(T item)
  22. {
  23. if (Count >= Limit)
  24. {
  25. TryDequeue(out var _);
  26. }
  27. base.Enqueue(item);
  28. }
  29. }
  30. }