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
cb0c15c5
Commit
cb0c15c5
authored
Apr 28, 2021
by
yanzg
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
分布式幂等性判断
parent
f03ecbd0
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
103 additions
and
0 deletions
+103
-0
pom.xml
yzg-util-redis/pom.xml
+6
-0
CacheLock.java
...-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
+97
-0
No files found.
yzg-util-redis/pom.xml
View file @
cb0c15c5
...
...
@@ -17,6 +17,12 @@
<!--<groupId>org.springframework.boot</groupId>-->
<!--<artifactId>spring-boot-starter-data-redis</artifactId>-->
<!--</dependency>-->
<dependency>
<groupId>
com.yanzuoguang
</groupId>
<artifactId>
yzg-util-base
</artifactId>
</dependency>
<dependency>
<groupId>
com.alicp.jetcache
</groupId>
<artifactId>
jetcache-starter-redis-lettuce
</artifactId>
...
...
yzg-util-redis/src/main/java/com.yanzuoguang.redis/CacheLock.java
0 → 100644
View file @
cb0c15c5
package
com
.
yanzuoguang
.
redis
;
import
com.alicp.jetcache.Cache
;
import
com.yanzuoguang.util.thread.ThreadHelper
;
import
java.util.concurrent.TimeUnit
;
/**
* 运行函数
*
* @author 颜佐光
*/
public
class
CacheLock
implements
Runnable
{
/**
* 是否执行标记
*/
private
boolean
runFlag
=
false
;
/**
* 缓存对象
*/
private
Cache
cache
;
/**
* Redis 锁定时间(秒)
*/
private
int
lockTime
;
/**
* 每次等待时间(毫秒)
*/
private
int
waitUnitTime
;
/**
* 关键字
*/
private
String
key
;
/**
* 执行函数
*/
private
final
Runnable
func
;
/**
* 构造函数
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @param func
*/
public
CacheLock
(
Cache
cache
,
int
lockTime
,
int
waitUnitTime
,
String
key
,
Runnable
func
)
{
this
.
cache
=
cache
;
this
.
lockTime
=
lockTime
;
this
.
waitUnitTime
=
waitUnitTime
;
this
.
key
=
key
;
this
.
func
=
func
;
}
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*/
@Override
public
void
run
()
{
// 需要运行的函数
Runnable
runnable
=
new
Runnable
()
{
@Override
public
void
run
()
{
CacheLock
.
this
.
func
.
run
();
runFlag
=
true
;
}
};
do
{
// 开启唯一性锁,防止多人运行同一关键字的函数
cache
.
tryLockAndRun
(
key
,
lockTime
,
TimeUnit
.
SECONDS
,
runnable
);
// 假如没有运行,则等待50毫秒后继续运行
if
(!
runFlag
)
{
ThreadHelper
.
sleep
(
waitUnitTime
);
}
}
while
(!
runFlag
);
}
/**
* 开始执行,每个关键字会等待其他关键字执行完成后执行
*
* @param cache
* @param lockTime
* @param waitUnitTime
* @param key
* @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
);
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