Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

perf(sharing): Cache the list of tokens per user #11547

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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');
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should go into CachePrefix as a const

$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
Loading