Skip to content

Commit

Permalink
FIX Less cache fragmentation in ClassManifest (#11533)
Browse files Browse the repository at this point in the history
  • Loading branch information
lekoala authored Jan 16, 2025
1 parent 16ef094 commit 227e178
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions src/Core/Manifest/ClassManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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');
Expand All @@ -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'];
Expand Down Expand Up @@ -698,7 +714,8 @@ public function handleFile($basename, $pathname, $includeTests)
'traits' => $traits,
'enums' => $enums,
];
$this->cache->set($key, $cache);

$this->filesCache[$key] = $cache;
}
}

Expand Down

0 comments on commit 227e178

Please sign in to comment.