Skip to content

Commit 99d6d12

Browse files
authored
Cache serialization (#226)
1 parent bceeebc commit 99d6d12

File tree

7 files changed

+59
-7
lines changed

7 files changed

+59
-7
lines changed

‎src/Flow/ETL/Cache.php‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@
22

33
namespace Flow\ETL;
44

5-
interface Cache
5+
use Flow\Serializer\Serializable;
6+
7+
/**
8+
* @template T
9+
* @extends Serializable<T>
10+
*/
11+
interface Cache extends Serializable
612
{
713
public function add(string $id, Rows $rows) : void;
814

‎src/Flow/ETL/Cache/InMemoryCache.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,25 @@
77
use Flow\ETL\Cache;
88
use Flow\ETL\Rows;
99

10+
/**
11+
* @implements Cache<array<mixed>>
12+
*/
1013
final class InMemoryCache implements Cache
1114
{
1215
/**
1316
* @var array<string, array<Rows>>
1417
*/
1518
private array $cache = [];
1619

20+
public function __serialize() : array
21+
{
22+
return [];
23+
}
24+
25+
public function __unserialize(array $data) : void
26+
{
27+
}
28+
1729
public function add(string $id, Rows $rows) : void
1830
{
1931
if (!\array_key_exists($id, $this->cache)) {

‎src/Flow/ETL/Cache/LocalFilesystemCache.php‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use Flow\Serializer\Serializer;
1111

1212
/**
13+
* @implements Cache<array{path: string, serializer: Serializer}>
1314
* @infection-ignore-all
1415
*/
1516
final class LocalFilesystemCache implements Cache
@@ -23,6 +24,20 @@ public function __construct(
2324
}
2425
}
2526

27+
public function __serialize() : array
28+
{
29+
return [
30+
'path' => $this->path,
31+
'serializer' => $this->serializer,
32+
];
33+
}
34+
35+
public function __unserialize(array $data) : void
36+
{
37+
$this->path = $data['path'];
38+
$this->serializer = $data['serializer'];
39+
}
40+
2641
public function add(string $id, Rows $rows) : void
2742
{
2843
$cacheStream = \fopen($this->cachePath($id), 'a');

‎src/Flow/ETL/ConfigBuilder.php‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
use Flow\ETL\ExternalSort\MemorySort;
1010
use Flow\ETL\Monitoring\Memory\Unit;
1111
use Flow\Serializer\CompressingSerializer;
12-
use Flow\Serializer\NativePHPSerializer;
1312
use Flow\Serializer\Serializer;
1413

1514
final class ConfigBuilder
@@ -39,7 +38,7 @@ public function __construct()
3938
public function build() : Config
4039
{
4140
$this->id ??= \uniqid('flow_php');
42-
$this->serializer ??= new CompressingSerializer(new NativePHPSerializer());
41+
$this->serializer ??= new CompressingSerializer();
4342
$this->cache ??= new LocalFilesystemCache(
4443
\is_string(\getenv(Config::CACHE_DIR_ENV))
4544
? \getenv(Config::CACHE_DIR_ENV)

‎src/Flow/Serializer/CompressingSerializer.php‎

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,14 @@
88

99
final class CompressingSerializer implements Serializer
1010
{
11-
public function __construct(
12-
private readonly Serializer $serializer,
13-
private readonly int $compressionLevel = 9
14-
) {
11+
private readonly int $compressionLevel;
12+
13+
private readonly Serializer $serializer;
14+
15+
public function __construct()
16+
{
17+
$this->serializer = new NativePHPSerializer();
18+
$this->compressionLevel = 9;
1519
}
1620

1721
public function serialize(Serializable $serializable) : string

‎src/Flow/Serializer/NativePHPSerializer.php‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@
88

99
final class NativePHPSerializer implements Serializer
1010
{
11+
public function __construct()
12+
{
13+
}
14+
1115
public function serialize(Serializable $serializable) : string
1216
{
1317
return \serialize($serializable);

‎tests/Flow/ETL/Tests/Double/CacheSpy.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
use Flow\ETL\Cache;
88
use Flow\ETL\Rows;
99

10+
/**
11+
* @implements Cache<array<mixed>>
12+
*/
1013
final class CacheSpy implements Cache
1114
{
1215
/**
@@ -28,6 +31,15 @@ public function __construct(private readonly Cache $cache)
2831
{
2932
}
3033

34+
public function __serialize() : array
35+
{
36+
return [];
37+
}
38+
39+
public function __unserialize(array $data) : void
40+
{
41+
}
42+
3143
public function add(string $id, Rows $rows) : void
3244
{
3345
if (!\array_key_exists($id, $this->writes)) {

0 commit comments

Comments
 (0)