mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
添加功能:pretest discuss user dashboard statistics
This commit is contained in:
1 parent
61cba6f180
commit
15d43636c0
41 files changed
+920
-523
No files matched your search
+9
-30
@@ -11,41 +11,20 @@ async function update(_id, operation, config) {
|
||||
await coll.findOneAndUpdate({ _id }, operation, config);
|
||||
return get(_id);
|
||||
}
|
||||
async function set(_id, value) {
|
||||
await coll.findOneAndUpdate({ _id }, { value }, { upsert: true });
|
||||
return get(_id);
|
||||
}
|
||||
/**
|
||||
* Increments the user counter.
|
||||
* Increments the counter.
|
||||
* @returns {number} Integer value after increment.
|
||||
*/
|
||||
function incUserCounter() {
|
||||
return update('userCounter', { $inc: { value: 1 } }, { upsert: true });
|
||||
}
|
||||
/**
|
||||
* Increments the problem ID counter.
|
||||
* @returns {number} Integer value before increment.
|
||||
*/
|
||||
function incPidCounter() {
|
||||
return update('userCounter', { $inc: { value: 1 } }, { upsert: true });
|
||||
}
|
||||
async function acquireLock(name) {
|
||||
const value = Math.floor(Math.random() * 0xFFFFFFFF);
|
||||
try {
|
||||
await coll.updateOne({ _id: `lock_${name}`, value: 0 }, { $set: { value } }, { upsert: true });
|
||||
} catch (e) { return null; }
|
||||
return value;
|
||||
}
|
||||
async function releaseLock(name, value) {
|
||||
const result = await coll.updateOne({ _id: `lock_${name}`, value }, { $set: { value: 0 } });
|
||||
return !!result.matchedCount;
|
||||
}
|
||||
async function releaseLockAnyway(name) {
|
||||
await coll.updateOne({ _id: `lock_${name}` }, { $set: { value: 0 } });
|
||||
return true;
|
||||
function inc(field) {
|
||||
return update(field, { $inc: { value: 1 } }, { upsert: true });
|
||||
}
|
||||
module.exports = {
|
||||
get,
|
||||
update,
|
||||
incPidCounter,
|
||||
incUserCounter,
|
||||
acquireLock,
|
||||
releaseLock,
|
||||
releaseLockAnyway,
|
||||
inc,
|
||||
set,
|
||||
};
|
||||
Reference in new issue
Block a user