Skip to content

Commit

Permalink
cache user single id into user prefs
Browse files Browse the repository at this point in the history
Signed-off-by: Maxence Lange <maxence@artificial-owl.com>
  • Loading branch information
ArtificialOwl committed Jul 18, 2024
1 parent c3811f5 commit c963f5c
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions lib/Service/FederatedUserService.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@
use OCA\Circles\Tools\Traits\TStringTools;
use OCP\ICache;
use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IGroupManager;
use OCP\IUser;
use OCP\IUserManager;
Expand All @@ -72,16 +73,18 @@ class FederatedUserService {
use TNCLogger;
use TDeserialize;


public const USERPREF_SINGLE_CIRCLE = 'single_circle';
public const CACHE_SINGLE_CIRCLE = 'circles/singleCircle';
public const CACHE_SINGLE_CIRCLE_TTL = 900;
public const CACHE_SINGLE_CIRCLE_TTL = 86400;

public const CONFLICT_001 = 1;
public const CONFLICT_002 = 2;
public const CONFLICT_003 = 3;
public const CONFLICT_004 = 4;
public const CONFLICT_005 = 5;

/** @var IConfig $config */
private $config;

/** @var IUserSession */
private $userSession;
Expand Down Expand Up @@ -163,6 +166,7 @@ class FederatedUserService {
* @param ConfigService $configService
*/
public function __construct(
IConfig $config,
IUserSession $userSession,
IUserManager $userManager,
IGroupManager $groupManager,
Expand All @@ -178,6 +182,7 @@ public function __construct(
InterfaceService $interfaceService,
ConfigService $configService
) {
$this->config = $config;
$this->userSession = $userSession;
$this->userManager = $userManager;
$this->groupManager = $groupManager;
Expand Down Expand Up @@ -1263,8 +1268,9 @@ public function getGroupCircle(string $groupId): Circle {
* @throws SingleCircleNotFoundException
*/
private function getCachedSingleCircle(FederatedUser $federatedUser): Circle {
$key = $this->generateCacheKey($federatedUser);
$cachedData = $this->cache->get($key);
$cachedData = ($federatedUser->getUserType() === Member::TYPE_USER) ?
$this->config->getUserValue($federatedUser->getUserId(), 'circles', self::USERPREF_SINGLE_CIRCLE)
: $this->cache->get($this->generateCacheKey($federatedUser));

if (!is_string($cachedData)) {
throw new SingleCircleNotFoundException();
Expand All @@ -1285,6 +1291,11 @@ private function getCachedSingleCircle(FederatedUser $federatedUser): Circle {
* @param Circle $singleCircle
*/
private function cacheSingleCircle(FederatedUser $federatedUser, Circle $singleCircle): void {
if ($federatedUser->getUserType() === Member::TYPE_USER) {
$this->config->setUserValue($federatedUser->getUserId(), 'circles', self::USERPREF_SINGLE_CIRCLE, json_encode($singleCircle));
return;
}

$key = $this->generateCacheKey($federatedUser);
$this->cache->set($key, json_encode($singleCircle), self::CACHE_SINGLE_CIRCLE_TTL);
}
Expand Down

0 comments on commit c963f5c

Please sign in to comment.