1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace TechDeCo\ElasticApmAgent\Convenience\Cache; |
5
|
|
|
|
6
|
|
|
use Psr\Cache\CacheItemInterface; |
7
|
|
|
use Psr\Cache\CacheItemPoolInterface; |
8
|
|
|
use Psr\Cache\InvalidArgumentException; |
9
|
|
|
use TechDeCo\ElasticApmAgent\Convenience\OpenTransaction; |
10
|
|
|
use TechDeCo\ElasticApmAgent\Convenience\OpenTransactionEnricher; |
11
|
|
|
use TechDeCo\ElasticApmAgent\Convenience\Util\Stopwatch; |
12
|
|
|
use TechDeCo\ElasticApmAgent\Message\Span; |
13
|
|
|
use Traversable; |
14
|
|
|
use function implode; |
15
|
|
|
|
16
|
|
|
final class CacheItemPoolWrapper implements CacheItemPoolInterface, OpenTransactionEnricher |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var CacheItemPoolInterface |
20
|
|
|
*/ |
21
|
|
|
private $pool; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var OpenTransaction |
25
|
|
|
*/ |
26
|
|
|
private $transaction; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
private $type; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param CacheItemPoolInterface $pool The pool that will be wrapped |
35
|
|
|
* @param string $type The type of cache, for example redis, that will be reported in the span |
36
|
|
|
*/ |
37
|
23 |
|
public function __construct( |
38
|
|
|
CacheItemPoolInterface $pool, |
39
|
|
|
string $type |
40
|
|
|
) { |
41
|
23 |
|
$this->pool = $pool; |
42
|
23 |
|
$this->type = $type; |
43
|
23 |
|
} |
44
|
|
|
|
45
|
23 |
|
public function setOpenTransaction(OpenTransaction $transaction): void |
46
|
|
|
{ |
47
|
23 |
|
$this->transaction = $transaction; |
48
|
23 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $key |
52
|
|
|
* @throws InvalidArgumentException |
53
|
|
|
*/ |
54
|
2 |
|
public function getItem($key): CacheItemInterface |
55
|
|
|
{ |
56
|
2 |
|
$start = Stopwatch::start(); |
57
|
2 |
|
$offset = $this->transaction->getStartOffset(); |
58
|
|
|
|
59
|
|
|
try { |
60
|
2 |
|
return $this->pool->getItem($key); |
61
|
|
|
} finally { |
62
|
2 |
|
$this->transaction->addSpan(new Span( |
63
|
2 |
|
Stopwatch::stop($start), |
64
|
2 |
|
$key, |
65
|
2 |
|
$offset, |
66
|
2 |
|
$this->type . '.get-item' |
67
|
|
|
)); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param string[] $keys |
73
|
|
|
* @throws InvalidArgumentException |
74
|
|
|
* @return array|Traversable |
75
|
|
|
*/ |
76
|
4 |
|
public function getItems(array $keys = []) |
77
|
|
|
{ |
78
|
4 |
|
$start = Stopwatch::start(); |
79
|
4 |
|
$offset = $this->transaction->getStartOffset(); |
80
|
|
|
|
81
|
|
|
try { |
82
|
4 |
|
return $this->pool->getItems($keys); |
83
|
|
|
} finally { |
84
|
4 |
|
$this->transaction->addSpan(new Span( |
85
|
4 |
|
Stopwatch::stop($start), |
86
|
4 |
|
implode(',', $keys), |
87
|
4 |
|
$offset, |
88
|
4 |
|
$this->type . '.get-items' |
89
|
|
|
)); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* @param string $key |
95
|
|
|
* @throws InvalidArgumentException |
96
|
|
|
*/ |
97
|
3 |
|
public function hasItem($key): ?bool |
98
|
|
|
{ |
99
|
3 |
|
$start = Stopwatch::start(); |
100
|
3 |
|
$offset = $this->transaction->getStartOffset(); |
101
|
|
|
|
102
|
|
|
try { |
103
|
3 |
|
return $this->pool->hasItem($key); |
104
|
|
|
} finally { |
105
|
3 |
|
$this->transaction->addSpan(new Span( |
106
|
3 |
|
Stopwatch::stop($start), |
107
|
3 |
|
$key, |
108
|
3 |
|
$offset, |
109
|
3 |
|
$this->type . '.has-item' |
110
|
|
|
)); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
114
|
2 |
|
public function clear(): ?bool |
115
|
|
|
{ |
116
|
2 |
|
$start = Stopwatch::start(); |
117
|
2 |
|
$offset = $this->transaction->getStartOffset(); |
118
|
|
|
|
119
|
|
|
try { |
120
|
2 |
|
return $this->pool->clear(); |
121
|
|
|
} finally { |
122
|
2 |
|
$this->transaction->addSpan(new Span( |
123
|
2 |
|
Stopwatch::stop($start), |
124
|
2 |
|
'', |
125
|
2 |
|
$offset, |
126
|
2 |
|
$this->type . '.clear' |
127
|
|
|
)); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* @param string $key |
133
|
|
|
* @throws InvalidArgumentException |
134
|
|
|
*/ |
135
|
3 |
|
public function deleteItem($key): ?bool |
136
|
|
|
{ |
137
|
3 |
|
$start = Stopwatch::start(); |
138
|
3 |
|
$offset = $this->transaction->getStartOffset(); |
139
|
|
|
|
140
|
|
|
try { |
141
|
3 |
|
return $this->pool->deleteItem($key); |
142
|
|
|
} finally { |
143
|
3 |
|
$this->transaction->addSpan(new Span( |
144
|
3 |
|
Stopwatch::stop($start), |
145
|
3 |
|
$key, |
146
|
3 |
|
$offset, |
147
|
3 |
|
$this->type . '.delete-item' |
148
|
|
|
)); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* @param string[] $keys |
154
|
|
|
* @throws InvalidArgumentException |
155
|
|
|
*/ |
156
|
3 |
|
public function deleteItems(array $keys): ?bool |
157
|
|
|
{ |
158
|
3 |
|
$start = Stopwatch::start(); |
159
|
3 |
|
$offset = $this->transaction->getStartOffset(); |
160
|
|
|
|
161
|
|
|
try { |
162
|
3 |
|
return $this->pool->deleteItems($keys); |
163
|
|
|
} finally { |
164
|
3 |
|
$this->transaction->addSpan(new Span( |
165
|
3 |
|
Stopwatch::stop($start), |
166
|
3 |
|
implode(',', $keys), |
167
|
3 |
|
$offset, |
168
|
3 |
|
$this->type . '.delete-items' |
169
|
|
|
)); |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
2 |
|
public function save(CacheItemInterface $item): ?bool |
174
|
|
|
{ |
175
|
2 |
|
$start = Stopwatch::start(); |
176
|
2 |
|
$offset = $this->transaction->getStartOffset(); |
177
|
|
|
|
178
|
|
|
try { |
179
|
2 |
|
return $this->pool->save($item); |
180
|
|
|
} finally { |
181
|
2 |
|
$this->transaction->addSpan(new Span( |
182
|
2 |
|
Stopwatch::stop($start), |
183
|
2 |
|
$item->getKey(), |
184
|
2 |
|
$offset, |
185
|
2 |
|
$this->type . '.save' |
186
|
|
|
)); |
187
|
|
|
} |
188
|
|
|
} |
189
|
|
|
|
190
|
2 |
|
public function saveDeferred(CacheItemInterface $item): ?bool |
191
|
|
|
{ |
192
|
2 |
|
$start = Stopwatch::start(); |
193
|
2 |
|
$offset = $this->transaction->getStartOffset(); |
194
|
|
|
|
195
|
|
|
try { |
196
|
2 |
|
return $this->pool->saveDeferred($item); |
197
|
|
|
} finally { |
198
|
2 |
|
$this->transaction->addSpan(new Span( |
199
|
2 |
|
Stopwatch::stop($start), |
200
|
2 |
|
$item->getKey(), |
201
|
2 |
|
$offset, |
202
|
2 |
|
$this->type . '.save-deferred' |
203
|
|
|
)); |
204
|
|
|
} |
205
|
|
|
} |
206
|
|
|
|
207
|
2 |
|
public function commit(): ?bool |
208
|
|
|
{ |
209
|
2 |
|
$start = Stopwatch::start(); |
210
|
2 |
|
$offset = $this->transaction->getStartOffset(); |
211
|
|
|
|
212
|
|
|
try { |
213
|
2 |
|
return $this->pool->commit(); |
214
|
|
|
} finally { |
215
|
2 |
|
$this->transaction->addSpan(new Span( |
216
|
2 |
|
Stopwatch::stop($start), |
217
|
2 |
|
'commit', |
218
|
2 |
|
$offset, |
219
|
2 |
|
$this->type . '.commit' |
220
|
|
|
)); |
221
|
|
|
} |
222
|
|
|
} |
223
|
|
|
} |
224
|
|
|
|