Skip to content

Commit 7eef08a

Browse files
committed
Add cache to AllowedOriginRepository
1 parent c87204a commit 7eef08a

File tree

1 file changed

+25
-1
lines changed

1 file changed

+25
-1
lines changed

src/Repositories/AllowedOriginRepository.php

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ public function getTableName(): string
2121
public function set(string $clientId, array $origins): void
2222
{
2323
$this->delete($clientId);
24+
$this->clearCache($origins);
2425

2526
$origins = array_unique(array_filter(array_values($origins)));
2627

@@ -65,11 +66,34 @@ public function get(string $clientId): array
6566

6667
public function has(string $origin): bool
6768
{
69+
// We only cache this method since it is used in authentication flow.
70+
$has = $this->protocolCache?->get(null, $this->getCacheKey($origin));
71+
72+
if ($has !== null) {
73+
return (bool) $has;
74+
}
75+
6876
$stmt = $this->database->read(
6977
"SELECT origin FROM {$this->getTableName()} WHERE origin = :origin LIMIT 1",
7078
['origin' => $origin],
7179
);
7280

73-
return (bool) count($stmt->fetchAll(PDO::FETCH_COLUMN, 0));
81+
$has = (bool) count($stmt->fetchAll(PDO::FETCH_COLUMN, 0));
82+
83+
$this->protocolCache?->set(
84+
$has,
85+
$this->moduleConfig->getProtocolClientEntityCacheDuration(),
86+
$this->getCacheKey($origin),
87+
);
88+
89+
return $has;
90+
}
91+
92+
protected function clearCache(array $origins): void
93+
{
94+
/** @var string $origin */
95+
foreach ($origins as $origin) {
96+
$this->protocolCache?->delete($this->getCacheKey($origin));
97+
}
7498
}
7599
}

0 commit comments

Comments
 (0)