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