-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSymfonyCacheHandler.php
More file actions
76 lines (60 loc) · 1.88 KB
/
SymfonyCacheHandler.php
File metadata and controls
76 lines (60 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<?php
declare(strict_types=1);
namespace OpenClassrooms\ServiceProxy\Handler\Impl\Cache;
use OpenClassrooms\ServiceProxy\Handler\Contract\CacheHandler;
use OpenClassrooms\ServiceProxy\Handler\Impl\ConfigurableHandler;
use Psr\Cache\CacheItemInterface;
use Symfony\Component\Cache\Adapter\TagAwareAdapterInterface;
final class SymfonyCacheHandler implements CacheHandler
{
use ConfigurableHandler;
/**
* @var iterable<string, TagAwareAdapterInterface>
*/
private iterable $pools;
/**
* @param iterable<string, TagAwareAdapterInterface> $pools
*/
public function __construct(iterable $pools = [])
{
$this->pools = $pools;
}
public function fetch(string $poolName, string $id): CacheItemInterface
{
$pool = $this->getPool($poolName);
return $pool->getItem($id);
}
public function save(string $poolName, string $id, $data, ?int $ttl = null, array $tags = []): void
{
$pool = $this->getPool($poolName);
$item = $pool->getItem($id)
->set($data)
->expiresAfter($ttl)
->tag($tags)
;
$pool->save($item);
}
public function invalidateTags(string $poolName, array $tags): void
{
$this->getPool($poolName)
->invalidateTags($tags);
}
public function getName(): string
{
return 'symfony_cache';
}
private function getPool(string $poolName): TagAwareAdapterInterface
{
$existingPools = [...$this->pools];
if (!isset($existingPools[$poolName])) {
throw new \InvalidArgumentException(
sprintf(
'No cache pool found for "%s". Available pools are: "%s".',
$poolName,
implode('", "', array_keys($existingPools))
)
);
}
return $existingPools[$poolName];
}
}