Skip to content

Commit

Permalink
perf(sharing): Cache the list of tokens per user
Browse files Browse the repository at this point in the history
Signed-off-by: Joas Schilling <coding@schilljs.com>
  • Loading branch information
nickvergessen committed Feb 8, 2024
1 parent e7563ee commit 1847df7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/Manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -55,6 +56,7 @@
class Manager {

protected ICommentsManager $commentsManager;
protected ICache $cache;

public function __construct(
protected IDBConnection $db,
Expand All @@ -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 {
Expand Down Expand Up @@ -430,13 +434,20 @@ public function removeUserFromAllRooms(IUser $user, bool $privateOnly = false):
$roomService->setReadOnly($room, Room::READ_ONLY);
}
}

$this->cache->remove($user->getUID());
}

/**
* @param string $userId
* @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')
Expand All @@ -456,6 +467,8 @@ public function getRoomTokensForUser(string $userId): array {
}
$result->closeCursor();

$this->cache->set($userId, $roomTokens, 300);

return $roomTokens;
}

Expand Down
5 changes: 5 additions & 0 deletions lib/Service/ParticipantService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -92,6 +93,7 @@ class ParticipantService {
protected array $actorCache;
/** @var array<int, array<string, Participant>> */
protected array $sessionCache;
protected ICache $cache;

public function __construct(
protected IConfig $serverConfig,
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 3 additions & 1 deletion tests/php/Controller/SignalingControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down
4 changes: 3 additions & 1 deletion tests/php/Recording/BackendNotifierTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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),
);
}

Expand Down

0 comments on commit 1847df7

Please sign in to comment.