ObjectPool.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. using System;
  2. using System.Collections.Generic;
  3. namespace IFramework
  4. {
  5. /// <summary>
  6. /// 基础对象池
  7. /// </summary>
  8. /// <typeparam name="T"></typeparam>
  9. public abstract class ObjectPool<T> : Unit, IDisposable, IObjectPool
  10. {
  11. /// <summary>
  12. /// 数据容器
  13. /// </summary>
  14. protected Queue<T> pool { get { return _lazy.Value; } }
  15. private Lazy<Queue<T>> _lazy = new Lazy<Queue<T>>(() => { return new Queue<T>(); }, true);
  16. /// <summary>
  17. /// 自旋锁
  18. /// </summary>
  19. protected object para = new object();
  20. /// <summary>
  21. /// 存储数据类型
  22. /// </summary>
  23. public virtual Type type { get { return typeof(T); } }
  24. /// <summary>
  25. /// 池子数量
  26. /// </summary>
  27. public int count { get { return pool.Count; } }
  28. /// <summary>
  29. /// 释放时
  30. /// </summary>
  31. protected override void OnDispose()
  32. {
  33. Clear(null);
  34. }
  35. /// <summary>
  36. /// 获取
  37. /// </summary>
  38. /// <param name="arg"></param>
  39. /// <returns></returns>
  40. public virtual T Get(IEventArgs arg = null)
  41. {
  42. lock (para)
  43. {
  44. T t;
  45. if (pool.Count > 0)
  46. {
  47. t = pool.Dequeue();
  48. }
  49. else
  50. {
  51. t = CreateNew(arg);
  52. OnCreate(t, arg);
  53. }
  54. OnGet(t, arg);
  55. return t;
  56. }
  57. }
  58. /// <summary>
  59. /// 回收
  60. /// </summary>
  61. /// <param name="obj"></param>
  62. /// <param name="args"></param>
  63. public void Set(object obj, IEventArgs args)
  64. {
  65. if (obj is T)
  66. {
  67. Set((T)obj, args);
  68. }
  69. }
  70. /// <summary>
  71. /// 回收
  72. /// </summary>
  73. /// <param name="t"></param>
  74. /// <param name="arg"></param>
  75. /// <returns></returns>
  76. public virtual bool Set(T t, IEventArgs arg = null)
  77. {
  78. lock (para)
  79. {
  80. if (!pool.Contains(t))
  81. {
  82. if (OnSet(t, arg))
  83. {
  84. pool.Enqueue(t);
  85. }
  86. return true;
  87. }
  88. else
  89. {
  90. Log.E("Set Err: Exist " + type);
  91. return false;
  92. }
  93. }
  94. }
  95. /// <summary>
  96. /// 清除
  97. /// </summary>
  98. /// <param name="arg"></param>
  99. public void Clear(IEventArgs arg = null)
  100. {
  101. lock (para)
  102. {
  103. while (pool.Count > 0)
  104. {
  105. var t = pool.Dequeue();
  106. OnClear(t, arg);
  107. IDisposable dispose = t as IDisposable;
  108. if (dispose != null)
  109. dispose.Dispose();
  110. }
  111. }
  112. }
  113. /// <summary>
  114. /// 清除
  115. /// </summary>
  116. /// <param name="count"></param>
  117. /// <param name="arg"></param>
  118. public void Clear(int count, IEventArgs arg = null)
  119. {
  120. lock (para)
  121. {
  122. count = count > pool.Count ? 0 : pool.Count - count;
  123. while (pool.Count > count)
  124. {
  125. var t = pool.Dequeue();
  126. OnClear(t, arg);
  127. }
  128. }
  129. }
  130. /// <summary>
  131. /// 创建一个新对象
  132. /// </summary>
  133. /// <param name="arg"></param>
  134. /// <returns></returns>
  135. protected abstract T CreateNew(IEventArgs arg);
  136. /// <summary>
  137. /// 数据被清除时
  138. /// </summary>
  139. /// <param name="t"></param>
  140. /// <param name="arg"></param>
  141. protected virtual void OnClear(T t, IEventArgs arg) { }
  142. /// <summary>
  143. /// 数据被回收时,返回true可以回收
  144. /// </summary>
  145. /// <param name="t"></param>
  146. /// <param name="arg"></param>
  147. /// <returns></returns>
  148. protected virtual bool OnSet(T t, IEventArgs arg)
  149. {
  150. return true;
  151. }
  152. /// <summary>
  153. /// 数据被获取时
  154. /// </summary>
  155. /// <param name="t"></param>
  156. /// <param name="arg"></param>
  157. protected virtual void OnGet(T t, IEventArgs arg) { }
  158. /// <summary>
  159. /// 数据被创建时
  160. /// </summary>
  161. /// <param name="t"></param>
  162. /// <param name="arg"></param>
  163. protected virtual void OnCreate(T t, IEventArgs arg) { }
  164. }
  165. }