Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
晶之木
miaosha
提交
39d863eb
M
miaosha
项目概览
晶之木
/
miaosha
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
M
miaosha
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
39d863eb
编写于
12月 22, 2018
作者:
Q
qiurunze
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
lua分布式锁
上级
8aa5ce85
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
61 addition
and
3 deletion
+61
-3
README.md
README.md
+1
-0
docs/redis-good.md
docs/redis-good.md
+5
-3
src/main/java/com/geekq/miaosha/redis/redismanager/lua/RedisLuaLock.java
...om/geekq/miaosha/redis/redismanager/lua/RedisLuaLock.java
+5
-0
src/main/java/com/geekq/miaosha/redis/redismanager/lua/luaLockScript.lua
...om/geekq/miaosha/redis/redismanager/lua/luaLockScript.lua
+27
-0
src/main/java/com/geekq/miaosha/redis/redismanager/lua/testlua.lua
...java/com/geekq/miaosha/redis/redismanager/lua/testlua.lua
+23
-0
未找到文件。
README.md
浏览文件 @
39d863eb
...
...
@@ -66,6 +66,7 @@
| 024 |如何进行分库分表 |
[
解决思路
](
/docs/mysql-master-slave.md
)
|
| 025 |秒杀类似场景sql的写法注意事项有哪些?|
[
解决思路
](
/docs/mysql-master-slave.md
)
|
| 026 |如何利用lua脚本进行操作限流与分布式锁(可保证原子性)?|
[
解决思路
](
/docs/redis-good.md
)
|
| 027 |如何利用lua脚本进行分布式锁操作?|
[
解决思路
](
/docs/redis-good.md
)
|
#### [分布式系统发展历程(已更新)](/docs/fenbushi.md)
#### [分布式系统](/docs/redis-code.md)
...
...
docs/redis-good.md
浏览文件 @
39d863eb
...
...
@@ -155,6 +155,8 @@
最后第二个窗口的运行结果是Busy, 可以通过script kill命令终止正在执行的脚本
如果当前执行的lua脚本对redis的数据进行了修改,比如(set)操作,那么script kill命令没办法终止脚本的运行,
因为要保证lua脚本的原子性。如果执行一部分终止了,就违背了这一个原则
在这种情况下,只能通过 shutdown nosave命令强行终止
在这种情况下,只能通过 shutdown nosave命令强行终止
**Redis(2.6以后)--lua分布式锁**
!
[
整体流程
](
https://raw.githubusercontent.com/qiurunze123/imageall/master/lualock.png
)
src/main/java/com/geekq/miaosha/redis/redismanager/lua/RedisLuaLock.java
0 → 100644
浏览文件 @
39d863eb
package
com.geekq.miaosha.redis.redismanager.lua
;
public
class
RedisLuaLock
{
}
src/main/java/com/geekq/miaosha/redis/redismanager/lua/luaLockScript.lua
0 → 100644
浏览文件 @
39d863eb
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by qiurunze.
--- DateTime: 2018/12/22 18:20
---
--- 释放锁
if
redis
.
call
(
'get'
,
KEY
[
1
]
==
ARGV
[
1
])
then
return
redis
.
call
(
'del'
,
KEY
[
1
])
else
return
0
end
--- 加锁
local
key
=
KEY
[
1
]
local
content
=
KEY
[
2
]
local
ttl
=
AVG
[
1
]
local
lockSet
=
redis
.
call
(
'setnx'
,
key
,
content
)
if
lockSet
==
1
then
redis
.
call
(
'pexpire'
,
key
,
ttl
)
else
local
value
=
redis
.
call
(
'get'
,
key
)
if
value
==
content
then
lockSet
=
1
redis
.
call
(
'pexpire'
,
key
,
ttl
)
end
end
return
lockSet
\ No newline at end of file
src/main/java/com/geekq/miaosha/redis/redismanager/lua/testlua.lua
0 → 100644
浏览文件 @
39d863eb
---
--- Generated by EmmyLua(https://github.com/EmmyLua)
--- Created by qiurunze.
--- DateTime: 2018/12/22 16:33
---
---
local
key
=
KEY
[
1
]
local
content
=
KEY
[
2
]
local
ttl
=
AVG
[
1
]
local
lockSet
=
redis
.
call
(
'setnx'
,
key
,
content
)
if
lockSet
==
1
then
redis
.
call
(
'pexpire'
,
key
,
ttl
)
else
local
value
=
redis
.
call
(
'get'
,
key
)
if
value
==
content
then
lockSet
=
1
redis
.
call
(
'pexpire'
,
key
,
ttl
)
end
end
return
lockSet
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录