From 1847df7279fe2f007a2d484964904f1e8dd3a563 Mon Sep 17 00:00:00 2001 From: Joas Schilling Date: Wed, 7 Feb 2024 12:45:39 +0100 Subject: [PATCH] perf(sharing): Cache the list of tokens per user Signed-off-by: Joas Schilling --- lib/Manager.php | 13 +++++++++++++ lib/Service/ParticipantService.php | 5 +++++ tests/php/Controller/SignalingControllerTest.php | 4 +++- tests/php/Recording/BackendNotifierTest.php | 4 +++- 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/lib/Manager.php b/lib/Manager.php index 581f8fcca53..35f2dfd2d73 100644 --- a/lib/Manager.php +++ b/lib/Manager.php @@ -42,6 +42,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\IEventDispatcher; use OCP\ICache; +use OCP\ICacheFactory; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroupManager; @@ -55,6 +56,7 @@ class Manager { protected ICommentsManager $commentsManager; + protected ICache $cache; public function __construct( protected IDBConnection $db, @@ -73,8 +75,10 @@ public function __construct( protected ITimeFactory $timeFactory, protected IHasher $hasher, protected IL10N $l, + ICacheFactory $cacheFactory, ) { $this->commentsManager = $commentsManager; + $this->cache = $cacheFactory->createDistributed('talk/usertokens'); } public function forAllRooms(callable $callback): void { @@ -430,6 +434,8 @@ public function removeUserFromAllRooms(IUser $user, bool $privateOnly = false): $roomService->setReadOnly($room, Room::READ_ONLY); } } + + $this->cache->remove($user->getUID()); } /** @@ -437,6 +443,11 @@ public function removeUserFromAllRooms(IUser $user, bool $privateOnly = false): * @return string[] */ public function getRoomTokensForUser(string $userId): array { + $cachedTokens = $this->cache->get($userId); + if ($cachedTokens !== null) { + return $cachedTokens; + } + $query = $this->db->getQueryBuilder(); $query->select('r.token') ->from('talk_attendees', 'a') @@ -456,6 +467,8 @@ public function getRoomTokensForUser(string $userId): array { } $result->closeCursor(); + $this->cache->set($userId, $roomTokens, 300); + return $roomTokens; } diff --git a/lib/Service/ParticipantService.php b/lib/Service/ParticipantService.php index 5a455b981e1..1e84442c7e1 100644 --- a/lib/Service/ParticipantService.php +++ b/lib/Service/ParticipantService.php @@ -76,6 +76,7 @@ use OCP\DB\QueryBuilder\IQueryBuilder; use OCP\EventDispatcher\IEventDispatcher; use OCP\Federation\ICloudIdManager; +use OCP\ICache; use OCP\ICacheFactory; use OCP\IConfig; use OCP\IDBConnection; @@ -92,6 +93,7 @@ class ParticipantService { protected array $actorCache; /** @var array> */ protected array $sessionCache; + protected ICache $cache; public function __construct( protected IConfig $serverConfig, @@ -512,6 +514,9 @@ public function addUsers(Room $room, array $participants, ?IUser $addedBy = null $this->attendeeMapper->delete($attendee); throw new CannotReachRemoteException(); } + } elseif ($attendee->getActorType() === Attendee::ACTOR_USERS) { + $cache = $this->cacheFactory->createDistributed('talk/usertokens'); + $cache->remove($attendee->getActorId()); } } catch (Exception $e) { if ($e->getReason() !== Exception::REASON_UNIQUE_CONSTRAINT_VIOLATION) { diff --git a/tests/php/Controller/SignalingControllerTest.php b/tests/php/Controller/SignalingControllerTest.php index 6118636504d..53a22644d26 100644 --- a/tests/php/Controller/SignalingControllerTest.php +++ b/tests/php/Controller/SignalingControllerTest.php @@ -44,6 +44,7 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; use OCP\Http\Client\IClientService; +use OCP\ICacheFactory; use OCP\IConfig; use OCP\IDBConnection; use OCP\IGroupManager; @@ -998,7 +999,8 @@ public function testLeaveRoomWithOldSession() { $dispatcher, $this->timeFactory, $this->createMock(IHasher::class), - $this->createMock(IL10N::class) + $this->createMock(IL10N::class), + $this->createMock(ICacheFactory::class), ); $this->recreateSignalingController(); diff --git a/tests/php/Recording/BackendNotifierTest.php b/tests/php/Recording/BackendNotifierTest.php index 56874dfb2e6..3c4cf2aa538 100644 --- a/tests/php/Recording/BackendNotifierTest.php +++ b/tests/php/Recording/BackendNotifierTest.php @@ -37,6 +37,7 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\EventDispatcher\IEventDispatcher; use OCP\Http\Client\IClientService; +use OCP\ICacheFactory; use OCP\IGroupManager; use OCP\IL10N; use OCP\IURLGenerator; @@ -130,7 +131,8 @@ public function setUp(): void { $dispatcher, $timeFactory, $this->createMock(IHasher::class), - $this->createMock(IL10N::class) + $this->createMock(IL10N::class), + $this->createMock(ICacheFactory::class), ); }