MINESWEEPER 系统设计
原型文档 · 非正式发布返回游戏 ↗
参考源码设计 v0.8.1

put_immutable.lua

参考源码 · docs/account-system/redis/put_immutable.lua

lua
-- KEYS[1]: server-derived cache/upload/body/chunk key.
-- ARGV[1]: validated binary value; ARGV[2]: absolute expiry in UTC milliseconds.
-- Application enforces ownership, candidate scope, byte budgets and expiry policy.
local deadline = tonumber(ARGV[2])
local time = redis.call('TIME')
local now = tonumber(time[1]) * 1000 + math.floor(tonumber(time[2]) / 1000)
if not deadline or deadline ~= math.floor(deadline) or deadline <= now then
  return redis.error_reply('DEADLINE_EXPIRED')
end
local previous = redis.call('GET', KEYS[1])
if previous then
  if previous ~= ARGV[1] then return redis.error_reply('CONTENT_MISMATCH') end
  local expires = redis.call('PEXPIRETIME', KEYS[1])
  if expires <= now then return redis.error_reply('INVALID_EXISTING_TTL') end
  -- Preserve the exact expiry. A write on this connection permits a subsequent
  -- WAITAOF barrier to cover prior writes, including recovery of a lost reply.
  redis.call('PEXPIREAT', KEYS[1], expires)
  return {'existing', tostring(expires)}
end
redis.call('SET', KEYS[1], ARGV[1], 'NX', 'PXAT', deadline)
return {'created', tostring(deadline)}