Redis实现可重入的分布式锁

加锁脚本

-- 加锁脚本
-- 成功返回1,失败返回-1
local key = KEYS[1]
local requestId = KEYS[2]
-- 单位毫秒
local ttl = tonumber(KEYS[3])
local result = redis.call('setnx', key, requestId)
if result == 1 then
    redis.call('pexpire', key, ttl)
else
    result = -1
    local value = redis.call('get', key)
    -- 可重入
    if (value == requestId) then
        result = 1;
        redis.call('pexpire', key, ttl)
    end
end
return result;

解锁脚本

-- 解锁脚本
-- 成功返回1,失败返回-1
local key = KEYS[1]
local requestId = KEYS[2]
local value = redis.call('get', key)
if value == requestId then
    redis.call('del', key)
    return 1
end
return -1

执行脚本代码

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.JedisPoolConfig;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;

public class RedisLuaScriptExample {
    public static void main(String[] args) {
        JedisPool pool = new JedisPool(new JedisPoolConfig(), "localhost");
        try (Jedis jedis = pool.getResource()) {
            // 读取 Lua 脚本文件
            String script = new String(Files.readAllBytes(Paths.get("lock.lua")));

            // 加载 Lua 脚本到 Redis
            String scriptSha = jedis.scriptLoad(script, "0");

            // 执行 Lua 脚本
            jedis.evalsha(script, 0); // 改一下参数

        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            pool.close();
        }
    }
}
posted @   风小雅  阅读(38)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· AI 智能体引爆开源社区「GitHub 热点速览」
· C#/.NET/.NET Core技术前沿周刊 | 第 29 期(2025年3.1-3.9)
· 从HTTP原因短语缺失研究HTTP/2和HTTP/3的设计差异
点击右上角即可分享
微信分享提示