Merge pull request #52008 from nextcloud/fix/cache-hit-getFirstNodeById

fix: Proper order for checking path prefix for getting file by id from cache
pull/52050/head
Robin Appelman 2025-04-08 14:43:46 +07:00 committed by GitHub
commit 5cc942905a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 10 additions and 6 deletions

@ -384,13 +384,17 @@ class Root extends Folder implements IRootFolder {
// scope the cache by user, so we don't return nodes for different users
if ($this->user) {
$cachedPath = $this->pathByIdCache->get($this->user->getUID() . '::' . $id);
if ($cachedPath && str_starts_with($path, $cachedPath)) {
if ($cachedPath && str_starts_with($cachedPath, $path)) {
// getting the node by path is significantly cheaper than finding it by id
$node = $this->get($cachedPath);
// by validating that the cached path still has the requested fileid we can work around the need to invalidate the cached path
// if the cached path is invalid or a different file now we fall back to the uncached logic
if ($node && $node->getId() === $id) {
return $node;
try {
$node = $this->get($cachedPath);
// by validating that the cached path still has the requested fileid we can work around the need to invalidate the cached path
// if the cached path is invalid or a different file now we fall back to the uncached logic
if ($node && $node->getId() === $id) {
return $node;
}
} catch (NotFoundException|NotPermittedException) {
// The file may be moved but the old path still in cache
}
}
}