Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Sign in / Register
Toggle navigation
Y
yzg-util
Project
Project
Details
Activity
Releases
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
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
YZG
yzg-util
Commits
2ebffc67
Commit
2ebffc67
authored
Apr 29, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
分布式幂等性判断
parent
267ee8d8
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
45 additions
and
5 deletions
+45
-5
CacheLock.java
...-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
+45
-5
No files found.
yzg-util-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
View file @
2ebffc67
...
...
@@ -35,6 +35,10 @@ public class CacheLock implements Runnable {
* 关键字
*/
private
String
key
;
/**
* 执行函数
*/
private
Runnable
funcWait
;
/**
* 执行函数
*/
...
...
@@ -54,7 +58,7 @@ public class CacheLock implements Runnable {
* @param key
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
)
{
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
);
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
null
);
}
/**
...
...
@@ -67,10 +71,25 @@ public class CacheLock implements Runnable {
* @param func
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
func
)
{
this
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
}
/**
* 构造函数
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param funcWait 等待执行
* @param func
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
funcWait
,
Runnable
func
)
{
this
.
cache
=
cache
;
this
.
lockTime
=
lockTime
;
this
.
waitUnitTime
=
waitUnitTime
;
this
.
key
=
key
;
this
.
funcWait
=
funcWait
;
this
.
func
=
func
;
}
...
...
@@ -103,11 +122,9 @@ public class CacheLock implements Runnable {
Runnable
runnable
=
new
Runnable
()
{
@Override
public
void
run
()
{
CacheLock
.
this
.
func
.
run
();
runFlag
=
true
;
funcRun
();
}
};
do
{
// 开启唯一性锁,防止多人运行同一关键字的函数
cache
.
tryLockAndRun
(
key
,
lockTime
,
TimeUnit
.
SECONDS
,
runnable
);
...
...
@@ -119,6 +136,16 @@ public class CacheLock implements Runnable {
}
while
(!
runFlag
);
}
private
void
funcRun
()
{
if
(
this
.
waitCount
>
0
&&
this
.
funcWait
!=
null
)
{
funcWait
.
run
();
}
if
(
this
.
func
!=
null
)
{
func
.
run
();
}
runFlag
=
true
;
}
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
...
...
@@ -129,7 +156,20 @@ public class CacheLock implements Runnable {
* @param func
*/
public
static
void
run
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
func
)
{
CacheLock
lock
=
new
CacheLock
(
cache
,
lockTime
,
waitUnitTime
,
key
,
func
);
run
(
cache
,
lockTime
,
waitUnitTime
,
key
,
null
,
func
);
}
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param func
*/
public
static
void
run
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
funcWait
,
Runnable
func
)
{
CacheLock
lock
=
new
CacheLock
(
cache
,
lockTime
,
waitUnitTime
,
key
,
funcWait
,
func
);
lock
.
run
();
}
}
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