mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
fix(cache): advance the generation on field-level purges too
Review on #75: purge($key, $hash) only ran HDEL on the field and left the per-key generation untouched, so the lease was bypassed for hashed entries — a reader could capture the generation, a concurrent field-purge would delete the field without advancing the generation, and the reader's saveWithLease(..., $hash, capturedGeneration) would still pass the CAS and re-cache stale data. Field-level purge now deletes the field and advances the generation in one single-key Lua step (LUA_PURGE_FIELD), mirroring the full-purge bump, while preserving purge()'s deletion-result return. Over-invalidating sibling fields of the same key is safe: it only costs cache misses, never staleness. Covered by a regression test. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
1 parent
de42073a96
commit
e02fa05594
2 files changed
+35
-1
No files matched your search
@@ -55,6 +55,20 @@ class Redis implements Adapter, Leasable, Retryable
|
||||
return removed
|
||||
LUA;
|
||||
|
||||
/**
|
||||
* Delete a single $hash field and advance the key's generation in one step,
|
||||
* so a field-level purge invalidates in-flight leases just like a full purge.
|
||||
* Returns the number of fields removed, preserving purge()'s deletion-result
|
||||
* semantics. Single-key. KEYS[1]=key; ARGV[1]=field.
|
||||
*/
|
||||
private const LUA_PURGE_FIELD = <<<'LUA'
|
||||
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)
|
||||
return removed
|
||||
LUA;
|
||||
|
||||
/**
|
||||
* @var Client
|
||||
*/
|
||||
@@ -293,7 +307,11 @@ class Redis implements Adapter, Leasable, Retryable
|
||||
return false;
|
||||
}
|
||||
|
||||
return (bool) $this->execute(fn () => $this->redis->hdel($key, $hash));
|
||||
return (bool) $this->execute(fn () => $this->redis->eval(
|
||||
self::LUA_PURGE_FIELD,
|
||||
[$key, $hash],
|
||||
1
|
||||
));
|
||||
}
|
||||
|
||||
// Drop the value fields and advance the in-hash generation in one atomic
|
||||
|
||||
@@ -41,6 +41,22 @@ class LeasableTest extends TestCase
|
||||
$this->assertNotSame($before, $this->cache->getGeneration('doc:1'));
|
||||
}
|
||||
|
||||
public function testHashPurgeAdvancesGeneration(): void
|
||||
{
|
||||
$captured = $this->cache->getGeneration('doc:1');
|
||||
$this->assertNotFalse($this->cache->saveWithLease('doc:1', ['v' => 1], 'field-a', $captured));
|
||||
|
||||
// A field-level purge must advance the per-key generation, just like a
|
||||
// full purge, or the lease is bypassed for hashed entries.
|
||||
$this->assertTrue($this->cache->purge('doc:1', 'field-a'));
|
||||
$this->assertNotSame($captured, $this->cache->getGeneration('doc:1'));
|
||||
|
||||
// A reader holding the pre-purge generation can no longer re-cache a
|
||||
// stale field via saveWithLease.
|
||||
$this->assertFalse($this->cache->saveWithLease('doc:1', ['stale' => true], 'field-a', $captured));
|
||||
$this->assertFalse($this->cache->load('doc:1', 60, 'field-a'));
|
||||
}
|
||||
|
||||
/**
|
||||
* The core read-after-write guarantee: a reader that captured the generation
|
||||
* BEFORE a concurrent purge must NOT be able to re-cache its now-stale value
|
||||
|
||||
Reference in new issue
Block a user