Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
O
OZT-Integration
Project
Project
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
Graph
比较
统计图
议题
0
议题
0
列表
看板
标记
Milestones
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
许俊
OZT-Integration
Commits
fa8873e4
提交
fa8873e4
authored
12月 17, 2021
作者:
许俊
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
微调
上级
391b9226
隐藏空白字符变更
内嵌
并排
正在显示
1 个修改的文件
包含
0 行增加
和
107 行删除
+0
-107
RedisLock.java
...mmon/src/main/java/org/jeecg/common/system/RedisLock.java
+0
-107
没有找到文件。
jeecg-boot-base-common/src/main/java/org/jeecg/common/system/RedisLock.java
deleted
100644 → 0
浏览文件 @
391b9226
package
org
.
jeecg
.
common
.
util
;
import
org.springframework.data.redis.connection.RedisConnection
;
import
org.springframework.data.redis.connection.RedisConnectionFactory
;
import
org.springframework.data.redis.connection.RedisStringCommands
;
import
org.springframework.data.redis.connection.ReturnType
;
import
org.springframework.data.redis.core.RedisConnectionUtils
;
import
org.springframework.data.redis.core.StringRedisTemplate
;
import
org.springframework.data.redis.core.types.Expiration
;
import
org.springframework.stereotype.Repository
;
import
java.nio.charset.Charset
;
import
java.util.UUID
;
import
java.util.concurrent.TimeUnit
;
@Repository
public
class
RedisLock
{
/**
* 解锁脚本,原子操作
*/
private
static
final
String
unlockScript
=
"if redis.call(\"get\",KEYS[1]) == ARGV[1]\n"
+
"then\n"
+
" return redis.call(\"del\",KEYS[1])\n"
+
"else\n"
+
" return 0\n"
+
"end"
;
private
StringRedisTemplate
redisTemplate
;
public
RedisLock
(
StringRedisTemplate
redisTemplate
)
{
this
.
redisTemplate
=
redisTemplate
;
}
/**
* 加锁,有阻塞
* @param name
* @param expire
* @param timeout
* @return
*/
public
String
lock
(
String
name
,
long
expire
,
long
timeout
){
long
startTime
=
System
.
currentTimeMillis
();
String
token
;
do
{
token
=
tryLock
(
name
,
expire
);
if
(
token
==
null
)
{
if
((
System
.
currentTimeMillis
()-
startTime
)
>
(
timeout
-
50
))
break
;
try
{
Thread
.
sleep
(
50
);
//try 50 per sec
}
catch
(
InterruptedException
e
)
{
e
.
printStackTrace
();
return
null
;
}
}
}
while
(
token
==
null
);
return
token
;
}
/**
* 加锁,无阻塞
* @param name
* @param expire
* @return
*/
public
String
tryLock
(
String
name
,
long
expire
)
{
String
token
=
UUID
.
randomUUID
().
toString
();
RedisConnectionFactory
factory
=
redisTemplate
.
getConnectionFactory
();
RedisConnection
conn
=
factory
.
getConnection
();
try
{
Boolean
result
=
conn
.
set
(
name
.
getBytes
(
Charset
.
forName
(
"UTF-8"
)),
token
.
getBytes
(
Charset
.
forName
(
"UTF-8"
)),
Expiration
.
from
(
expire
,
TimeUnit
.
MILLISECONDS
),
RedisStringCommands
.
SetOption
.
SET_IF_ABSENT
);
if
(
result
!=
null
&&
result
)
return
token
;
}
finally
{
RedisConnectionUtils
.
releaseConnection
(
conn
,
factory
);
}
return
null
;
}
/**
* 解锁
* @param name
* @param token
* @return
*/
public
boolean
unlock
(
String
name
,
String
token
)
{
byte
[][]
keysAndArgs
=
new
byte
[
2
][];
keysAndArgs
[
0
]
=
name
.
getBytes
(
Charset
.
forName
(
"UTF-8"
));
keysAndArgs
[
1
]
=
token
.
getBytes
(
Charset
.
forName
(
"UTF-8"
));
RedisConnectionFactory
factory
=
redisTemplate
.
getConnectionFactory
();
RedisConnection
conn
=
factory
.
getConnection
();
try
{
Long
result
=
(
Long
)
conn
.
scriptingCommands
().
eval
(
unlockScript
.
getBytes
(
Charset
.
forName
(
"UTF-8"
)),
ReturnType
.
INTEGER
,
1
,
keysAndArgs
);
if
(
result
!=
null
&&
result
>
0
)
return
true
;
}
finally
{
RedisConnectionUtils
.
releaseConnection
(
conn
,
factory
);
}
return
false
;
}
}
\ No newline at end of file
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论