Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FEAT Less cache fragmentation in ClassManifest #11533

Open
wants to merge 3 commits into
base: 5.3
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 23 additions & 2 deletions src/Core/Manifest/ClassManifest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ class ClassManifest
*/
protected $cacheKey;

/**
* In memory cache array for individually parsed files
* @var array|null
*/
protected ?array $filesCache = null;

/**
* Key to use for files cache
*
* @var string
*/
protected string $filesCacheKey;

/**
* Array of properties to cache
*
Expand Down Expand Up @@ -203,6 +216,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 +577,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 +602,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 @@ -696,8 +715,10 @@ public function handleFile($basename, $pathname, $includeTests)
'classes' => $classes,
'interfaces' => $interfaces,
'traits' => $traits,
'enums' => $enums,
];
$this->cache->set($key, $cache);

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

Expand Down
Loading