|
1
|
|
|
<?php |
|
2
|
|
|
/* |
|
3
|
|
|
* This file is part of the StfalconStudioDoctrineRedisCacheBundle. |
|
4
|
|
|
* |
|
5
|
|
|
* (c) Stfalcon LLC <stfalcon.com> |
|
6
|
|
|
* |
|
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
8
|
|
|
* file that was distributed with this source code. |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
declare(strict_types=1); |
|
12
|
|
|
|
|
13
|
|
|
namespace StfalconStudio\DoctrineRedisCacheBundle\Cache; |
|
14
|
|
|
|
|
15
|
|
|
use Doctrine\Common\Cache\PredisCache as BasePredisCache; |
|
16
|
|
|
use Predis\ClientInterface; |
|
17
|
|
|
use StfalconStudio\DoctrineRedisCacheBundle\Service\Migration\MigrationVersionService; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* PredisCache. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Artem Henvald <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
class PredisCache extends BasePredisCache |
|
25
|
|
|
{ |
|
26
|
|
|
/** @var MigrationVersionService */ |
|
27
|
|
|
private $migrationVersionService; |
|
28
|
|
|
|
|
29
|
|
|
/** @var int */ |
|
30
|
|
|
private $defaultLifeTime; |
|
31
|
|
|
|
|
32
|
|
|
/** @var string|null */ |
|
33
|
|
|
private $lastMigrationVersion; |
|
34
|
|
|
|
|
35
|
|
|
/** |
|
36
|
|
|
* @param ClientInterface $client |
|
37
|
|
|
* @param MigrationVersionService $migrationVersionService |
|
38
|
|
|
* @param int $defaultLifeTime |
|
39
|
|
|
*/ |
|
40
|
|
|
public function __construct(ClientInterface $client, MigrationVersionService $migrationVersionService, $defaultLifeTime = 0) |
|
41
|
|
|
{ |
|
42
|
|
|
parent::__construct($client); |
|
43
|
|
|
|
|
44
|
|
|
$this->migrationVersionService = $migrationVersionService; |
|
45
|
|
|
$this->defaultLifeTime = $defaultLifeTime; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* {@inheritdoc} |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function doFetch($id) |
|
52
|
|
|
{ |
|
53
|
|
|
return parent::doFetch($this->getModifiedKeyWithMigrationPrefix($id)); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function doFetchMultiple(array $keys): array |
|
60
|
|
|
{ |
|
61
|
|
|
return parent::doFetchMultiple($this->getModifiedKeysWithMigrationPrefix($keys)); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
/** |
|
65
|
|
|
* {@inheritdoc} |
|
66
|
|
|
*/ |
|
67
|
|
|
protected function doContains($id): bool |
|
68
|
|
|
{ |
|
69
|
|
|
return parent::doContains($this->getModifiedKeyWithMigrationPrefix($id)); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* {@inheritdoc} |
|
74
|
|
|
*/ |
|
75
|
|
|
protected function doDelete($id): bool |
|
76
|
|
|
{ |
|
77
|
|
|
return parent::doDelete($this->getModifiedKeyWithMigrationPrefix($id)); |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* {@inheritdoc} |
|
82
|
|
|
*/ |
|
83
|
|
|
protected function doDeleteMultiple(array $keys): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return parent::doDeleteMultiple($this->getModifiedKeysWithMigrationPrefix($keys)); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* {@inheritdoc} |
|
90
|
|
|
*/ |
|
91
|
|
|
protected function doSave($id, $data, $lifetime = 0): bool |
|
92
|
|
|
{ |
|
93
|
|
|
if (0 === $lifetime && 0 !== $this->defaultLifeTime) { |
|
94
|
|
|
$ttl = $this->defaultLifeTime; |
|
95
|
|
|
} else { |
|
96
|
|
|
$ttl = $lifetime; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
return parent::doSave($this->getModifiedKeyWithMigrationPrefix($id), $data, $ttl); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
/** |
|
103
|
|
|
* {@inheritdoc} |
|
104
|
|
|
*/ |
|
105
|
|
|
protected function doSaveMultiple(array $keysAndValues, $lifetime = 0): bool |
|
106
|
|
|
{ |
|
107
|
|
|
if (0 === $lifetime && 0 !== $this->defaultLifeTime) { |
|
108
|
|
|
$ttl = $this->defaultLifeTime; |
|
109
|
|
|
} else { |
|
110
|
|
|
$ttl = $lifetime; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
return parent::doSaveMultiple($this->getModifiedKeysAndValuesWithMigrationPrefix($keysAndValues), $ttl); |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* @param string $key |
|
118
|
|
|
* |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
|
|
private function getModifiedKeyWithMigrationPrefix(string $key): string |
|
122
|
|
|
{ |
|
123
|
|
|
if (null === $this->lastMigrationVersion) { |
|
124
|
|
|
$this->lastMigrationVersion = $this->migrationVersionService->getLastMigrationVersion(); |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
return \sprintf('[%s]%s', $this->lastMigrationVersion, $key); |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
/** |
|
131
|
|
|
* @param array $keys |
|
132
|
|
|
* |
|
133
|
|
|
* @return array |
|
134
|
|
|
*/ |
|
135
|
|
|
private function getModifiedKeysWithMigrationPrefix(array $keys): array |
|
136
|
|
|
{ |
|
137
|
|
|
return \array_map( |
|
138
|
|
|
function (string $key) { |
|
139
|
|
|
return $this->getModifiedKeyWithMigrationPrefix($key); |
|
140
|
|
|
}, |
|
141
|
|
|
$keys |
|
142
|
|
|
); |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @param array $keysAndValues |
|
147
|
|
|
* |
|
148
|
|
|
* @return array |
|
149
|
|
|
*/ |
|
150
|
|
|
private function getModifiedKeysAndValuesWithMigrationPrefix(array $keysAndValues): array |
|
151
|
|
|
{ |
|
152
|
|
|
$result = []; |
|
153
|
|
|
foreach ($keysAndValues as $key => $value) { |
|
154
|
|
|
$result[$this->getModifiedKeyWithMigrationPrefix($key)] = $value; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
return $result; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|