fix(tags performance): Cache tag counts on server and load them using initial state

... and only fetch file lists when the corresponding tag is visible

Signed-off-by: Marcel Klehr <mklehr@gmx.net>
This commit is contained in:
Marcel Klehr 2023-01-25 15:58:49 +01:00 committed by backportbot-nextcloud[bot]
parent 731abc641c
commit 7325f1e5d4
4 changed files with 60 additions and 6 deletions

View File

@ -29,6 +29,7 @@ use OCA\DAV\Events\SabrePluginAuthInitEvent;
use OCA\Photos\Listener\SabrePluginAuthInitListener;
use OCA\DAV\Connector\Sabre\Principal;
use OCA\Photos\Listener\NodeDeletedListener;
use OCA\Photos\Listener\TagListener;
use OCA\Photos\Listener\GroupUserRemovedListener;
use OCA\Photos\Listener\GroupDeletedListener;
use OCP\AppFramework\App;
@ -36,6 +37,7 @@ use OCP\AppFramework\Bootstrap\IBootContext;
use OCP\AppFramework\Bootstrap\IBootstrap;
use OCP\AppFramework\Bootstrap\IRegistrationContext;
use OCP\Files\Events\Node\NodeDeletedEvent;
use OCP\SystemTag\MapperEvent;
use OCP\Group\Events\UserRemovedEvent;
use OCP\Group\Events\GroupDeletedEvent;
@ -69,7 +71,7 @@ class Application extends App implements IBootstrap {
public function register(IRegistrationContext $context): void {
/** Register $principalBackend for the DAV collection */
$context->registerServiceAlias('principalBackend', Principal::class);
$context->registerEventListener(NodeDeletedEvent::class, NodeDeletedListener::class);
$context->registerEventListener(UserRemovedEvent::class, GroupUserRemovedListener::class);
@ -77,6 +79,9 @@ class Application extends App implements IBootstrap {
$context->registerEventListener(GroupDeletedEvent::class, GroupDeletedListener::class);
$context->registerEventListener(SabrePluginAuthInitEvent::class, SabrePluginAuthInitListener::class);
$context->registerEventListener(MapperEvent::EVENT_ASSIGN, TagListener::class);
$context->registerEventListener(MapperEvent::EVENT_UNASSIGN, TagListener::class);
}
public function boot(IBootContext $context): void {

View File

@ -146,7 +146,7 @@ class PageController extends Controller {
$this->initialState->provideInitialState('nomedia-paths', $paths);
$key = $user->getUID() . ':' . $userFolder->getEtag();
$key = $user->getUID();
$tagCounts = $this->tagCountsCache->get($key);
if ($tagCounts === null) {
$tags = $this->tagManager->getAllTags(true);

View File

@ -0,0 +1,44 @@
<?php
namespace OCA\Photos\Listener;
use OCP\EventDispatcher\Event;
use OCP\Files\Config\ICachedMountInfo;
use OCP\Files\Config\IUserMountCache;
use OCP\Files\IRootFolder;
use OCP\ICacheFactory;
use OCP\SystemTag\MapperEvent;
class TagListener implements \OCP\EventDispatcher\IEventListener {
private \OCP\ICache $tagCountsCache;
private IRootFolder $rootFolder;
private IUserMountCache $userMountCache;
public function __construct(ICacheFactory $cacheFactory, IRootFolder $rootFolder, IUserMountCache $userMountCache) {
$this->tagCountsCache = $cacheFactory->createLocal('photos:tag-counts');
$this->rootFolder = $rootFolder;
$this->userMountCache = $userMountCache;
}
/**
* @inheritDoc
*/
public function handle(Event $event): void {
if ($event instanceof MapperEvent) {
if ($event->getObjectType() !== 'files') {
return;
}
$node = current($this->rootFolder->getById((int)$event->getObjectId()));
if (!$node) {
return;
}
$mounts = $this->userMountCache->getMountsForRootId($node->getMountPoint()->getStorageRootId());
$userIds = array_map(static function (ICachedMountInfo $mount) {
return $mount->getUser()->getUID();
}, $mounts);
foreach ($userIds as $userId) {
$this->tagCountsCache->remove($userId);
}
}
}
}

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<files psalm-version="4.26.0@6998fabb2bf528b65777bf9941920888d23c03ac">
<files psalm-version="4.29.0@7ec5ffbd5f68ae03782d7fd33fff0c45a69f95b3">
<file src="lib/Controller/AlbumsController.php">
<UndefinedClass occurrences="2">
<code>SharedStorage</code>
@ -7,7 +7,10 @@
</UndefinedClass>
</file>
<file src="lib/Controller/PageController.php">
<UndefinedClass occurrences="10">
<InvalidArgument occurrences="1">
<code>new SearchQuery(new SearchComparison(ISearchComparison::COMPARE_EQUAL, 'systemtag', $tag-&gt;getName()), 0, 0, [], $user)</code>
</InvalidArgument>
<UndefinedClass occurrences="12">
<code>$e</code>
<code>LoadSidebar</code>
<code>LoadSidebar</code>
@ -17,13 +20,15 @@
<code>SearchBinaryOperator</code>
<code>SearchComparison</code>
<code>SearchComparison</code>
<code>SearchComparison</code>
<code>SearchQuery</code>
<code>SearchQuery</code>
</UndefinedClass>
</file>
<file src="lib/Controller/PublicAlbumController.php">
<UndefinedClass occurrences="10">
<UndefinedClass occurrences="2">
<code>LoadViewer</code>
<code>LoadViewer</code>
</UndefinedClass>
</file>
</files>
</files>