SoftCache.cs 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. using HslCommunication.Core;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace HslCommunication.BasicFramework
  7. {
  8. /****************************************************************************
  9. *
  10. * 日期: 2017年6月28日 18:34:14
  11. * 功能: 一个泛型的数组缓冲类,支持高性能存储
  12. *
  13. ***************************************************************************/
  14. /// <summary>
  15. /// 内存队列的基类
  16. /// </summary>
  17. public abstract class SoftCacheArrayBase
  18. {
  19. /// <summary>
  20. /// 字节数据流
  21. /// </summary>
  22. protected byte[] DataBytes = null;
  23. /// <summary>
  24. /// 数据的长度
  25. /// </summary>
  26. public int ArrayLength { get; protected set; }
  27. /// <summary>
  28. /// 数据数组变动时的数据锁
  29. /// </summary>
  30. protected SimpleHybirdLock HybirdLock = new SimpleHybirdLock();
  31. /// <summary>
  32. /// 用于从保存的数据对象初始化的
  33. /// </summary>
  34. /// <param name="dataSave"></param>
  35. /// <exception cref="NullReferenceException"></exception>
  36. public virtual void LoadFromBytes(byte[] dataSave)
  37. {
  38. }
  39. /// <summary>
  40. /// 获取原本的数据字节
  41. /// </summary>
  42. /// <returns></returns>
  43. public byte[] GetAllData()
  44. {
  45. byte[] result = new byte[DataBytes.Length];
  46. DataBytes.CopyTo(result, 0);
  47. return result;
  48. }
  49. }
  50. /// <summary>
  51. /// 一个内存队列缓存的类,数据类型为Int64
  52. /// </summary>
  53. public sealed class SoftCacheArrayLong : SoftCacheArrayBase
  54. {
  55. /// <summary>
  56. /// 数据的本身面貌
  57. /// </summary>
  58. private long[] DataArray = null;
  59. /// <summary>
  60. /// 实例化一个数据对象
  61. /// </summary>
  62. /// <param name="capacity"></param>
  63. /// <param name="defaultValue"></param>
  64. public SoftCacheArrayLong(int capacity, int defaultValue)
  65. {
  66. if (capacity < 10) capacity = 10;
  67. ArrayLength = capacity;
  68. DataArray = new long[capacity];
  69. DataBytes = new byte[capacity * 8];
  70. if (defaultValue != 0)
  71. {
  72. for (int i = 0; i < capacity; i++)
  73. {
  74. DataArray[i] = defaultValue;
  75. }
  76. }
  77. }
  78. /// <summary>
  79. /// 用于从保存的数据对象初始化的
  80. /// </summary>
  81. /// <param name="dataSave"></param>
  82. /// <exception cref="NullReferenceException"></exception>
  83. public override void LoadFromBytes(byte[] dataSave)
  84. {
  85. int capacity = dataSave.Length / 8;
  86. ArrayLength = capacity;
  87. DataArray = new long[capacity];
  88. DataBytes = new byte[capacity * 8];
  89. for (int i = 0; i < capacity; i++)
  90. {
  91. DataArray[i] = BitConverter.ToInt64(dataSave, i * 8);
  92. }
  93. }
  94. /// <summary>
  95. /// 线程安全的添加数据
  96. /// </summary>
  97. /// <param name="value">值</param>
  98. public void AddValue(long value)
  99. {
  100. HybirdLock.Enter();
  101. //进入混合锁模式
  102. for (int i = 0; i < ArrayLength - 1; i++)
  103. {
  104. DataArray[i] = DataArray[i + 1];
  105. }
  106. DataArray[ArrayLength - 1] = value;
  107. for (int i = 0; i < ArrayLength; i++)
  108. {
  109. BitConverter.GetBytes(DataArray[i]).CopyTo(DataBytes, 8 * i);
  110. }
  111. HybirdLock.Leave();
  112. }
  113. }
  114. /// <summary>
  115. /// 一个内存队列缓存的类,数据类型为Int32
  116. /// </summary>
  117. public sealed class SoftCacheArrayInt : SoftCacheArrayBase
  118. {
  119. /// <summary>
  120. /// 数据的本身面貌
  121. /// </summary>
  122. private int[] DataArray = null;
  123. /// <summary>
  124. /// 实例化一个数据对象
  125. /// </summary>
  126. /// <param name="capacity"></param>
  127. /// <param name="defaultValue"></param>
  128. public SoftCacheArrayInt(int capacity, int defaultValue)
  129. {
  130. if (capacity < 10) capacity = 10;
  131. ArrayLength = capacity;
  132. DataArray = new int[capacity];
  133. DataBytes = new byte[capacity * 4];
  134. if (defaultValue != 0)
  135. {
  136. for (int i = 0; i < capacity; i++)
  137. {
  138. DataArray[i] = defaultValue;
  139. }
  140. }
  141. }
  142. /// <summary>
  143. /// 用于从保存的数据对象初始化的
  144. /// </summary>
  145. /// <param name="dataSave"></param>
  146. /// <exception cref="NullReferenceException"></exception>
  147. public override void LoadFromBytes(byte[] dataSave)
  148. {
  149. int capacity = dataSave.Length / 4;
  150. ArrayLength = capacity;
  151. DataArray = new int[capacity];
  152. DataBytes = new byte[capacity * 4];
  153. for (int i = 0; i < capacity; i++)
  154. {
  155. DataArray[i] = BitConverter.ToInt32(dataSave, i * 4);
  156. }
  157. }
  158. /// <summary>
  159. /// 线程安全的添加数据
  160. /// </summary>
  161. /// <param name="value">值</param>
  162. public void AddValue(int value)
  163. {
  164. HybirdLock.Enter();
  165. //进入混合锁模式
  166. for (int i = 0; i < ArrayLength - 1; i++)
  167. {
  168. DataArray[i] = DataArray[i + 1];
  169. }
  170. DataArray[ArrayLength - 1] = value;
  171. for (int i = 0; i < ArrayLength; i++)
  172. {
  173. BitConverter.GetBytes(DataArray[i]).CopyTo(DataBytes, 4 * i);
  174. }
  175. HybirdLock.Leave();
  176. }
  177. /// <summary>
  178. /// 安全的获取数组队列
  179. /// </summary>
  180. /// <returns></returns>
  181. public int[] GetIntArray()
  182. {
  183. int[] result = null;
  184. HybirdLock.Enter();
  185. result = new int[ArrayLength];
  186. //进入混合锁模式
  187. for (int i = 0; i < ArrayLength; i++)
  188. {
  189. result[i] = DataArray[i];
  190. }
  191. HybirdLock.Leave();
  192. return result;
  193. }
  194. }
  195. }