-
Notifications
You must be signed in to change notification settings - Fork 567
Expand file tree
/
Copy pathDebugEngine.php
More file actions
338 lines (287 loc) · 8.35 KB
/
DebugEngine.php
File metadata and controls
338 lines (287 loc) · 8.35 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
<?php
declare(strict_types=1);
/**
* CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
* @link https://cakephp.org CakePHP(tm) Project
* @license https://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace DebugKit\Cache\Engine;
use Cake\Cache\CacheEngine;
use Cake\Cache\CacheRegistry;
use Psr\Log\LoggerInterface;
use function Cake\Core\namespaceSplit;
/**
* A spying proxy for cache engines.
*
* Used by the CachePanel to wrap and track metrics related to caching.
*/
class DebugEngine extends CacheEngine
{
/**
* Proxied engine
*/
protected CacheEngine $_engine;
protected LoggerInterface $logger;
protected string $name;
/**
* Hit/miss metrics.
*
* @var array<string, int>
*/
protected array $metrics = [
'set' => 0,
'delete' => 0,
'get hit' => 0,
'get miss' => 0,
];
/**
* Constructor
*
* @param \Cake\Cache\CacheEngine|array<string, mixed> $config Config data or the proxied adapter.
* @param string $name The name of the proxied cache engine.
* @param \Psr\Log\LoggerInterface $logger Logger for collecting cache operation logs.
*/
public function __construct(CacheEngine|array $config, string $name, LoggerInterface $logger)
{
if ($config instanceof CacheEngine) {
$this->_engine = $config;
} else {
$this->_config = $config;
}
$this->logger = $logger;
$this->name = $name;
}
/**
* Initialize the proxied Cache Engine
*
* @param array $config Array of setting for the engine.
* @return bool True, this engine cannot fail to initialize.
*/
public function init(array $config = []): bool
{
if (!isset($this->_engine)) {
$registry = new CacheRegistry();
$this->_engine = $registry->load('spies', $this->_config);
unset($registry);
}
return true;
}
/**
* Get the internal engine
*
* @return \Cake\Cache\CacheEngine
*/
public function engine(): CacheEngine
{
return $this->_engine;
}
/**
* Get the metrics for this object.
*
* @return array
*/
public function metrics(): array
{
return $this->metrics;
}
/**
* Track a metric.
*
* @param string $metric The metric to increment.
* @return void
*/
protected function track(string $metric): void
{
$this->metrics[$metric]++;
}
/**
* Log a cache operation
*
* @param string $operation The operation performed.
* @param float $duration The duration of the operation.
* @param string|null $key The cache key.
* @return void
*/
protected function log(string $operation, float $duration, ?string $key = null): void
{
$key = $key ? sprintf(' `%s`', $key) : '';
$duration = number_format($duration, 5);
$this->logger->log('info', sprintf(':%s: %s%s - %sms', $this->name, $operation, $key, $duration));
}
/**
* @inheritDoc
*/
public function set($key, $value, $ttl = null): bool
{
$start = microtime(true);
$result = $this->_engine->set($key, $value, $ttl);
$duration = microtime(true) - $start;
$this->track('set');
$this->log('set', $duration, $key);
return $result;
}
/**
* @inheritDoc
*/
public function setMultiple($values, $ttl = null): bool
{
$start = microtime(true);
$result = $this->_engine->setMultiple($values);
$duration = microtime(true) - $start;
$this->track('set');
$this->log('setMultiple', $duration);
return $result;
}
/**
* @inheritDoc
*/
public function get(string $key, mixed $default = null): mixed
{
$start = microtime(true);
$result = $this->_engine->get($key, $default);
$duration = microtime(true) - $start;
$metric = 'hit';
if ($result === null) {
$metric = 'miss';
}
$this->track('get ' . $metric);
$this->log('get', $duration, $key);
return $result;
}
/**
* @inheritDoc
*/
public function getMultiple($keys, $default = null): iterable
{
$start = microtime(true);
$result = $this->_engine->getMultiple($keys, $default);
$duration = microtime(true) - $start;
$this->track('get hit');
$this->log('getMultiple', $duration);
return $result;
}
/**
* @inheritDoc
*/
public function increment(string $key, int $offset = 1): int|false
{
$start = microtime(true);
$result = $this->_engine->increment($key, $offset);
$duration = microtime(true) - $start;
$this->track('set');
$this->log('increment', $duration, $key);
return $result;
}
/**
* @inheritDoc
*/
public function decrement(string $key, int $offset = 1): int|false
{
$start = microtime(true);
$result = $this->_engine->decrement($key, $offset);
$duration = microtime(true) - $start;
$this->track('set');
$this->log('decrement', $duration, $key);
return $result;
}
/**
* @inheritDoc
*/
public function delete($key): bool
{
$start = microtime(true);
$result = $this->_engine->delete($key);
$duration = microtime(true) - $start;
$this->track('delete');
$this->log('delete', $duration, $key);
return $result;
}
/**
* @inheritDoc
*/
public function deleteMultiple($keys): bool
{
$start = microtime(true);
$result = $this->_engine->deleteMultiple($keys);
$duration = microtime(true) - $start;
$this->track('delete');
$this->log('deleteMultiple', $duration);
return $result;
}
/**
* @inheritDoc
*/
public function clear(): bool
{
$start = microtime(true);
$result = $this->_engine->clear();
$duration = microtime(true) - $start;
$this->track('delete');
$this->log('clear', $duration);
return $result;
}
/**
* @inheritDoc
*/
public function groups(): array
{
return $this->_engine->groups();
}
/**
* Returns the config.
*
* @param string|null $key The key to get or null for the whole config.
* @param mixed $default The return value when the key does not exist.
* @return mixed Config value being read.
*/
public function getConfig(?string $key = null, mixed $default = null): mixed
{
return $this->_engine->getConfig($key, $default);
}
/**
* Sets the config.
*
* @param array|string $key The key to set, or a complete array of configs.
* @param mixed|null $value The value to set.
* @param bool $merge Whether to recursively merge or overwrite existing config, defaults to true.
* @return $this
* @throws \Cake\Core\Exception\CakeException When trying to set a key that is invalid.
*/
public function setConfig(array|string $key, mixed $value = null, bool $merge = true)
{
$this->_engine->setConfig($key, $value, $merge);
return $this;
}
/**
* @inheritDoc
*/
public function clearGroup(string $group): bool
{
$start = microtime(true);
$result = $this->_engine->clearGroup($group);
$duration = microtime(true) - $start;
$this->track('delete');
$this->log('clearGroup', $duration, $group);
return $result;
}
/**
* Magic __toString() method to get the CacheEngine's name
*
* @return string Returns the CacheEngine's name
*/
public function __toString(): string
{
if (isset($this->_engine)) {
// phpcs:ignore SlevomatCodingStandard.Variables.UnusedVariable.UnusedVariable
[$ns, $class] = namespaceSplit($this->_engine::class);
return str_replace('Engine', '', $class);
}
return $this->_config['className'];
}
}