|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Bankiru\Api\Doctrine\Cache; |
|
4
|
|
|
|
|
5
|
|
|
use Bankiru\Api\Doctrine\EntityDataCacheInterface; |
|
6
|
|
|
use Bankiru\Api\Doctrine\Mapping\ApiMetadata; |
|
7
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
|
8
|
|
|
|
|
9
|
|
|
class ApiEntityCache implements EntityDataCacheInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** @var CacheItemPoolInterface */ |
|
12
|
|
|
private $cache; |
|
13
|
|
|
/** @var ApiMetadata */ |
|
14
|
|
|
private $metadata; |
|
15
|
|
|
/** @var CacheConfigurationInterface */ |
|
16
|
|
|
private $configuration; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* ApiEntityCache constructor. |
|
20
|
|
|
* |
|
21
|
|
|
* @param CacheItemPoolInterface $cache |
|
22
|
|
|
* @param ApiMetadata $metadata |
|
23
|
|
|
* @param CacheConfigurationInterface $configuration |
|
24
|
|
|
*/ |
|
25
|
1 |
|
public function __construct( |
|
26
|
|
|
CacheItemPoolInterface $cache, |
|
27
|
|
|
ApiMetadata $metadata, |
|
28
|
|
|
CacheConfigurationInterface $configuration |
|
29
|
|
|
) { |
|
30
|
1 |
|
$this->cache = $cache; |
|
31
|
1 |
|
$this->metadata = $metadata; |
|
32
|
1 |
|
$this->configuration = $configuration; |
|
33
|
1 |
|
} |
|
34
|
|
|
|
|
35
|
|
|
/** {@inheritdoc} */ |
|
36
|
1 |
|
public function get(array $identifier) |
|
37
|
|
|
{ |
|
38
|
1 |
|
if (!$this->configuration->isEnabled()) { |
|
39
|
|
|
return null; |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
1 |
|
return $this->cache->getItem($this->getKey($identifier))->get(); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
/** {@inheritdoc} */ |
|
46
|
1 |
|
public function set(array $identifier, $data) |
|
47
|
|
|
{ |
|
48
|
1 |
|
if (!$this->configuration->isEnabled()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$this->cache->save( |
|
53
|
1 |
|
$this->cache |
|
54
|
1 |
|
->getItem($this->getKey($identifier)) |
|
55
|
1 |
|
->set($data) |
|
56
|
1 |
|
->expiresAfter($this->configuration->getTtl()) |
|
57
|
1 |
|
); |
|
58
|
1 |
|
} |
|
59
|
|
|
|
|
60
|
|
|
/** {@inheritdoc} */ |
|
61
|
1 |
|
public function getMetadata() |
|
62
|
|
|
{ |
|
63
|
1 |
|
return $this->metadata; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** {@inheritdoc} */ |
|
67
|
|
|
public function getConfiguration() |
|
68
|
|
|
{ |
|
69
|
|
|
return $this->configuration; |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* @param array $identifier |
|
75
|
|
|
* |
|
76
|
|
|
* @return string |
|
77
|
|
|
*/ |
|
78
|
1 |
|
private function getKey(array $identifier) |
|
79
|
|
|
{ |
|
80
|
1 |
|
return $this->configuration->getStrategy()->getEntityKey($this->metadata, $identifier); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Clears the cache for given entity identifier |
|
85
|
|
|
* |
|
86
|
|
|
* @param array $identifier |
|
87
|
|
|
* |
|
88
|
|
|
* @return void |
|
89
|
|
|
*/ |
|
90
|
|
|
public function clear(array $identifier) |
|
91
|
|
|
{ |
|
92
|
|
|
$this->cache->deleteItem($this->getKey($identifier)); |
|
93
|
|
|
} |
|
94
|
|
|
} |
|
95
|
|
|
|