Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions src/InMemoryKvOps.php
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ public function pttl(string $key): int
return \max(0, $this->deadlines[$key] - $this->nowMs());
}

public function flush(): bool
{
$this->values = [];
$this->deadlines = [];
return true;
}

/**
* Resolve a key's value, expiring it lazily if its deadline has passed.
*/
Expand Down
11 changes: 11 additions & 0 deletions src/KvOpsInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,4 +73,15 @@ public function ttl(string $key): int;
* -2 if the key does not exist, otherwise the ms remaining.
*/
public function pttl(string $key): int;

/**
* Remove every key from the effective store. Backed by ephpm_kv_flush_all()
* (ePHPm v0.1.2+); a no-op returning false on older runtimes.
*
* Note: this is a *global* flush — it drops more than just session
* keys. KvSessionHandler deliberately does not expose this; the
* recommended way to invalidate every session is to bump the handler's
* prefix in config and let TTLs age the old entries out.
*/
public function flush(): bool;
}
8 changes: 8 additions & 0 deletions src/SapiKvOps.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,12 @@ public function pttl(string $key): int
{
return (int) \ephpm_kv_pttl($key);
}

public function flush(): bool
{
if (!\function_exists('ephpm_kv_flush_all')) {
return false;
}
return (bool) \ephpm_kv_flush_all();
}
}
11 changes: 11 additions & 0 deletions tests/InMemoryKvOpsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,15 @@ public function test_expire_with_zero_or_negative_is_noop(): void
// Original TTL unchanged.
self::assertGreaterThan(0, $ops->pttl('k'));
}

public function test_flush_clears_values_and_deadlines(): void
{
$ops = new InMemoryKvOps();
$ops->set('a', 'one');
$ops->set('b', 'two', 60);
self::assertTrue($ops->flush());
self::assertNull($ops->get('a'));
self::assertNull($ops->get('b'));
self::assertSame(-2, $ops->pttl('b'));
}
}
Loading