Skip to content

Commit 570052b

Browse files
qdequippeSpomky
andauthored
Add caching to UrlKeySetFactory (#550)
Implemented caching in the UrlKeySetFactory to store and retrieve URL data. This is accomplished by adding the PSR Cache package to the composer.json file and incorporating it with the CacheItemPoolInterface in the UrlKeySetFactory class. This addition intends to improve performance by storing data from previous URL requests and reusing them, if available, instead of making repeated URL requests. Co-authored-by: Florent Morselli <[email protected]>
1 parent c755aaa commit 570052b

File tree

4 files changed

+35
-3
lines changed

4 files changed

+35
-3
lines changed

composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
"brick/math": "^0.9|^0.10|^0.11|^0.12",
5656
"paragonie/constant_time_encoding": "^2.6",
5757
"paragonie/sodium_compat": "^1.20",
58+
"psr/cache": "^3.0",
5859
"psr/clock": "^1.0",
5960
"psr/event-dispatcher": "^1.0",
6061
"psr/http-client": "^1.0",

phpstan-baseline.neon

+5
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,11 @@ parameters:
20432043
count: 1
20442044
path: src/Library/KeyManagement/KeyConverter/RSAKey.php
20452045

2046+
-
2047+
message: "#^Method Jose\\\\Component\\\\KeyManagement\\\\UrlKeySetFactory\\:\\:getContent\\(\\) should return string but returns mixed\\.$#"
2048+
count: 1
2049+
path: src/Library/KeyManagement/UrlKeySetFactory.php
2050+
20462051
-
20472052
message: "#^Cannot cast mixed to string\\.$#"
20482053
count: 1

src/Library/KeyManagement/UrlKeySetFactory.php

+28-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
namespace Jose\Component\KeyManagement;
66

7+
use Psr\Cache\CacheItemPoolInterface;
78
use Psr\Http\Client\ClientInterface;
89
use Psr\Http\Message\RequestFactoryInterface;
910
use RuntimeException;
11+
use Symfony\Component\Cache\Adapter\NullAdapter;
1012
use Symfony\Contracts\HttpClient\HttpClientInterface;
1113
use function assert;
1214

@@ -15,6 +17,10 @@
1517
*/
1618
abstract class UrlKeySetFactory
1719
{
20+
private CacheItemPoolInterface $cacheItemPool;
21+
22+
private int $expiresAfter = 3600;
23+
1824
public function __construct(
1925
private readonly ClientInterface|HttpClientInterface $client,
2026
private readonly null|RequestFactoryInterface $requestFactory = null
@@ -35,17 +41,36 @@ public function __construct(
3541
ClientInterface::class
3642
));
3743
}
44+
$this->cacheItemPool = new NullAdapter();
45+
}
46+
47+
public function enabledCache(CacheItemPoolInterface $cacheItemPool, int $expiresAfter = 3600): void
48+
{
49+
$this->cacheItemPool = $cacheItemPool;
50+
$this->expiresAfter = $expiresAfter;
3851
}
3952

4053
/**
4154
* @param array<string, string|string[]> $header
4255
*/
4356
protected function getContent(string $url, array $header = []): string
4457
{
45-
if ($this->client instanceof HttpClientInterface) {
46-
return $this->sendSymfonyRequest($url, $header);
58+
$cacheKey = hash('xxh128', $url);
59+
$item = $this->cacheItemPool->getItem($cacheKey);
60+
if ($item->isHit()) {
61+
return $item->get();
4762
}
48-
return $this->sendPsrRequest($url, $header);
63+
64+
$content = $this->client instanceof HttpClientInterface ? $this->sendSymfonyRequest(
65+
$url,
66+
$header
67+
) : $this->sendPsrRequest($url, $header);
68+
$item = $this->cacheItemPool->getItem($cacheKey);
69+
$item->expiresAfter($this->expiresAfter);
70+
$item->set($content);
71+
$this->cacheItemPool->save($item);
72+
73+
return $content;
4974
}
5075

5176
/**

src/Library/composer.json

+1
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
"brick/math": "^0.9|^0.10|^0.11|^0.12",
4545
"paragonie/constant_time_encoding": "^2.6",
4646
"paragonie/sodium_compat": "^1.20",
47+
"psr/cache": "^3.0",
4748
"psr/clock": "^1.0",
4849
"psr/http-factory": "^1.0",
4950
"psr/http-client": "^1.0",

0 commit comments

Comments
 (0)