forked from cyq1162/cyqdata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MemCache.cs
199 lines (175 loc) · 6.4 KB
/
MemCache.cs
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
using System;
using System.Collections.Generic;
using System.Text;
using CYQ.Data.Cache;
using System.Web.Caching;
using CYQ.Data.Table;
using CYQ.Data.Tool;
namespace CYQ.Data.Cache
{
/// <summary>
/// ·Ö²¼Ê½»º´æÀà
/// </summary>
internal class MemCache : CacheManage
{
MemcachedClient client;
internal MemCache()
{
MemcachedClient.Setup("MyCache", AppConfig.Cache.MemCacheServers.Split(','));
client = MemcachedClient.GetInstance("MyCache");
}
public override void Add(string key, object value, double cacheMinutes)
{
client.Add(key, value, DateTime.Now.AddMinutes(cacheMinutes));
}
public override void Add(string key, object value, string fileName, double cacheMinutes)
{
client.Add(key, value, DateTime.Now.AddMinutes(cacheMinutes));
}
public override void Add(string key, object value, string fileName)
{
client.Add(key, value, DateTime.Now.AddMinutes(AppConfig.Cache.DefaultCacheTime));
}
public override void Add(string key, object value)
{
client.Add(key, value, DateTime.Now.AddMinutes(AppConfig.Cache.DefaultCacheTime));
}
public override void Add(string key, object value, string fileName, double cacheMinutes, CacheItemPriority level)
{
client.Add(key, value, DateTime.Now.AddMinutes(cacheMinutes));
}
public override Dictionary<string, CacheDependencyInfo> CacheInfo
{
get { return null; }
}
DateTime allowCacheTableTime = DateTime.Now;
private MDataTable cacheTable = null;
public override MDataTable CacheTable
{
get
{
if (cacheTable == null || DateTime.Now > allowCacheTableTime)
{
cacheTable = null;
cacheTable = new MDataTable();
Dictionary<string, Dictionary<string, string>> status = client.Stats();
if (status != null)
{
foreach (KeyValuePair<string, Dictionary<string, string>> item in status)
{
if (item.Value.Count > 0)
{
MDataTable dt = MDataTable.CreateFrom(item.Value);
if (cacheTable.Columns.Count == 0)//µÚÒ»´Î
{
cacheTable = dt;
}
else
{
cacheTable.JoinOnName = "Key";
cacheTable = cacheTable.Join(dt, "Value");
}
cacheTable.Columns["Value"].ColumnName = item.Key;
}
}
}
cacheTable.TableName = "MemCache";
allowCacheTableTime = DateTime.Now.AddMinutes(1);
}
return cacheTable;
}
}
public override void Clear()
{
client.FlushAll();
}
public override bool Contains(string key)
{
return Get(key) != null;
}
//int count = -1;
//DateTime allowGetCountTime = DateTime.Now;
public override int Count
{
get
{
int count = 0;
MDataRow row = CacheTable.FindRow("Key='curr_items'");
if (row != null)
{
for (int i = 1; i < row.Columns.Count; i++)
{
count += int.Parse(row[i].strValue);
}
}
return count;
}
}
public override object Get(string key)
{
return client.Get(key);
}
public override bool GetFileDependencyHasChanged(string key)
{
return false;
}
public override bool GetHasChanged(string key)
{
return false;
}
public override long RemainMemoryBytes
{
get { return 0; }
}
public override long RemainMemoryPercentage
{
get { return 0; }
}
public override void Remove(string key)
{
client.Delete(key);
}
public override void Set(string key, object value)
{
client.Set(key, value, DateTime.Now.AddMilliseconds(AppConfig.Cache.DefaultCacheTime));
}
public override void Set(string key, object value, double cacheMinutes)
{
client.Set(key, value, DateTime.Now.AddMinutes(cacheMinutes));
}
public override void SetChange(string key, bool isChange)
{
}
public override void Update(string key, object value)
{
client.Replace(key, value);
}
DateTime allowGetWorkInfoTime = DateTime.Now;
string workInfo = string.Empty;
public override string WorkInfo
{
get
{
if (workInfo == string.Empty || DateTime.Now > allowGetWorkInfoTime)
{
workInfo = null;
Dictionary<string, Dictionary<string, string>> status = client.Status();
if (status != null)
{
JsonHelper js = new JsonHelper(false, false);
js.Add("OKServerCount", client.okServer.ToString());
js.Add("DeadServerCount", client.errorServer.ToString());
foreach (KeyValuePair<string, Dictionary<string, string>> item in status)
{
js.Add(item.Key, JsonHelper.ToJson(item.Value));
}
js.AddBr();
workInfo = js.ToString();
}
allowGetWorkInfoTime = DateTime.Now.AddMinutes(5);
}
return workInfo;
}
}
}
}