Commit a7d0a16c by zhaoxiqing

gsb

parent e90a04e4
...@@ -7,12 +7,14 @@ const logCtl = System.getObject("web.oplogCtl"); ...@@ -7,12 +7,14 @@ const logCtl = System.getObject("web.oplogCtl");
const fs = require("fs"); const fs = require("fs");
const xlsx = require('node-xlsx') const xlsx = require('node-xlsx')
var moment = require('moment') var moment = require('moment')
const uuidv1 = require('uuid/v1');
class TestApi { class TestApi {
constructor() { constructor() {
this.utilesignbaoSve = System.getObject("service.utilesignbaoSve"); this.utilesignbaoSve = System.getObject("service.utilesignbaoSve");
this.redisClient = System.getObject("util.redisClient"); this.redisClient = System.getObject("util.redisClient");
this.downcontractClient = System.getObject("util.downcontractClient"); this.downcontractClient = System.getObject("util.downcontractClient");
// this.publishClient = System.getObject("util.publishClient"); // this.publishClient = System.getObject("util.publishClient");
this.redisLock = System.getObject("util.redisLock");
} }
async testAddDL(obj) { async testAddDL(obj) {
...@@ -88,6 +90,34 @@ class TestApi { ...@@ -88,6 +90,34 @@ class TestApi {
} }
} }
sleep(time) {
return new Promise((resolve) => {
setTimeout(function () {
resolve();
}, time || 1000);
});
}
async test1(key) {
try {
const id = uuidv1();
await this.redisLock.lock(key, id, 20);
await this.sleep(3000);
console.log(11111111)
const unLock = await this.redisLock.unLock(key, id);
console.log('unLock: ', key, id, unLock);
} catch (err) {
console.log('上锁失败', err);
}
}
async sss(key){
this.test1(key.key)
this.test1(key.key)
}
} }
module.exports = TestApi; module.exports = TestApi;
const Redis = require("ioredis");
const redis = new Redis(6379, "127.0.0.1");
class RedisLock {
constructor() {
this.lockLeaseTime = 2; // 默认锁过期时间 2 秒
this.lockTimeout = 5; // 默认锁超时时间 5 秒
this.expiryMode = 'EX';
this.setMode = 'NX';
this.client = redis;
}
/**
* 上锁
* @param {*} key
* @param {*} val
* @param {*} expire
*/
async lock(key, val, expire) {
const start = Date.now();
const self = this;
return (async function intranetLock() {
try {
const result = await self.client.set(key, val, self.expiryMode, expire || self.lockLeaseTime, self.setMode);
// 上锁成功
if (result === 'OK') {
console.log(`${key} ${val} 上锁成功`);
return true;
}
// 锁超时
if (Math.floor((Date.now() - start) / 1000) > self.lockTimeout) {
console.log(`${key} ${val} 上锁重试超时结束`);
return false;
}
// 循环等待重试
console.log(`${key} ${val} 等待重试`);
await sleep(3000);
console.log(`${key} ${val} 开始重试`);
return intranetLock();
} catch (err) {
throw new Error(err);
}
})();
}
/**
* 释放锁
* @param {*} key
* @param {*} val
*/
async unLock(key, val) {
const self = this;
const script = "if redis.call('get',KEYS[1]) == ARGV[1] then" +
" return redis.call('del',KEYS[1]) " +
"else" +
" return 0 " +
"end";
try {
const result = await self.client.eval(script, 1, key, val);
if (result === 1) {
return true;
}
return false;
} catch (err) {
throw new Error(err);
}
}
}
function sleep(time) {
return new Promise((resolve) => {
setTimeout(function () {
resolve();
}, time || 1000);
});
}
module.exports = RedisLock;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment