From 51a3da3677c107a9d10451cc824e17925664da1d Mon Sep 17 00:00:00 2001 From: Crypta Eve Date: Fri, 24 Jan 2025 23:57:18 +1030 Subject: [PATCH] fix: handle corrupt files in cache (#100) --- src/Cache/FileCache.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/Cache/FileCache.php b/src/Cache/FileCache.php index a027439..a2e02e7 100644 --- a/src/Cache/FileCache.php +++ b/src/Cache/FileCache.php @@ -142,13 +142,14 @@ public function get(string $key, mixed $default = null): mixed return $default; // Get the data from the file and unserialize it - $data = unserialize(file_get_contents($cache_file_path)); + $data = @unserialize(file_get_contents($cache_file_path)); if ($data === false) return $default; // If the cached entry is expired and does not have any ETag, remove it. - if ($data->expired() && ! $data->hasHeader('ETag')) { + // The false case occur if a cache save is incomplete so the unserialize fails. + if ($data === false || $data->expired() && ! $data->hasHeader('ETag')) { $this->delete($key); return $default;