Skip to content

Commit 77c0e94

Browse files
committed
Add cache to AuthCodeRepository
1 parent 7eef08a commit 77c0e94

File tree

1 file changed

+46
-14
lines changed

1 file changed

+46
-14
lines changed

src/Repositories/AuthCodeRepository.php

Lines changed: 46 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
class AuthCodeRepository extends AbstractDatabaseRepository implements AuthCodeRepositoryInterface
3434
{
35+
final public const TABLE_NAME = 'oidc_auth_code';
36+
3537
public function __construct(
3638
ModuleConfig $moduleConfig,
3739
Database $database,
@@ -43,8 +45,6 @@ public function __construct(
4345
parent::__construct($moduleConfig, $database, $protocolCache);
4446
}
4547

46-
final public const TABLE_NAME = 'oidc_auth_code';
47-
4848
public function getTableName(): string
4949
{
5050
return $this->database->applyPrefix(self::TABLE_NAME);
@@ -79,6 +79,14 @@ public function persistNewAuthCode(OAuth2AuthCodeEntityInterface $authCodeEntity
7979
$stmt,
8080
$this->preparePdoState($authCodeEntity->getState()),
8181
);
82+
83+
$this->protocolCache?->set(
84+
$authCodeEntity->getState(),
85+
$this->helpers->dateTime()->getSecondsToExpirationTime(
86+
$authCodeEntity->getExpiryDateTime()->getTimestamp(),
87+
),
88+
$this->getCacheKey((string)$authCodeEntity->getIdentifier()),
89+
);
8290
}
8391

8492
/**
@@ -87,22 +95,38 @@ public function persistNewAuthCode(OAuth2AuthCodeEntityInterface $authCodeEntity
8795
*/
8896
public function findById(string $codeId): ?AuthCodeEntityInterface
8997
{
90-
$stmt = $this->database->read(
91-
"SELECT * FROM {$this->getTableName()} WHERE id = :id",
92-
[
93-
'id' => $codeId,
94-
],
95-
);
96-
97-
if (empty($rows = $stmt->fetchAll())) {
98-
return null;
98+
/** @var ?array $data */
99+
$data = $this->protocolCache?->get(null, $this->getCacheKey($codeId));
100+
101+
if (!is_array($data)) {
102+
$stmt = $this->database->read(
103+
"SELECT * FROM {$this->getTableName()} WHERE id = :id",
104+
[
105+
'id' => $codeId,
106+
],
107+
);
108+
109+
if (empty($rows = $stmt->fetchAll())) {
110+
return null;
111+
}
112+
113+
/** @var array $data */
114+
$data = current($rows);
99115
}
100116

101-
/** @var array $data */
102-
$data = current($rows);
103117
$data['client'] = $this->clientRepository->findById((string)$data['client_id']);
104118

105-
return $this->authCodeEntityFactory->fromState($data);
119+
$authCodeEntity = $this->authCodeEntityFactory->fromState($data);
120+
121+
$this->protocolCache?->set(
122+
$authCodeEntity->getState(),
123+
$this->helpers->dateTime()->getSecondsToExpirationTime(
124+
$authCodeEntity->getExpiryDateTime()->getTimestamp(),
125+
),
126+
$this->getCacheKey((string)$authCodeEntity->getIdentifier()),
127+
);
128+
129+
return $authCodeEntity;
106130
}
107131

108132
/**
@@ -177,6 +201,14 @@ private function update(AuthCodeEntity $authCodeEntity): void
177201
$stmt,
178202
$this->preparePdoState($authCodeEntity->getState()),
179203
);
204+
205+
$this->protocolCache?->set(
206+
$authCodeEntity->getState(),
207+
$this->helpers->dateTime()->getSecondsToExpirationTime(
208+
$authCodeEntity->getExpiryDateTime()->getTimestamp(),
209+
),
210+
$this->getCacheKey((string)$authCodeEntity->getIdentifier()),
211+
);
180212
}
181213

182214
protected function preparePdoState(array $state): array

0 commit comments

Comments
 (0)