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

cache user single id into user prefs #1637

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/Api/v1/Circles.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ public static function joinedCircles($userId = '', $forceAll = false) {
$probe->includePersonalCircles($personalCircle);
$probe->filterHiddenCircles();

return $circleService->getCircles($probe);
return $circleService->probeCircles($probe);
}


Expand Down
18 changes: 18 additions & 0 deletions lib/Db/CircleRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,24 @@ public function getSingleCircle(IFederatedUser $initiator): Circle {
}
}

/**
* method that return the single-user Circle based on a FederatedUser.
*
* @param string $singleId
*
* @return Circle
* @throws SingleCircleNotFoundException
*/
public function getSingleCircleById(string $singleId): Circle {
$qb = $this->getCircleSelectSql(CoreQueryBuilder::SINGLE, true);
$qb->limitToUniqueId($singleId);

try {
return $this->getItemFromRequest($qb);
} catch (CircleNotFoundException $e) {
throw new SingleCircleNotFoundException();
}
}

/**
* @param Circle $circle
Expand Down
4 changes: 2 additions & 2 deletions lib/Db/CoreQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ class CoreQueryBuilder extends ExtendedQueryBuilder {
self::OPTIONS => [
'getData' => true
],
self::OWNER,
//self::OWNER,
self::MEMBERSHIPS => [
self::CONFIG
],
Expand Down Expand Up @@ -136,7 +136,7 @@ class CoreQueryBuilder extends ExtendedQueryBuilder {
]
],
self::INVITED_BY => [
self::OWNER,
//self::OWNER,
self::BASED_ON
]
],
Expand Down
45 changes: 32 additions & 13 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 @@ -1061,6 +1066,9 @@ private function getSingleCircle(FederatedUser $federatedUser, bool $generate =
}

$this->cacheSingleCircle($federatedUser, $singleCircle);
if ($federatedUser->getUserType() === Member::TYPE_USER) {
$this->config->setUserValue($federatedUser->getUserId(), 'circles', self::USERPREF_SINGLE_CIRCLE, $singleCircle->getSingleId());
}

return $singleCircle;
}
Expand Down Expand Up @@ -1257,27 +1265,38 @@ public function getGroupCircle(string $groupId): Circle {


/**
* if data is in cache (15m) we returns the circle generation from the json in cache
* if user prefs contains single id, we get details from the circle fom db
* if nothing else, means there is no cache
* @param FederatedUser $federatedUser
*
* @return Circle
* @throws SingleCircleNotFoundException
*/
private function getCachedSingleCircle(FederatedUser $federatedUser): Circle {
$key = $this->generateCacheKey($federatedUser);
$cachedData = $this->cache->get($key);

if (!is_string($cachedData)) {
throw new SingleCircleNotFoundException();
$cachedData = $this->cache->get($this->generateCacheKey($federatedUser));
try {
if (is_string($cachedData)) {
/** @var Circle $singleCircle */
$singleCircle = $this->deserializeJson($cachedData, Circle::class);
return $singleCircle;
}
} catch (InvalidItemException) {
}

try {
/** @var Circle $singleCircle */
$singleCircle = $this->deserializeJson($cachedData, Circle::class);
} catch (InvalidItemException $e) {
throw new SingleCircleNotFoundException();
if ($federatedUser->getUserType() === Member::TYPE_USER) {
$userSingleId = $this->config->getUserValue($federatedUser->getUserId(), 'circles', self::USERPREF_SINGLE_CIRCLE);
if ($userSingleId !== '') {
try {
$singleCircle = $this->circleRequest->getSingleCircleById($userSingleId);
$this->cacheSingleCircle($federatedUser, $singleCircle);
return $singleCircle;
} catch (CircleNotFoundException) {
}
}
}

return $singleCircle;
throw new SingleCircleNotFoundException();
}

/**
Expand Down
16 changes: 13 additions & 3 deletions lib/Tools/Db/ExtendedQueryBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
use OC;
use OC\DB\QueryBuilder\QueryBuilder;
use OC\SystemConfig;
use OCA\Circles\AppInfo\Application;
use OCA\Circles\Tools\Exceptions\DateTimeException;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\Exceptions\RowNotFoundException;
use OCA\Circles\Tools\Traits\TArrayTools;
use OCP\DB\QueryBuilder\ICompositeExpression;
use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\IAppConfig;
use OCP\IDBConnection;
use OCP\Server;
use Psr\Log\LoggerInterface;

class ExtendedQueryBuilder extends QueryBuilder {
Expand All @@ -36,13 +39,16 @@ class ExtendedQueryBuilder extends QueryBuilder {

/** @var array */
private $defaultValues = [];

private LoggerInterface $logger;
private IAppConfig $appConfig;

public function __construct() {
$this->logger = Server::get(LoggerInterface::class);
$this->appConfig = Server::get(IAppConfig::class);
parent::__construct(
OC::$server->get(IDBConnection::class),
OC::$server->get(SystemConfig::class),
OC::$server->get(LoggerInterface::class)
$this->logger
);
}

Expand Down Expand Up @@ -1023,6 +1029,7 @@ public function getRow(callable $method, string $object = '', array $params = []
*/
public function getRows(callable $method, string $object = '', array $params = []): array {
$rows = [];
$st = microtime(true);
$cursor = $this->execute();
while ($data = $cursor->fetch()) {
try {
Expand All @@ -1031,7 +1038,10 @@ public function getRows(callable $method, string $object = '', array $params = [
}
}
$cursor->closeCursor();

$spent = microtime(true) - $st;
if ($spent > $this->appConfig->getValueFloat(Application::APP_ID, 'slow_queries_time', 3)) { // in case request takes more than n seconds, we add a notice to logs
$this->logger->warning('Teams - slow request', ['exception' => new Exception('slow request'), 'spent' => $spent]);
}
return $rows;
}

Expand Down
7 changes: 6 additions & 1 deletion lib/Tools/Traits/TDeserialize.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace OCA\Circles\Tools\Traits;

use JsonException;
use JsonSerializable;
use OCA\Circles\Tools\Exceptions\InvalidItemException;
use OCA\Circles\Tools\IDeserializable;
Expand Down Expand Up @@ -110,7 +111,11 @@ public function deserializeList(string $json, string $class): array {
* @throws InvalidItemException
*/
public function deserializeJson(string $json, string $class): IDeserializable {
$data = json_decode($json, true);
try {
$data = json_decode($json, true, flags: JSON_THROW_ON_ERROR);
} catch (JsonException) {
throw new InvalidItemException('not json');
}

return $this->deserialize($data, $class);
}
Expand Down
Loading