From 227e1788ba52de719b8cd6ab052fa04b1ada0da1 Mon Sep 17 00:00:00 2001 From: Thomas Portelange Date: Thu, 16 Jan 2025 20:43:37 +0100 Subject: [PATCH] FIX Less cache fragmentation in ClassManifest (#11533) --- src/Core/Manifest/ClassManifest.php | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/Core/Manifest/ClassManifest.php b/src/Core/Manifest/ClassManifest.php index e3898598c72..c1189f433d7 100644 --- a/src/Core/Manifest/ClassManifest.php +++ b/src/Core/Manifest/ClassManifest.php @@ -53,6 +53,16 @@ class ClassManifest */ protected $cacheKey; + /** + * In memory cache array for individually parsed files + */ + protected ?array $filesCache = null; + + /** + * Key to use for files cache + */ + protected string $filesCacheKey; + /** * Array of properties to cache * @@ -203,6 +213,7 @@ public function __construct($base, CacheFactory $cacheFactory = null) $this->base = $base; $this->cacheFactory = $cacheFactory; $this->cacheKey = 'manifest'; + $this->filesCacheKey = 'manifestFiles'; } private function buildCache($includeTests = false) @@ -563,6 +574,7 @@ public function regenerate($includeTests) if ($this->cache) { $data = $this->getState(); + $this->cache->set($this->filesCacheKey, $this->filesCache); $this->cache->set($this->cacheKey, $data); $this->cache->set('generated_at', time()); $this->cache->delete('regenerate'); @@ -587,11 +599,15 @@ public function handleFile($basename, $pathname, $includeTests) // since just using the datetime lead to problems with upgrading. $key = preg_replace('/[^a-zA-Z0-9_]/', '_', $basename ?? '') . '_' . md5_file($pathname ?? ''); + if ($this->cache && $this->filesCache === null) { + $this->filesCache = $this->cache->get($this->filesCacheKey); + } + // Attempt to load from cache // Note: $classes, $interfaces and $traits arrays have correct-case keys, not lowercase $changed = false; if ($this->cache - && ($data = $this->cache->get($key)) + && ($data = ($this->filesCache[$key] ?? null)) && $this->validateItemCache($data) ) { $classes = $data['classes']; @@ -698,7 +714,8 @@ public function handleFile($basename, $pathname, $includeTests) 'traits' => $traits, 'enums' => $enums, ]; - $this->cache->set($key, $cache); + + $this->filesCache[$key] = $cache; } }