Skip to content

Commit f33db1f

Browse files
fix: add self-healing for cache session index inconsistencies
- Auto-repair when session exists in cache but missing from index - Clean up index when session doesn't exist in cache - Prevents inconsistent state between session data and index
1 parent 65bcc7b commit f33db1f

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/Session/CacheSessionHandler.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,20 @@ public function __construct(
2727
public function read(string $sessionId): string|false
2828
{
2929
$session = $this->cache->get($sessionId, false);
30-
if ($session === false || !isset($this->sessionIndex[$sessionId])) {
30+
if ($session === false) {
31+
if (isset($this->sessionIndex[$sessionId])) {
32+
unset($this->sessionIndex[$sessionId]);
33+
$this->cache->set(self::SESSION_INDEX_KEY, $this->sessionIndex);
34+
}
3135
return false;
3236
}
3337

38+
if (!isset($this->sessionIndex[$sessionId])) {
39+
$this->sessionIndex[$sessionId] = $this->clock->now()->getTimestamp();
40+
$this->cache->set(self::SESSION_INDEX_KEY, $this->sessionIndex);
41+
return $session;
42+
}
43+
3444
if ($this->clock->now()->getTimestamp() - $this->sessionIndex[$sessionId] > $this->ttl) {
3545
$this->cache->delete($sessionId);
3646
return false;

0 commit comments

Comments
 (0)