github.com/benz9527/xboot@v0.0.0-20240504061247-c23f15593274/dlock/lockrenewal.lua (about)

     1  -- MGET key1 key2 ...
     2  -- Fetch locked keys' values then update TTL if all matched
     3  -- to target value.
     4  -- Redis Lua5.1 only support unpack() function,
     5  -- so we can't use table.unpack() here.
     6  local lockedValues = redis.call("MGET", unpack(KEYS))
     7  for i, _ in ipairs(KEYS) do
     8      if lockedValues[i] ~= ARGV[1] then
     9          return redis.error_reply("dlock token mismatch, unable to refresh")
    10      end
    11  end
    12  
    13  -- PEXPIRE key milliseconds
    14  -- 1: OK
    15  -- 0: Not exist or set failed.
    16  local function updateLockTTL(ttl)
    17      for _, k in ipairs(KEYS) do
    18          redis.call("PEXPIRE", k, ttl)
    19      end
    20  end
    21  
    22  updateLockTTL(ARGV[2])
    23  return redis.status_reply("OK")