Skip to content

Commit abc3162

Browse files
feat(cache): Introduce cache version storage (#69)
Co-authored-by: Oskar Stark <[email protected]>
1 parent 006cdab commit abc3162

File tree

4 files changed

+121
-4
lines changed

4 files changed

+121
-4
lines changed
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of storyblok/php-content-api-client.
7+
*
8+
* (c) Storyblok GmbH <[email protected]>
9+
* in cooperation with SensioLabs Deutschland <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Storyblok\Api\CacheVersion;
16+
17+
/**
18+
* @experimental
19+
*
20+
* Interface for managing cache version storage.
21+
*
22+
* This interface defines methods to get and set the cache version.
23+
* If the cache version is not set, it should return null.
24+
*/
25+
interface CacheVersionStorageInterface
26+
{
27+
/**
28+
* @return null|int returns int if cache version is set, null if not set
29+
*/
30+
public function get(): ?int;
31+
32+
/**
33+
* @param null|int $version Sets the cache version. If null the cache version is unset.
34+
*/
35+
public function set(?int $version): void;
36+
}

src/CacheVersion/InMemoryStorage.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of storyblok/php-content-api-client.
7+
*
8+
* (c) Storyblok GmbH <[email protected]>
9+
* in cooperation with SensioLabs Deutschland <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Storyblok\Api\CacheVersion;
16+
17+
/**
18+
* @experimental
19+
*/
20+
final class InMemoryStorage implements CacheVersionStorageInterface
21+
{
22+
private ?int $cacheVersion = null;
23+
24+
public function get(): ?int
25+
{
26+
return $this->cacheVersion;
27+
}
28+
29+
public function set(?int $version): void
30+
{
31+
$this->cacheVersion = $version;
32+
}
33+
}

src/StoryblokClient.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
use Psr\Log\LoggerInterface;
1919
use Psr\Log\NullLogger;
2020
use Storyblok\Api\Bridge\HttpClient\QueryStringHelper;
21+
use Storyblok\Api\CacheVersion\CacheVersionStorageInterface;
22+
use Storyblok\Api\CacheVersion\InMemoryStorage;
2123
use Symfony\Component\HttpClient\HttpClient;
2224
use Symfony\Contracts\HttpClient\HttpClientInterface;
2325
use Symfony\Contracts\HttpClient\ResponseInterface;
@@ -30,14 +32,14 @@
3032
final class StoryblokClient implements StoryblokClientInterface
3133
{
3234
private HttpClientInterface $client;
33-
private ?int $cacheVersion = null;
3435

3536
public function __construct(
3637
string $baseUri,
3738
#[\SensitiveParameter]
3839
private string $token,
3940
int $timeout = 4,
4041
private LoggerInterface $logger = new NullLogger(),
42+
private CacheVersionStorageInterface $storage = new InMemoryStorage(),
4143
) {
4244
$this->client = HttpClient::createForBaseUri($baseUri);
4345
$this->token = TrimmedNonEmptyString::fromString($token, '$token must not be an empty string')->toString();
@@ -72,13 +74,13 @@ public function request(string $method, string $url, array $options = []): Respo
7274
$url = QueryStringHelper::applyQueryString($url, [
7375
...$options['query'],
7476
'token' => $this->token,
75-
'cv' => $this->cacheVersion,
77+
'cv' => $this->storage->get(),
7678
]);
7779
unset($options['query']);
7880
} else {
7981
$options['query'] = [
8082
'token' => $this->token,
81-
'cv' => $this->cacheVersion,
83+
'cv' => $this->storage->get(),
8284
];
8385
}
8486

@@ -110,7 +112,7 @@ public function request(string $method, string $url, array $options = []): Respo
110112
$parsedUrl = parse_url($response->getInfo('url'), \PHP_URL_QUERY);
111113
parse_str($parsedUrl, $parsed);
112114

113-
$this->cacheVersion = \array_key_exists('cv', $parsed) ? (int) $parsed['cv'] : $this->cacheVersion;
115+
$this->storage->set(\array_key_exists('cv', $parsed) ? (int) $parsed['cv'] : $this->storage->get());
114116
}
115117

116118
return $response;
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
/**
6+
* This file is part of storyblok/php-content-api-client.
7+
*
8+
* (c) Storyblok GmbH <[email protected]>
9+
* in cooperation with SensioLabs Deutschland <[email protected]>
10+
*
11+
* For the full copyright and license information, please view the LICENSE
12+
* file that was distributed with this source code.
13+
*/
14+
15+
namespace Storyblok\Api\Tests\Unit\CacheVersion;
16+
17+
use PHPUnit\Framework\Attributes\DataProvider;
18+
use PHPUnit\Framework\Attributes\Test;
19+
use PHPUnit\Framework\TestCase;
20+
use Storyblok\Api\CacheVersion\InMemoryStorage;
21+
22+
/**
23+
* @author Silas Joisten <[email protected]>
24+
*/
25+
final class InMemoryStorageTest extends TestCase
26+
{
27+
#[DataProvider('values')]
28+
#[Test]
29+
public function setAndGetWithValues(?int $value): void
30+
{
31+
$storage = new InMemoryStorage();
32+
$storage->set($value);
33+
34+
self::assertSame($value, $storage->get());
35+
}
36+
37+
/**
38+
* @return iterable<array<int, null|int>>
39+
*/
40+
public static function values(): iterable
41+
{
42+
yield [12];
43+
yield [0];
44+
yield [null];
45+
}
46+
}

0 commit comments

Comments
 (0)