-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathHitCountDatabase.cs
183 lines (154 loc) · 5.23 KB
/
HitCountDatabase.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
using System.Linq;
using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using MongoDB.Driver;
namespace DigitalPlatform.LibraryServer
{
/// <summary>
/// 对象计数器数据库。为每个 URL 维持一个计数值
/// </summary>
public class HitCountDatabase
{
string m_strHitCountDatabaseName = "";
IMongoCollection<HitCountItem> _hitCountCollection = null;
// 初始化
// parameters:
public int Open(
// string strMongoDbConnStr,
MongoClient client,
string strInstancePrefix,
out string strError)
{
strError = "";
#if NO
if (string.IsNullOrEmpty(strMongoDbConnStr) == true)
{
strError = "strMongoDbConnStr 参数值不应为空";
return -1;
}
#endif
if (string.IsNullOrEmpty(strInstancePrefix) == false)
strInstancePrefix = strInstancePrefix + "_";
m_strHitCountDatabaseName = strInstancePrefix + "hitcount";
#if NO
try
{
this.m_mongoClient = new MongoClient(strMongoDbConnStr);
}
catch (Exception ex)
{
strError = "初始化 MongoClient 时出错: " + ex.Message;
return -1;
}
#endif
// var server = client.GetServer();
{
// var db = server.GetDatabase(this.m_strHitCountDatabaseName);
var db = client.GetDatabase(this.m_strHitCountDatabaseName);
_hitCountCollection = db.GetCollection<HitCountItem>("hitcount");
// if (_hitCountCollection.GetIndexes().Count == 0)
if (_hitCountCollection.Indexes.List().ToList().Count == 0)
CreateIndex();
}
return 0;
}
public void CreateIndex()
{
{
var indexModel = new CreateIndexModel<HitCountItem>(
Builders<HitCountItem>.IndexKeys.Ascending(_ => _.URL),
new CreateIndexOptions() { Unique = true });
_hitCountCollection.Indexes.CreateOne(indexModel);
}
}
#if OLD
public void CreateIndex()
{
_hitCountCollection.CreateIndex(new IndexKeysBuilder().Ascending("URL"),
IndexOptions.SetUnique(true));
}
#endif
// 清除集合内的全部内容
public int Clear(out string strError)
{
strError = "";
if (_hitCountCollection == null)
{
strError = "访问计数 mongodb 集合尚未初始化";
return -1;
}
// WriteConcernResult result = _hitCountCollection.RemoveAll();
var result = _hitCountCollection.DeleteMany(Builders<HitCountItem>.Filter.Empty); // RemoveAll();
CreateIndex();
return 0;
}
public IMongoCollection<HitCountItem> HitCountCollection
{
get
{
return this._hitCountCollection;
}
}
// 增加一次访问计数
// return:
// false 没有成功。通常因为 mongodb 无法打开等原因
// true 成功
public bool IncHitCount(string strURL)
{
IMongoCollection<HitCountItem> collection = this.HitCountCollection;
if (collection == null)
return false;
var updateDef = Builders<HitCountItem>.Update.Inc(o => o.HitCount, 1);
collection.UpdateOne(
o => o.URL == strURL,
updateDef,
new UpdateOptions { IsUpsert = true });
return true;
}
#if OLD
// 增加一次访问计数
// return:
// false 没有成功。通常因为 mongodb 无法打开等原因
// true 成功
public bool IncHitCount(string strURL)
{
IMongoCollection<HitCountItem> collection = this.HitCountCollection;
if (collection == null)
return false;
var query = new QueryDocument("URL", strURL);
var update = Update.Inc("HitCount", 1);
collection.Update(
query,
update,
UpdateFlags.Upsert);
return true;
}
#endif
public long GetHitCount(string strURL)
{
IMongoCollection<HitCountItem> collection = this.HitCountCollection;
if (collection == null)
return -1;
var item = collection.Find(Builders<HitCountItem>.Filter.Eq("URL", strURL))
.FirstOrDefault();
if (item == null)
return 0;
return item.HitCount;
/*
var query = new QueryDocument("URL", strURL);
var item = collection.FindOne(query);
if (item == null)
return 0;
return item.HitCount;
*/
}
}
public class HitCountItem
{
[BsonId]
[BsonRepresentation(BsonType.ObjectId)]
public string Id { get; private set; }
public string URL { get; set; } // 资源 URL
public long HitCount { get; set; } // 次数
}
}