From 1a540a163f509db640c8d0dd1afb594c45a26612 Mon Sep 17 00:00:00 2001 From: Kyrian Obikwelu Date: Fri, 27 Jun 2025 09:34:38 +0100 Subject: [PATCH] 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 --- src/Session/CacheSessionHandler.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/Session/CacheSessionHandler.php b/src/Session/CacheSessionHandler.php index abfda22..e4f1f7c 100644 --- a/src/Session/CacheSessionHandler.php +++ b/src/Session/CacheSessionHandler.php @@ -27,10 +27,20 @@ public function __construct( public function read(string $sessionId): string|false { $session = $this->cache->get($sessionId, false); - if ($session === false || !isset($this->sessionIndex[$sessionId])) { + if ($session === false) { + if (isset($this->sessionIndex[$sessionId])) { + unset($this->sessionIndex[$sessionId]); + $this->cache->set(self::SESSION_INDEX_KEY, $this->sessionIndex); + } return false; } + if (!isset($this->sessionIndex[$sessionId])) { + $this->sessionIndex[$sessionId] = $this->clock->now()->getTimestamp(); + $this->cache->set(self::SESSION_INDEX_KEY, $this->sessionIndex); + return $session; + } + if ($this->clock->now()->getTimestamp() - $this->sessionIndex[$sessionId] > $this->ttl) { $this->cache->delete($sessionId); return false;