Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Z
zhichan
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
蒋勇
zhichan
Commits
a7d0a16c
Commit
a7d0a16c
authored
May 15, 2020
by
zhaoxiqing
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
gsb
parent
e90a04e4
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
122 additions
and
0 deletions
+122
-0
bpo-web/app/base/api/impl/testApi.js
+30
-0
bpo-web/app/base/utils/redisLock.js
+92
-0
No files found.
bpo-web/app/base/api/impl/testApi.js
View file @
a7d0a16c
...
@@ -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
;
bpo-web/app/base/utils/redisLock.js
0 → 100644
View file @
a7d0a16c
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
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment