lease
ModuleGeneration leases and purge tombstones, shared by the Redis-protocol
adapters (crate::Redis, crate::Multiplexing).
Each key carries a generation in a reserved field of its own hash, advanced atomically by every purge. A reader captures it before reading the source of truth and presents it when saving; the save lands only if no purge happened in between. An optional grace window also refuses saves for a while after a purge (the tombstone). Every script touches one key, so they stay correct on Redis Cluster.
Contents
use utopia_cache::lease::is_reserved;pub fn is_reserved(field: &str) -> boolWhether field is one of the reserved fields.
use utopia_cache::lease::script_sha;pub fn script_sha(script: &str) -> std::borrow::Cow<'static, str>The SHA-1 digest Redis identifies a script by (memoized for the lease scripts).
pub const GENERATION_FIELD: &str = "__utopia_gen__"Reserved field holding a key's generation. Not usable as a cache field.
pub const LUA_PURGE_BUMP: &str = "local gen = '__utopia_gen__' local tomb = '__utopia_tomb__' local removed = redis.call('HLEN', KEYS[1]) - redis.call('HEXISTS', KEYS[1], gen) - redis.call('HEXISTS', KEYS[1], tomb) local current = redis.call('HGET', KEYS[1], gen) local next = (tonumber(current) or 0) + 1 redis.call('DEL', KEYS[1]) redis.call('HSET', KEYS[1], gen, next) local window = tonumber(ARGV[1]) or 0 if window > 0 then local t = redis.call('TIME') local now = tonumber(t[1]) * 1000000 + tonumber(t[2]) redis.call('HSET', KEYS[1], tomb, now + window * 1000) end return removed"Drops every value field, advances the generation and stamps the
tombstone (ARGV[1] = grace window, ms). Returns the value fields removed.
pub const LUA_PURGE_FIELD: &str = "local removed = redis.call('HDEL', KEYS[1], ARGV[1]) local current = redis.call('HGET', KEYS[1], '__utopia_gen__') local next = (tonumber(current) or 0) + 1 redis.call('HSET', KEYS[1], '__utopia_gen__', next) local window = tonumber(ARGV[2]) or 0 if window > 0 then local t = redis.call('TIME') local now = tonumber(t[1]) * 1000000 + tonumber(t[2]) redis.call('HSET', KEYS[1], '__utopia_tomb__', now + window * 1000) end return removed"Deletes field ARGV[1], advances the generation and stamps the
tombstone (ARGV[2] = grace window, ms). Returns the fields removed.
pub const LUA_SAVE_WITH_LEASE: &str = "local current = redis.call('HGET', KEYS[1], '__utopia_gen__') if current == false then current = '0' end if current ~= ARGV[3] then return 0 end local window = tonumber(ARGV[4]) or 0 if window > 0 then local tomb = redis.call('HGET', KEYS[1], '__utopia_tomb__') if tomb ~= false then local deadline = tonumber(tomb) if deadline ~= nil then local t = redis.call('TIME') local now = tonumber(t[1]) * 1000000 + tonumber(t[2]) -- 2nd clause ignores a deadline left by a since-rewound clock if now < deadline and (deadline - now) <= window * 1000 then return 0 end end redis.call('HDEL', KEYS[1], '__utopia_tomb__') end end redis.call('HSET', KEYS[1], ARGV[1], ARGV[2]) return 1"Saves ARGV[1] = ARGV[2] only when the generation equals ARGV[3] and
the key is past its tombstone (ARGV[4] = grace window, ms).
pub const TOMBSTONE_FIELD: &str = "__utopia_tomb__"Reserved field holding the post-purge deadline (epoch microseconds) before which leased saves are refused. Not usable as a cache field.