From e2e1f3e6cfb0dd9d99e20e9e545090dce7360f06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julius=20H=C3=A4rtl?= Date: Tue, 25 Jun 2024 18:06:46 +0200 Subject: [PATCH] perf: Cache workspace content for propfinds MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Julius Härtl --- lib/DAV/WorkspacePlugin.php | 43 +++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 19 deletions(-) diff --git a/lib/DAV/WorkspacePlugin.php b/lib/DAV/WorkspacePlugin.php index dbe6c19844..cfe965a2dd 100644 --- a/lib/DAV/WorkspacePlugin.php +++ b/lib/DAV/WorkspacePlugin.php @@ -14,9 +14,13 @@ use OCA\DAV\Files\FilesHome; use OCA\Text\AppInfo\Application; use OCA\Text\Service\WorkspaceService; +use OCP\Files\GenericFileException; use OCP\Files\IRootFolder; +use OCP\Files\NotPermittedException; use OCP\Files\StorageNotAvailableException; +use OCP\ICacheFactory; use OCP\IConfig; +use OCP\Lock\LockedException; use Sabre\DAV\INode; use Sabre\DAV\PropFind; use Sabre\DAV\Server; @@ -29,23 +33,13 @@ class WorkspacePlugin extends ServerPlugin { /** @var Server */ private $server; - /** @var WorkspaceService */ - private $workspaceService; - - /** @var IRootFolder */ - private $rootFolder; - - /** @var IConfig */ - private $config; - - /** @var string|null */ - private $userId; - - public function __construct(WorkspaceService $workspaceService, IRootFolder $rootFolder, IConfig $config, $userId) { - $this->workspaceService = $workspaceService; - $this->rootFolder = $rootFolder; - $this->config = $config; - $this->userId = $userId; + public function __construct( + private WorkspaceService $workspaceService, + private IRootFolder $rootFolder, + private ICacheFactory $cacheFactory, + private IConfig $config, + private ?string $userId + ) { } /** @@ -97,10 +91,21 @@ public function propFind(PropFind $propFind, INode $node) { // Only return the property for the parent node and ignore it for further in depth nodes $propFind->handle(self::WORKSPACE_PROPERTY, function () use ($file) { + $cachedContent = ''; if ($file instanceof File) { - return $file->getContent(); + $cache = $this->cacheFactory->createDistributed('text_workspace'); + $cacheKey = $file->getFileInfo()->getId() . '_' . $file->getFileInfo()->getEtag(); + if ($cachedContent = $cache->get($cacheKey)) { + return $cachedContent; + } + + try { + $cachedContent = $file->getContent(); + $cache->set($cacheKey, $cachedContent, 3600); + } catch (GenericFileException|NotPermittedException|LockedException $e) { + } } - return ''; + return $cachedContent; }); $propFind->handle(self::WORKSPACE_FILE_PROPERTY, function () use ($file) { if ($file instanceof File) {