@waline/vercel 1.17.0 → 1.17.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@waline/vercel",
3
- "version": "1.17.0",
3
+ "version": "1.17.1",
4
4
  "description": "vercel server for waline comment system",
5
5
  "keywords": [
6
6
  "waline",
@@ -18,7 +18,7 @@ module.exports = class extends BaseRest {
18
18
  const users = await this.modelInstance.select(
19
19
  {},
20
20
  {
21
- desc: 'insertedAt',
21
+ desc: 'createdAt',
22
22
  limit: pageSize,
23
23
  offset: Math.max((page - 1) * pageSize, 0),
24
24
  }
@@ -24,6 +24,10 @@ module.exports = {
24
24
  },
25
25
  promiseAllQueue(promises, taskNum) {
26
26
  return new Promise((resolve, reject) => {
27
+ if (!promises.length) {
28
+ return resolve();
29
+ }
30
+
27
31
  const ret = [];
28
32
  let index = 0;
29
33
  let count = 0;
@@ -126,6 +126,88 @@ module.exports = class extends Base {
126
126
  return data;
127
127
  }
128
128
 
129
+ async _getCmtGroupByMailUserIdCache(key, where) {
130
+ if (this.tableName !== 'Comment' || key !== 'user_id_mail') {
131
+ return [];
132
+ }
133
+
134
+ const cacheTableName = `cache_group_count_${key}`;
135
+ const currentTableName = this.tableName;
136
+ this.tableName = cacheTableName;
137
+ const cacheData = await this.select({ _complex: where._complex });
138
+ this.tableName = currentTableName;
139
+ return cacheData;
140
+ }
141
+
142
+ async _setCmtGroupByMailUserIdCache(key, data) {
143
+ if (this.tableName !== 'Comment' || key !== 'user_id_mail') {
144
+ return;
145
+ }
146
+
147
+ const cacheTableName = `cache_group_count_${key}`;
148
+ const currentTableName = this.tableName;
149
+ this.tableName = cacheTableName;
150
+
151
+ await think.promiseAllQueue(
152
+ data.map((item) => {
153
+ if (item.user_id && !think.isString(item.user_id)) {
154
+ item.user_id = item.user_id.toString();
155
+ }
156
+ return this.add(item);
157
+ }),
158
+ 1
159
+ );
160
+ this.tableName = currentTableName;
161
+ }
162
+
163
+ async _updateCmtGroupByMailUserIdCache(data, method) {
164
+ if (this.tableName !== 'Comment') {
165
+ return;
166
+ }
167
+
168
+ if (!data.user_id && !data.mail) {
169
+ return;
170
+ }
171
+
172
+ const cacheTableName = `cache_group_count_user_id_mail`;
173
+ const cacheData = await this.select({
174
+ _complex: {
175
+ _logic: 'or',
176
+ user_id: think.isObject(data.user_id)
177
+ ? data.user_id.toString()
178
+ : data.user_id,
179
+ mail: data.mail,
180
+ },
181
+ });
182
+ if (think.isEmpty(data)) {
183
+ return;
184
+ }
185
+
186
+ let count = cacheData[0].count;
187
+ switch (method) {
188
+ case 'add':
189
+ if (data.status === 'approved') {
190
+ count += 1;
191
+ }
192
+ break;
193
+ case 'udpate_status':
194
+ if (data.status === 'approved') {
195
+ count += 1;
196
+ } else {
197
+ count -= 1;
198
+ }
199
+ break;
200
+ case 'delete':
201
+ count -= 1;
202
+ break;
203
+ }
204
+
205
+ const currentTableName = this.tableName;
206
+ this.tableName = cacheTableName;
207
+ await this.update({ count }, { objectId: cacheData[0].objectId });
208
+ this.tableName = currentTableName;
209
+ }
210
+
129
211
  async count(where = {}, options = {}) {
130
212
  const instance = this.where(this.tableName, where);
131
213
  if (!options.group) {
@@ -137,7 +219,19 @@ module.exports = class extends Base {
137
219
  });
138
220
  }
139
221
 
140
- // todo: query optimize
222
+ // get group count cache by group field where data
223
+ const cacheData = await this._getCmtGroupByMailUserIdCache(
224
+ options.group.join('_'),
225
+ where
226
+ );
227
+ const cacheDataMap = {};
228
+ for (let i = 0; i < cacheData.length; i++) {
229
+ const key = options.group
230
+ .map((item) => cacheData[i][item] || null)
231
+ .join('_');
232
+ cacheDataMap[key] = cacheData[i];
233
+ }
234
+
141
235
  const counts = [];
142
236
  const countsPromise = [];
143
237
  for (let i = 0; i < options.group.length; i++) {
@@ -152,6 +246,19 @@ module.exports = class extends Base {
152
246
  });
153
247
 
154
248
  for (let j = 0; j < where._complex[groupName][1].length; j++) {
249
+ const cacheKey = options.group
250
+ .map(
251
+ (item) =>
252
+ ({
253
+ ...groupFlatValue,
254
+ [groupName]: where._complex[groupName][1][j],
255
+ }[item] || null)
256
+ )
257
+ .join('_');
258
+ if (cacheDataMap[cacheKey]) {
259
+ continue;
260
+ }
261
+
155
262
  const groupWhere = {
156
263
  ...where,
157
264
  ...groupFlatValue,
@@ -172,8 +279,10 @@ module.exports = class extends Base {
172
279
  }
173
280
  }
174
281
 
175
- await think.promiseAllQueue(countsPromise, 3);
176
- return counts;
282
+ await think.promiseAllQueue(countsPromise, 1);
283
+ // cache data
284
+ await this._setCmtGroupByMailUserIdCache(options.group.join('_'), counts);
285
+ return [...cacheData, ...counts];
177
286
  }
178
287
 
179
288
  async add(
@@ -190,6 +299,7 @@ module.exports = class extends Base {
190
299
  instance.setACL(acl);
191
300
 
192
301
  const resp = await instance.save();
302
+ await this._updateCmtGroupByMailUserIdCache(data, 'add');
193
303
  return resp.toJSON();
194
304
  }
195
305
 
@@ -199,11 +309,16 @@ module.exports = class extends Base {
199
309
 
200
310
  return Promise.all(
201
311
  ret.map(async (item) => {
312
+ const _oldStatus = item.get('status');
202
313
  if (think.isFunction(data)) {
203
314
  item.set(data(item.toJSON()));
204
315
  } else {
205
316
  item.set(data);
206
317
  }
318
+ const _newStatus = item.get('status');
319
+ if (_newStatus && _oldStatus !== _newStatus) {
320
+ await this._updateCmtGroupByMailUserIdCache(data, 'update_status');
321
+ }
207
322
 
208
323
  const resp = await item.save();
209
324
  return resp.toJSON();
@@ -214,6 +329,7 @@ module.exports = class extends Base {
214
329
  async delete(where) {
215
330
  const instance = this.where(this.tableName, where);
216
331
  const data = await instance.find();
332
+ await this._updateCmtGroupByMailUserIdCache(data, 'delete');
217
333
 
218
334
  return AV.Object.destroyAll(data);
219
335
  }