diff options
author | provokateurin <kate@provokateurin.de> | 2025-05-20 13:00:16 +0200 |
---|---|---|
committer | provokateurin <kate@provokateurin.de> | 2025-05-20 13:00:16 +0200 |
commit | b991c0e21c4b96a7758950ba991dfbecb9471af9 (patch) | |
tree | ae889da66d682611ae9caa951d65aea00596d866 | |
parent | 565d524cd4b95e24469f9d0c105cd72dddbdd757 (diff) | |
download | nextcloud-server-perf/files/cache-garbage-collection-background-job.tar.gz nextcloud-server-perf/files/cache-garbage-collection-background-job.zip |
perf(files): Move cache garbage collection to a background jobperf/files/cache-garbage-collection-background-job
Signed-off-by: provokateurin <kate@provokateurin.de>
-rw-r--r-- | apps/files/appinfo/info.xml | 1 | ||||
-rw-r--r-- | apps/files/composer/composer/autoload_classmap.php | 1 | ||||
-rw-r--r-- | apps/files/composer/composer/autoload_static.php | 1 | ||||
-rw-r--r-- | apps/files/lib/BackgroundJob/CacheGarbageCollection.php | 48 | ||||
-rw-r--r-- | lib/base.php | 17 | ||||
-rw-r--r-- | lib/private/Cache/File.php | 18 |
6 files changed, 63 insertions, 23 deletions
diff --git a/apps/files/appinfo/info.xml b/apps/files/appinfo/info.xml index aedcd5b7ed5..ecd0fb884ee 100644 --- a/apps/files/appinfo/info.xml +++ b/apps/files/appinfo/info.xml @@ -33,6 +33,7 @@ <job>OCA\Files\BackgroundJob\CleanupFileLocks</job> <job>OCA\Files\BackgroundJob\CleanupDirectEditingTokens</job> <job>OCA\Files\BackgroundJob\DeleteExpiredOpenLocalEditor</job> + <job>OCA\Files\BackgroundJob\CacheGarbageCollection</job> </background-jobs> <commands> diff --git a/apps/files/composer/composer/autoload_classmap.php b/apps/files/composer/composer/autoload_classmap.php index 070cb46de38..da141839074 100644 --- a/apps/files/composer/composer/autoload_classmap.php +++ b/apps/files/composer/composer/autoload_classmap.php @@ -19,6 +19,7 @@ return array( 'OCA\\Files\\AdvancedCapabilities' => $baseDir . '/../lib/AdvancedCapabilities.php', 'OCA\\Files\\App' => $baseDir . '/../lib/App.php', 'OCA\\Files\\AppInfo\\Application' => $baseDir . '/../lib/AppInfo/Application.php', + 'OCA\\Files\\BackgroundJob\\CacheGarbageCollection' => $baseDir . '/../lib/BackgroundJob/CacheGarbageCollection.php', 'OCA\\Files\\BackgroundJob\\CleanupDirectEditingTokens' => $baseDir . '/../lib/BackgroundJob/CleanupDirectEditingTokens.php', 'OCA\\Files\\BackgroundJob\\CleanupFileLocks' => $baseDir . '/../lib/BackgroundJob/CleanupFileLocks.php', 'OCA\\Files\\BackgroundJob\\DeleteExpiredOpenLocalEditor' => $baseDir . '/../lib/BackgroundJob/DeleteExpiredOpenLocalEditor.php', diff --git a/apps/files/composer/composer/autoload_static.php b/apps/files/composer/composer/autoload_static.php index ce79d370e7c..a326d4cb7d2 100644 --- a/apps/files/composer/composer/autoload_static.php +++ b/apps/files/composer/composer/autoload_static.php @@ -34,6 +34,7 @@ class ComposerStaticInitFiles 'OCA\\Files\\AdvancedCapabilities' => __DIR__ . '/..' . '/../lib/AdvancedCapabilities.php', 'OCA\\Files\\App' => __DIR__ . '/..' . '/../lib/App.php', 'OCA\\Files\\AppInfo\\Application' => __DIR__ . '/..' . '/../lib/AppInfo/Application.php', + 'OCA\\Files\\BackgroundJob\\CacheGarbageCollection' => __DIR__ . '/..' . '/../lib/BackgroundJob/CacheGarbageCollection.php', 'OCA\\Files\\BackgroundJob\\CleanupDirectEditingTokens' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanupDirectEditingTokens.php', 'OCA\\Files\\BackgroundJob\\CleanupFileLocks' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanupFileLocks.php', 'OCA\\Files\\BackgroundJob\\DeleteExpiredOpenLocalEditor' => __DIR__ . '/..' . '/../lib/BackgroundJob/DeleteExpiredOpenLocalEditor.php', diff --git a/apps/files/lib/BackgroundJob/CacheGarbageCollection.php b/apps/files/lib/BackgroundJob/CacheGarbageCollection.php new file mode 100644 index 00000000000..3d0c7f5a72a --- /dev/null +++ b/apps/files/lib/BackgroundJob/CacheGarbageCollection.php @@ -0,0 +1,48 @@ +<?php + +declare(strict_types=1); +/** + * SPDX-FileCopyrightText: 2025 Nextcloud GmbH and Nextcloud contributors + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +namespace OCA\Files\BackgroundJob; + +use Exception; +use OC\Cache\File; +use OCP\AppFramework\Utility\ITimeFactory; +use OCP\BackgroundJob\TimedJob; +use OCP\IUser; +use OCP\IUserManager; +use Psr\Log\LoggerInterface; + +class CacheGarbageCollection extends TimedJob { + public function __construct( + ITimeFactory $time, + private IUserManager $userManager, + private LoggerInterface $logger, + ) { + parent::__construct($time); + + $this->setTimeSensitivity(self::TIME_INSENSITIVE); + $this->setInterval(24 * 60 * 60); + } + + protected function run(mixed $argument): void { + $cache = new File(); + + $this->userManager->callForSeenUsers(function (IUser $user) use ($cache): void { + $userId = $user->getUID(); + + try { + $cache->gc($userId); + } catch (Exception $e) { + $this->logger->warning('Exception when running cache gc.', [ + 'app' => 'files', + 'user' => $userId, + 'exception' => $e, + ]); + } + }); + } +} diff --git a/lib/base.php b/lib/base.php index ad80df357c5..92306ab6296 100644 --- a/lib/base.php +++ b/lib/base.php @@ -889,23 +889,6 @@ class OC { $throttler = Server::get(IThrottler::class); $throttler->resetDelay($request->getRemoteAddress(), 'login', ['user' => $uid]); } - - try { - $cache = new \OC\Cache\File(); - $cache->gc(); - } catch (\OC\ServerNotAvailableException $e) { - // not a GC exception, pass it on - throw $e; - } catch (\OC\ForbiddenException $e) { - // filesystem blocked for this request, ignore - } catch (\Exception $e) { - // a GC exception should not prevent users from using OC, - // so log the exception - Server::get(LoggerInterface::class)->warning('Exception when running cache gc.', [ - 'app' => 'core', - 'exception' => $e, - ]); - } }); } } diff --git a/lib/private/Cache/File.php b/lib/private/Cache/File.php index ceddf472ebd..91c19d437e3 100644 --- a/lib/private/Cache/File.php +++ b/lib/private/Cache/File.php @@ -26,14 +26,20 @@ class File implements ICache { * @throws \OC\ForbiddenException * @throws \OC\User\NoUserException */ - protected function getStorage() { + protected function getStorage(?string $userId = null) { if ($this->storage !== null) { return $this->storage; } - $session = Server::get(IUserSession::class); - if ($session->isLoggedIn()) { + + if ($userId === null) { + $session = Server::get(IUserSession::class); + if ($session->isLoggedIn()) { + $userId = $session->getUser()->getUID(); + } + } + + if ($userId !== null) { $rootView = new View(); - $userId = $session->getUser()->getUID(); Filesystem::initMountPoints($userId); if (!$rootView->file_exists('/' . $userId . '/cache')) { $rootView->mkdir('/' . $userId . '/cache'); @@ -154,8 +160,8 @@ class File implements ICache { * Runs GC * @throws \OC\ForbiddenException */ - public function gc() { - $storage = $this->getStorage(); + public function gc(?string $userId = null) { + $storage = $this->getStorage($userId); if ($storage) { // extra hour safety, in case of stray part chunks that take longer to write, // because touch() is only called after the chunk was finished |