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

     1  -- MGET key1 key2 ...
     2  -- Fetch locked keys' values then get min 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 lockerValues = redis.call("MGET", unpack(KEYS))
     7  for i, _ in ipairs(lockerValues) do
     8      if lockerValues[i] ~= ARGV[1] then
     9          return redis.error_reply("dlock token mismatch, unable to fetch ttl")
    10      end
    11  end
    12  
    13  -- PTTL key
    14  -- -2: Not exist.
    15  -- -1: Exists but no TTL.
    16  local minTTL = 0
    17  for _, k in ipairs(KEYS) do
    18      local ttl = redis.call("PTTL", k)
    19      if ttl > 0 and (minTTL == 0 or ttl < minTTL) then
    20          minTTL = ttl
    21      end
    22  end
    23  -- ttl lower or equal to 0 probably means the keys
    24  -- no longer exists.
    25  return minTTL