GOBLIN.HCAD

GOBLIN.HCAD key field expected

Compare-and-delete a hash field. Delete field and reply 1 when it holds a string equal to expected; otherwise leave it in place and reply 0. It is the field-level form of GOBLIN.CAD: the atomic "delete only if it still holds what I put there" check, applied to one field of a hash instead of a whole key.

The idiom it replaces

Deleting a field only when it still holds an expected token — a per-field lock release, a conditional retraction — is a read-compare-delete that must be atomic, so in Redis it is a Lua script:

if redis.call("hget", KEYS[1], ARGV[1]) == ARGV[2] then
  return redis.call("hdel", KEYS[1], ARGV[1])
end
return 0

GOBLIN.HCAD key field expected does exactly this in one atomic C++ op — an HGET, a byte-wise compare, and a conditional HDEL. The redis.calls become direct calls to the store's primitives, with no interpreter and no command re-entry.

Semantics

Return value

Situation Reply
field holds a string equal to expected (integer) 1; the field is deleted
field holds a different value (integer) 0; nothing changes
field is absent, or key is absent (integer) 0
key holds a non-hash (string / zset) WRONGTYPE error

Examples

A per-field lock release — a worker deletes its slot only if it still owns it:

> HSET locks job:7 worker-a
(integer) 1
> GOBLIN.HCAD locks job:7 worker-b
(integer) 0          # not the owner -> the lock is left alone
> GOBLIN.HCAD locks job:7 worker-a
(integer) 1          # owner matches -> released
> HEXISTS locks job:7
(integer) 0

Because it deletes only on an exact match, two workers cannot both release the slot: the second HCAD sees a changed (or absent) value and returns 0, the same safety GOBLIN.CAD gives a whole-key lock.

See also