Skip to content

Commit

Permalink
perf: mise en cache de requêtes get users/me et datastore/$datastoreI…
Browse files Browse the repository at this point in the history
…d + nettoyage
  • Loading branch information
ocruze committed Dec 2, 2024
1 parent b44a7cd commit a50019d
Show file tree
Hide file tree
Showing 7 changed files with 28 additions and 1,270 deletions.
895 changes: 0 additions & 895 deletions json/getAllDetailed.json

This file was deleted.

273 changes: 0 additions & 273 deletions json/getAllWithFields.json

This file was deleted.

19 changes: 13 additions & 6 deletions src/Security/KeycloakUserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
use Symfony\Component\Security\Core\Exception\UserNotFoundException;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;

/**
* @implements UserProviderInterface<User>
Expand All @@ -32,6 +34,7 @@ public function __construct(
private ParameterBagInterface $params,
private UserApiService $userApiService,
private LoggerInterface $logger,
private CacheInterface $cache,
) {
}

Expand Down Expand Up @@ -78,12 +81,16 @@ public function loadUser(): User
throw new AuthenticationExpiredException('Failed to fetch user from token', Response::HTTP_UNAUTHORIZED, $th);
}

try {
$apiUser = $this->userApiService->getMe();
} catch (TimeoutException $ex) {
$this->logger->debug('{class}: Failed to fetch logged-in user', ['class' => self::class]);
throw new UserNotFoundException('Failed to fetch logged-in user', Response::HTTP_UNAUTHORIZED, $ex);
}
$apiUser = $this->cache->get('users-me', function (ItemInterface $item) {
$item->expiresAfter(60);

try {
return $this->userApiService->getMe();
} catch (TimeoutException $ex) {
$this->logger->debug('{class}: Failed to fetch logged-in user', ['class' => self::class]);
throw new UserNotFoundException('Failed to fetch logged-in user', Response::HTTP_UNAUTHORIZED, $ex);
}
});

$user = new User($keycloakUser->toArray(), $apiUser);

Expand Down
4 changes: 3 additions & 1 deletion src/Services/EntrepotApi/BaseEntrepotApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Symfony\Component\HttpClient\Exception\JsonException;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

Expand All @@ -20,7 +21,8 @@ public function __construct(
ParameterBagInterface $parameters,
Filesystem $filesystem,
RequestStack $requestStack,
LoggerInterface $logger
protected CacheInterface $cache,
LoggerInterface $logger,
) {
parent::__construct($httpClient, $parameters, $filesystem, $requestStack, $logger, 'api_entrepot_url');
}
Expand Down
7 changes: 6 additions & 1 deletion src/Services/EntrepotApi/DatastoreApiService.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@

use App\Exception\ApiException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\Cache\ItemInterface;

class DatastoreApiService extends BaseEntrepotApiService
{
public function get(string $datastoreId): array
{
return $this->request('GET', "datastores/$datastoreId");
return $this->cache->get("datastore-$datastoreId", function (ItemInterface $item) use ($datastoreId) {
$item->expiresAfter(60);

return $this->request('GET', "datastores/$datastoreId");
});
}

/**
Expand Down
Loading

0 comments on commit a50019d

Please sign in to comment.