Skip to content

Commit 8a29c69

Browse files
author
darkdarin
committed
feat: choose your serialization format by select Serializer
1 parent 2a2028b commit 8a29c69

File tree

6 files changed

+68
-4
lines changed

6 files changed

+68
-4
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"psr/simple-cache": "^3.0"
2020
},
2121
"require-dev": {
22+
"ext-igbinary": "*",
2223
"roave/security-advisories": "dev-latest"
2324
},
2425
"provide": {

src/ArrayFileCache.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,25 @@ class ArrayFileCache implements CacheInterface
99
private ?array $data = null;
1010
private string $cachePath;
1111
private string $cacheName;
12+
private SerializerInterface $serializer;
1213

13-
public function __construct(string $cachePath, string $cacheName)
14+
public function __construct(string $cachePath, string $cacheName, ?SerializerInterface $serializer = null)
1415
{
1516
$this->cachePath = $cachePath;
1617
$this->cacheName = $cacheName;
18+
if ($serializer === null) {
19+
$this->serializer = new DefaultSerializer();
20+
} else {
21+
$this->serializer = $serializer;
22+
}
1723
}
1824

1925
protected function getData(): array
2026
{
2127
if ($this->data === null) {
2228
$filePath = $this->getCacheFilePath();
2329
if (file_exists($filePath)) {
24-
$this->data = require $filePath;
30+
$this->data = $this->serializer->unserialize($filePath);
2531
} else {
2632
$this->data = [];
2733
}
@@ -37,8 +43,7 @@ protected function saveData(array $data): bool
3743
}
3844

3945
$this->data = $data;
40-
41-
return file_put_contents($this->getCacheFilePath(), '<?php return ' . var_export($data, true) . ';' . PHP_EOL);
46+
return $this->serializer->serialize($data, $this->getCacheFilePath());
4247
}
4348

4449
public function get(string $key, mixed $default = null): mixed

src/DefaultSerializer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tochka\Cache;
4+
5+
class DefaultSerializer implements SerializerInterface
6+
{
7+
public function serialize(mixed $data, string $fileName): bool
8+
{
9+
return file_put_contents($fileName, serialize($data));
10+
}
11+
12+
public function unserialize(string $fileName): mixed
13+
{
14+
return unserialize(file_get_contents($fileName));
15+
}
16+
}

src/IgBinarySerializer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tochka\Cache;
4+
5+
class IgBinarySerializer implements SerializerInterface
6+
{
7+
public function serialize(mixed $data, string $fileName): bool
8+
{
9+
return file_put_contents($fileName, igbinary_serialize($data));
10+
}
11+
12+
public function unserialize(string $fileName): mixed
13+
{
14+
return igbinary_unserialize(file_get_contents($fileName));
15+
}
16+
}

src/SerializerInterface.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace Tochka\Cache;
4+
5+
interface SerializerInterface
6+
{
7+
public function serialize(mixed $data, string $fileName): bool;
8+
9+
public function unserialize(string $fileName): mixed;
10+
}

src/VarExportSerializer.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Tochka\Cache;
4+
5+
class VarExportSerializer implements SerializerInterface
6+
{
7+
public function serialize(mixed $data, string $fileName): bool
8+
{
9+
return file_put_contents($fileName, '<?php return ' . var_export($data, true) . ';' . PHP_EOL);
10+
}
11+
12+
public function unserialize(string $fileName): mixed
13+
{
14+
return require $fileName;
15+
}
16+
}

0 commit comments

Comments
 (0)