From 4333c215cbc444f13fd07542a143e75f82609ac4 Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Thu, 21 Apr 2022 16:31:19 +0200 Subject: Cache display name This should saves some query in the share backend when displaying the owner and it's not important if the display name is 10 minutes outdated as it is very rare that this gets changed. Signed-off-by: Carl Schwan --- apps/files_sharing/lib/Cache.php | 40 +++++++++----------------------- apps/files_sharing/lib/SharedStorage.php | 3 ++- 2 files changed, 13 insertions(+), 30 deletions(-) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/Cache.php b/apps/files_sharing/lib/Cache.php index c49bebaef62..7290c33da94 100644 --- a/apps/files_sharing/lib/Cache.php +++ b/apps/files_sharing/lib/Cache.php @@ -33,6 +33,7 @@ use OC\Files\Cache\Wrapper\CacheJail; use OC\Files\Search\SearchBinaryOperator; use OC\Files\Search\SearchComparison; use OC\Files\Storage\Wrapper\Jail; +use OC\User\DisplayNameCache; use OCP\Files\Cache\ICacheEntry; use OCP\Files\Search\ISearchBinaryOperator; use OCP\Files\Search\ISearchComparison; @@ -47,27 +48,22 @@ use OCP\IUserManager; * don't use this class directly if you need to get metadata, use \OC\Files\Filesystem::getFileInfo instead */ class Cache extends CacheJail { - /** @var \OCA\Files_Sharing\SharedStorage */ + /** @var SharedStorage */ private $storage; - /** @var ICacheEntry */ - private $sourceRootInfo; - /** @var IUserManager */ - private $userManager; - - private $rootUnchanged = true; - - private $ownerDisplayName; - - private $numericId; + private ICacheEntry $sourceRootInfo; + private bool $rootUnchanged = true; + private ?string $ownerDisplayName = null; + private int $numericId; + private DisplayNameCache $displayNameCache; /** - * @param \OCA\Files_Sharing\SharedStorage $storage + * @param SharedStorage $storage */ - public function __construct($storage, ICacheEntry $sourceRootInfo, IUserManager $userManager) { + public function __construct($storage, ICacheEntry $sourceRootInfo, DisplayNameCache $displayNameCache) { $this->storage = $storage; $this->sourceRootInfo = $sourceRootInfo; - $this->userManager = $userManager; $this->numericId = $sourceRootInfo->getStorageId(); + $this->displayNameCache = $displayNameCache; parent::__construct( null, @@ -173,22 +169,8 @@ class Cache extends CacheJail { private function getOwnerDisplayName() { if (!$this->ownerDisplayName) { - /** @var ICacheFactory $cacheFactory */ - $cacheFactory = \OC::$server->get(ICacheFactory::class); - $memcache = $cacheFactory->createLocal('share_owner_name'); $uid = $this->storage->getOwner(''); - $cached = $memcache->get($uid); - if ($cached) { - $this->ownerDisplayName = $cached; - } else { - $user = $this->userManager->get($uid); - if ($user) { - $this->ownerDisplayName = $user->getDisplayName(); - } else { - $this->ownerDisplayName = $uid; - } - $memcache->set($uid, $this->ownerDisplayName, 60 * 60); - } + $this->ownerDisplayName = $this->displayNameCache->getDisplayName($uid); } return $this->ownerDisplayName; } diff --git a/apps/files_sharing/lib/SharedStorage.php b/apps/files_sharing/lib/SharedStorage.php index 8d123e7b873..60a292c498d 100644 --- a/apps/files_sharing/lib/SharedStorage.php +++ b/apps/files_sharing/lib/SharedStorage.php @@ -38,6 +38,7 @@ use OC\Files\Cache\Watcher; use OC\Files\ObjectStore\HomeObjectStoreStorage; use OC\Files\Storage\Common; use OC\Files\Storage\Home; +use OC\User\DisplayNameCache; use OCP\Files\Folder; use OCP\Files\IHomeStorage; use OCP\Files\Node; @@ -416,7 +417,7 @@ class SharedStorage extends \OC\Files\Storage\Wrapper\Jail implements ISharedSto $this->cache = new \OCA\Files_Sharing\Cache( $storage, $sourceRoot, - \OC::$server->get(IUserManager::class) + \OC::$server->get(DisplayNameCache::class) ); return $this->cache; } -- cgit v1.2.3 From ed4c1e584f685d18f77b5eca95e3a82826e8be5d Mon Sep 17 00:00:00 2001 From: Carl Schwan Date: Fri, 22 Apr 2022 10:01:35 +0200 Subject: Update cache when display name change This improve the correctness of the data Signed-off-by: Carl Schwan --- apps/files_sharing/lib/AppInfo/Application.php | 3 +++ apps/files_sharing/lib/Cache.php | 2 +- lib/private/User/DisplayNameCache.php | 13 ++++++++++++- 3 files changed, 16 insertions(+), 2 deletions(-) (limited to 'apps/files_sharing/lib') diff --git a/apps/files_sharing/lib/AppInfo/Application.php b/apps/files_sharing/lib/AppInfo/Application.php index befd1532d02..2539247b561 100644 --- a/apps/files_sharing/lib/AppInfo/Application.php +++ b/apps/files_sharing/lib/AppInfo/Application.php @@ -30,6 +30,7 @@ namespace OCA\Files_Sharing\AppInfo; use OC\Share\Share; +use OC\User\DisplayNameCache; use OCA\Files_Sharing\Capabilities; use OCA\Files_Sharing\Event\BeforeTemplateRenderedEvent; use OCA\Files_Sharing\External\Manager; @@ -65,6 +66,7 @@ use OCP\IUserSession; use OCP\L10N\IFactory; use OCP\Share\Events\ShareCreatedEvent; use OCP\Share\IManager; +use OCP\User\Events\UserChangedEvent; use OCP\Util; use Psr\Container\ContainerInterface; use Symfony\Component\EventDispatcher\EventDispatcherInterface; @@ -98,6 +100,7 @@ class Application extends App implements IBootstrap { $context->registerCapability(Capabilities::class); $context->registerNotifierService(Notifier::class); + $context->registerEventListener(UserChangedEvent::class, DisplayNameCache::class); } public function boot(IBootContext $context): void { diff --git a/apps/files_sharing/lib/Cache.php b/apps/files_sharing/lib/Cache.php index 7290c33da94..9f11431008f 100644 --- a/apps/files_sharing/lib/Cache.php +++ b/apps/files_sharing/lib/Cache.php @@ -53,7 +53,7 @@ class Cache extends CacheJail { private ICacheEntry $sourceRootInfo; private bool $rootUnchanged = true; private ?string $ownerDisplayName = null; - private int $numericId; + private $numericId; private DisplayNameCache $displayNameCache; /** diff --git a/lib/private/User/DisplayNameCache.php b/lib/private/User/DisplayNameCache.php index f44cdac6e85..ed0c723ef37 100644 --- a/lib/private/User/DisplayNameCache.php +++ b/lib/private/User/DisplayNameCache.php @@ -23,9 +23,12 @@ declare(strict_types=1); namespace OC\User; +use OCP\EventDispatcher\Event; +use OCP\EventDispatcher\IEventListener; use OCP\ICache; use OCP\ICacheFactory; use OCP\IUserManager; +use OCP\User\Events\UserChangedEvent; /** * Class that cache the relation UserId -> Display name @@ -34,7 +37,7 @@ use OCP\IUserManager; * their preferences. It's generally not an issue if this data is slightly * outdated. */ -class DisplayNameCache { +class DisplayNameCache implements IEventListener { private ICache $internalCache; private IUserManager $userManager; @@ -63,4 +66,12 @@ class DisplayNameCache { public function clear(): void { $this->internalCache->clear(); } + + public function handle(Event $event): void { + if ($event instanceof UserChangedEvent && $event->getFeature() === 'displayName') { + $userId = $event->getUser()->getUID(); + $newDisplayName = $event->getValue(); + $this->internalCache->set($userId, $newDisplayName, 60 * 10); // 10 minutes + } + } } -- cgit v1.2.3