Skip to content

Commit 6ab312e

Browse files
committed
Add cache to client repository
1 parent 9b04454 commit 6ab312e

File tree

5 files changed

+116
-33
lines changed

5 files changed

+116
-33
lines changed

config-templates/module_oidc.php

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -258,42 +258,46 @@
258258
// also give proper adapter arguments for its instantiation below.
259259
// @see https://symfony.com/doc/current/components/cache.html#available-cache-adapters
260260
ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER => null,
261-
//ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class,
262-
//ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER => \Symfony\Component\Cache\Adapter\MemcachedAdapter::class,
261+
// ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER => \Symfony\Component\Cache\Adapter\FilesystemAdapter::class,
262+
// ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER => \Symfony\Component\Cache\Adapter\MemcachedAdapter::class,
263263

264-
// Federation cache adapter arguments used for adapter instantiation. Refer to documentation for particular
264+
// Protocol cache adapter arguments used for adapter instantiation. Refer to documentation for particular
265265
// adapter on which arguments are needed to create its instance, in the order of constructor arguments.
266266
// See examples below.
267267
ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER_ARGUMENTS => [
268268
// Adapter arguments here...
269269
],
270270
// Example for FileSystemAdapter:
271-
//ModuleConfig::OPTION_FEDERATION_CACHE_ADAPTER_ARGUMENTS => [
272-
// 'openidFederation', // Namespace, subdirectory of main cache directory
273-
// 60 * 60 * 6, // Default lifetime in seconds (used when particular cache item doesn't define its own lifetime)
274-
// '/path/to/main/cache/directory' // Must be writable. Can be set to null to use system temporary directory.
275-
//],
276-
// Example for MemcachedAdapter:
277-
//ModuleConfig::OPTION_FEDERATION_CACHE_ADAPTER_ARGUMENTS => [
278-
// // First argument is a connection instance, so we can use the helper method to create it. In this example a
279-
// // single server is used. Refer to documentation on how to use multiple servers, and / or to provide other
280-
// // options.
281-
// \Symfony\Component\Cache\Adapter\MemcachedAdapter::createConnection(
282-
// 'memcached://localhost'
283-
// // the DSN can include config options (pass them as a query string):
284-
// // 'memcached://localhost:11222?retry_timeout=10'
285-
// // 'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2'
286-
// ),
287-
// 'openidFederation', // Namespace, key prefix.
288-
// 60 * 60 * 6, // Default lifetime in seconds (used when particular cache item doesn't define its own lifetime)
289-
//],
271+
// ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER_ARGUMENTS => [
272+
// 'openidFederation', // Namespace, subdirectory of main cache directory
273+
// 60 * 60 * 6, // Default lifetime in seconds (used when particular cache item doesn't define its own lifetime)
274+
// '/path/to/main/cache/directory' // Must be writable. Can be set to null to use system temporary directory.
275+
// ],
276+
// Example for MemcachedAdapter:
277+
// ModuleConfig::OPTION_PROTOCOL_CACHE_ADAPTER_ARGUMENTS => [
278+
// // First argument is a connection instance, so we can use the helper method to create it. In this example a
279+
// // single server is used. Refer to documentation on how to use multiple servers, and / or to provide other
280+
// // options.
281+
// \Symfony\Component\Cache\Adapter\MemcachedAdapter::createConnection(
282+
// 'memcached://localhost'
283+
// // the DSN can include config options (pass them as a query string):
284+
// // 'memcached://localhost:11222?retry_timeout=10'
285+
// // 'memcached://localhost:11222?socket_recv_size=1&socket_send_size=2'
286+
// ),
287+
// 'openidProtocol', // Namespace, key prefix.
288+
// 60 * 60 * 6, // Default lifetime in seconds (used when particular cache item doesn't define its own lifetime)
289+
// ],
290290

291+
/**
292+
* Protocol cache duration for particular entities. This is only relevant if protocol cache adapter is set up.
293+
* For duration format info, check https://www.php.net/manual/en/dateinterval.construct.php.
294+
*/
291295
// Cache duration for user entities (authenticated users data). If not set, cache duration will be the same as
292-
// session duration. This is used to avoid fetching user data from database on every authentication event.
293-
// This is only relevant if protocol cache adapter is set up. For duration format info, check
294-
// https://www.php.net/manual/en/dateinterval.construct.php.
296+
// session duration.
295297
// ModuleConfig::OPTION_PROTOCOL_USER_ENTITY_CACHE_DURATION => 'PT1H', // 1 hour
296-
ModuleConfig::OPTION_PROTOCOL_USER_ENTITY_CACHE_DURATION => null, // fallback to session duration
298+
ModuleConfig::OPTION_PROTOCOL_USER_ENTITY_CACHE_DURATION => null, // Fallback to session duration
299+
// Cache duration for client entities, with given default.
300+
ModuleConfig::OPTION_PROTOCOL_CLIENT_ENTITY_CACHE_DURATION => 'PT10M', // 10 minutes
297301

298302
/**
299303
* Cron related options.

src/ModuleConfig.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ class ModuleConfig
8080
final public const OPTION_PROTOCOL_CACHE_ADAPTER = 'protocol_cache_adapter';
8181
final public const OPTION_PROTOCOL_CACHE_ADAPTER_ARGUMENTS = 'protocol_cache_adapter_arguments';
8282
final public const OPTION_PROTOCOL_USER_ENTITY_CACHE_DURATION = 'protocol_user_entity_cache_duration';
83+
final public const OPTION_PROTOCOL_CLIENT_ENTITY_CACHE_DURATION = 'protocol_client_entity_cache_duration';
8384
final public const OPTION_FEDERATION_PARTICIPATION_LIMIT_BY_TRUST_MARKS =
8485
'federation_participation_limit_by_trust_marks';
8586

@@ -464,6 +465,21 @@ public function getProtocolUserEntityCacheDuration(): DateInterval
464465
);
465466
}
466467

468+
/**
469+
* Get cache duration for client entities (user data), with given default
470+
*
471+
* @throws \Exception
472+
*/
473+
public function getProtocolClientEntityCacheDuration(): DateInterval
474+
{
475+
return new DateInterval(
476+
$this->config()->getOptionalString(
477+
self::OPTION_PROTOCOL_CLIENT_ENTITY_CACHE_DURATION,
478+
null,
479+
) ?? 'PT10M',
480+
);
481+
}
482+
467483

468484
/*****************************************************************************************************************
469485
* OpenID Federation related config.

src/Repositories/AbstractDatabaseRepository.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,12 @@ public function __construct(
3232
) {
3333
}
3434

35+
public function getCacheKey(string $identifier): string
36+
{
37+
return is_string($tableName = $this->getTableName()) ?
38+
$tableName . '_' . $identifier :
39+
$identifier;
40+
}
41+
3542
abstract public function getTableName(): ?string;
3643
}

src/Repositories/ClientRepository.php

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,13 @@ public function validateClient($clientIdentifier, $clientSecret, $grantType): bo
9292
*/
9393
public function findById(string $clientIdentifier, ?string $owner = null): ?ClientEntityInterface
9494
{
95+
/** @var ?array $cachedState */
96+
$cachedState = $this->protocolCache?->get(null, $this->getCacheKey($clientIdentifier));
97+
98+
if (is_array($cachedState)) {
99+
return $this->clientEntityFactory->fromState($cachedState);
100+
}
101+
95102
/**
96103
* @var string $query
97104
* @var array $params
@@ -116,11 +123,26 @@ public function findById(string $clientIdentifier, ?string $owner = null): ?Clie
116123
return null;
117124
}
118125

119-
return $this->clientEntityFactory->fromState($row);
126+
$clientEntity = $this->clientEntityFactory->fromState($row);
127+
128+
$this->protocolCache?->set(
129+
$clientEntity->getState(),
130+
$this->moduleConfig->getProtocolClientEntityCacheDuration(),
131+
$this->getCacheKey($clientEntity->getIdentifier()),
132+
);
133+
134+
return $clientEntity;
120135
}
121136

122137
public function findByEntityIdentifier(string $entityIdentifier, ?string $owner = null): ?ClientEntityInterface
123138
{
139+
/** @var ?array $cachedState */
140+
$cachedState = $this->protocolCache?->get(null, $this->getCacheKey($entityIdentifier));
141+
142+
if (is_array($cachedState)) {
143+
return $this->clientEntityFactory->fromState($cachedState);
144+
}
145+
124146
/**
125147
* @var string $query
126148
* @var array $params
@@ -153,7 +175,15 @@ public function findByEntityIdentifier(string $entityIdentifier, ?string $owner
153175
return null;
154176
}
155177

156-
return $this->clientEntityFactory->fromState($row);
178+
$clientEntity = $this->clientEntityFactory->fromState($row);
179+
180+
$this->protocolCache?->set(
181+
$clientEntity->getState(),
182+
$this->moduleConfig->getProtocolClientEntityCacheDuration(),
183+
$this->getCacheKey($entityIdentifier),
184+
);
185+
186+
return $clientEntity;
157187
}
158188

159189
private function addOwnerWhereClause(string $query, array $params, ?string $owner = null): array
@@ -302,6 +332,19 @@ public function add(ClientEntityInterface $client): void
302332
$stmt,
303333
$client->getState(),
304334
);
335+
336+
$this->protocolCache?->set(
337+
$client->getState(),
338+
$this->moduleConfig->getProtocolClientEntityCacheDuration(),
339+
$this->getCacheKey($client->getIdentifier()),
340+
);
341+
if (($entityIdentifier = $client->getEntityIdentifier()) !== null) {
342+
$this->protocolCache?->set(
343+
$client->getState(),
344+
$this->moduleConfig->getProtocolClientEntityCacheDuration(),
345+
$this->getCacheKey($entityIdentifier),
346+
);
347+
}
305348
}
306349

307350
public function delete(ClientEntityInterface $client, ?string $owner = null): void
@@ -318,6 +361,11 @@ public function delete(ClientEntityInterface $client, ?string $owner = null): vo
318361
$owner,
319362
);
320363
$this->database->write($sqlQuery, $params);
364+
365+
$this->protocolCache?->delete($this->getCacheKey($client->getIdentifier()));
366+
if (($entityIdentifier = $client->getEntityIdentifier()) !== null) {
367+
$this->protocolCache?->delete($this->getCacheKey($entityIdentifier));
368+
}
321369
}
322370

323371
public function update(ClientEntityInterface $client, ?string $owner = null): void
@@ -366,6 +414,19 @@ public function update(ClientEntityInterface $client, ?string $owner = null): vo
366414
$sqlQuery,
367415
$params,
368416
);
417+
418+
$this->protocolCache?->set(
419+
$client->getState(),
420+
$this->moduleConfig->getProtocolClientEntityCacheDuration(),
421+
$this->getCacheKey($client->getIdentifier()),
422+
);
423+
if (($entityIdentifier = $client->getEntityIdentifier()) !== null) {
424+
$this->protocolCache?->set(
425+
$client->getState(),
426+
$this->moduleConfig->getProtocolClientEntityCacheDuration(),
427+
$this->getCacheKey($entityIdentifier),
428+
);
429+
}
369430
}
370431

371432
private function count(string $query, ?string $owner): int

src/Repositories/UserRepository.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,6 @@ public function getTableName(): string
4848
return $this->database->applyPrefix(self::TABLE_NAME);
4949
}
5050

51-
public function getCacheKey(string $identifier): string
52-
{
53-
return $this->getTableName() . '_' . $identifier;
54-
}
55-
5651
/**
5752
* @param string $identifier
5853
*

0 commit comments

Comments
 (0)