Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
perf(Caching): Compute the global prefix without relying on AppConfig
Signed-off-by: provokateurin <[email protected]>
  • Loading branch information
provokateurin committed Jul 14, 2025
commit 8b136208c75f12832848033b98a1eef0f1a2864d
16 changes: 10 additions & 6 deletions lib/private/Memcache/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class Factory implements ICacheFactory {
private IProfiler $profiler;

/**
* @param Closure $globalPrefixClosure
* @param Closure(self) $globalPrefixClosure
* @param LoggerInterface $logger
* @param ?class-string<ICache> $localCacheClass
* @param ?class-string<ICache> $distributedCacheClass
Expand Down Expand Up @@ -110,7 +110,7 @@ public function __construct(

private function getGlobalPrefix(): ?string {
if (is_null($this->globalPrefix)) {
$this->globalPrefix = ($this->globalPrefixClosure)();
$this->globalPrefix = ($this->globalPrefixClosure)($this);
}
return $this->globalPrefix;
}
Expand Down Expand Up @@ -175,10 +175,14 @@ public function createDistributed(string $prefix = ''): ICache {
* @param string $prefix
* @return ICache
*/
public function createLocal(string $prefix = ''): ICache {
$globalPrefix = $this->getGlobalPrefix();
if (is_null($globalPrefix)) {
return new ArrayCache($prefix);
public function createLocal(string $prefix = '', bool $disableGlobalPrefix = false): ICache {
if ($disableGlobalPrefix) {
$globalPrefix = '';
} else {
$globalPrefix = $this->getGlobalPrefix();
if (is_null($globalPrefix)) {
return new ArrayCache($prefix);
}
}

assert($this->localCacheClass !== null);
Expand Down
46 changes: 29 additions & 17 deletions lib/private/Server.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use OC\App\AppManager;
use OC\App\AppStore\Bundles\BundleFetcher;
use OC\App\AppStore\Fetcher\AppFetcher;
use OC\App\InfoParser;
use OC\AppFramework\Bootstrap\Coordinator;
use OC\AppFramework\Http\Request;
use OC\AppFramework\Http\RequestId;
Expand Down Expand Up @@ -163,7 +164,6 @@
use OCP\Files\Template\ITemplateManager;
use OCP\FilesMetadata\IFilesMetadataManager;
use OCP\FullTextSearch\IFullTextSearchManager;
use OCP\GlobalScale\IConfig;
use OCP\Group\ISubAdmin;
use OCP\Http\Client\IClientService;
use OCP\IAppConfig;
Expand Down Expand Up @@ -602,24 +602,36 @@ public function __construct($webRoot, \OC\Config $config) {
$serverVersion = $c->get(ServerVersion::class);

if ($config->getValue('installed', false) && !(defined('PHPUNIT_RUN') && PHPUNIT_RUN)) {
$logQuery = $config->getValue('log_query');
$prefixClosure = function () use ($logQuery, $serverVersion): ?string {
if (!$logQuery) {
try {
$v = \OCP\Server::get(IAppConfig::class)->getAppInstalledVersions(true);
} catch (\Doctrine\DBAL\Exception $e) {
// Database service probably unavailable
// Probably related to https://github.com/nextcloud/server/issues/37424
return null;
$prefixClosure = function (Factory $factory) use ($serverVersion): string {
// It is impossible to rely on OC_App or AppManager at this point, as they rely on the cache itself.
$appInfoFiles = [];
foreach (\OC::$APPSROOTS as $apps_dir) {
if (!is_readable($apps_dir['path'])) {
continue;
}

$dh = opendir($apps_dir['path']);
if (!is_resource($dh)) {
continue;
}

while (($file = readdir($dh)) !== false) {
if (
$file[0] !== '.'
&& is_dir($apps_dir['path'] . '/' . $file)
&& is_file($apps_dir['path'] . '/' . $file . '/appinfo/info.xml')
) {
$appInfoFiles[$file] = $apps_dir['path'] . '/' . $file . '/appinfo/info.xml';
}
}
} else {
// If the log_query is enabled, we can not get the app versions
// as that does a query, which will be logged and the logging
// depends on redis and here we are back again in the same function.
$v = [
'log_query' => 'enabled',
];
}

// We must disable the global prefix here, in order to compute the global prefix in this closure and avoid infinite recursion.
$prefixlessCache = $factory->createLocal(disableGlobalPrefix: true);

$infoParser = new InfoParser($prefixlessCache);

$v = array_map(static fn ($file): string => (string)$infoParser->parse($file)['version'], $appInfoFiles);
$v['core'] = implode(',', $serverVersion->getVersion());
$version = implode(',', array_keys($v)) . implode(',', $v);
$instanceId = \OC_Util::getInstanceId();
Expand Down