DebugEngine.php
6.4 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
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*
*/
namespace DebugKit\Cache\Engine;
use Cake\Cache\CacheEngine;
use Cake\Cache\CacheRegistry;
use Cake\Core\App;
use DebugKit\DebugTimer;
/**
* A spying proxy for cache engines.
*
* Used by the CachePanel to wrap and track metrics related to caching.
*/
class DebugEngine extends CacheEngine
{
/**
* Proxied cache engine config.
*
* @var mixed
*/
protected $_config;
/**
* Proxied engine
*
* @var mixed
*/
protected $_engine;
/**
* Hit/miss metrics.
*
* @var mixed
*/
protected $_metrics = [
'write' => 0,
'delete' => 0,
'read' => 0,
'hit' => 0,
'miss' => 0,
];
/**
* Constructor
*
* @param mixed $config Config data or the proxied adapter.
*/
public function __construct($config)
{
$this->_config = $config;
}
/**
* 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 = [])
{
if (is_object($this->_config)) {
$this->_engine = $this->_config;
return true;
}
$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()
{
return $this->_engine;
}
/**
* Get the metrics for this object.
*
* @return array
*/
public function metrics()
{
return $this->_metrics;
}
/**
* Track a metric.
*
* @param string $metric The metric to increment.
* @return void
*/
protected function _track($metric)
{
$this->_metrics[$metric]++;
}
/**
* {@inheritDoc}
*/
public function write($key, $value)
{
$this->_track('write');
DebugTimer::start('Cache.write ' . $key);
$result = $this->_engine->write($key, $value);
DebugTimer::stop('Cache.write ' . $key);
return $result;
}
/**
* {@inheritDoc}
*/
public function writeMany($data)
{
$this->_track('write');
DebugTimer::start('Cache.writeMany');
$result = $this->_engine->writeMany($data);
DebugTimer::stop('Cache.writeMany');
return $result;
}
/**
* {@inheritDoc}
*/
public function read($key)
{
$this->_track('read');
DebugTimer::start('Cache.read ' . $key);
$result = $this->_engine->read($key);
DebugTimer::stop('Cache.read ' . $key);
$metric = 'hit';
if ($result === false) {
$metric = 'miss';
}
$this->_track($metric);
return $result;
}
/**
* {@inheritDoc}
*/
public function readMany($data)
{
$this->_track('read');
DebugTimer::start('Cache.readMany');
$result = $this->_engine->readMany($data);
DebugTimer::stop('Cache.readMany');
return $result;
}
/**
* {@inheritDoc}
*/
public function increment($key, $offset = 1)
{
$this->_track('write');
DebugTimer::start('Cache.increment ' . $key);
$result = $this->_engine->increment($key, $offset);
DebugTimer::stop('Cache.increment ' . $key);
return $result;
}
/**
* {@inheritDoc}
*/
public function decrement($key, $offset = 1)
{
$this->_track('write');
DebugTimer::start('Cache.decrement ' . $key);
$result = $this->_engine->decrement($key, $offset);
DebugTimer::stop('Cache.decrement ' . $key);
return $result;
}
/**
* {@inheritDoc}
*/
public function delete($key)
{
$this->_track('delete');
DebugTimer::start('Cache.delete ' . $key);
$result = $this->_engine->delete($key);
DebugTimer::stop('Cache.delete ' . $key);
return $result;
}
/**
* {@inheritDoc}
*/
public function deleteMany($data)
{
$this->_track('delete');
DebugTimer::start('Cache.deleteMany');
$result = $this->_engine->deleteMany($data);
DebugTimer::stop('Cache.deleteMany');
return $result;
}
/**
* {@inheritDoc}
*/
public function clear($check)
{
$this->_track('delete');
DebugTimer::start('Cache.clear');
$result = $this->_engine->clear($check);
DebugTimer::stop('Cache.clear');
return $result;
}
/**
* {@inheritDoc}
*/
public function groups()
{
return $this->_engine->groups();
}
/**
* Return the proxied configuration data.
*
* This method uses func_get_args() as not doing so confuses the
* proxied class.
*
* @param string $key The key to set/read.
* @param mixed $value The value to set.
* @param bool $merge Whether or not configuration should be merged.
*/
public function config($key = null, $value = null, $merge = true)
{
return call_user_func_array([$this->_engine, 'config'], func_get_args());
}
/**
* {@inheritDoc}
*/
public function clearGroup($group)
{
$this->_track('delete');
DebugTimer::start('Cache.clearGroup ' . $group);
$result = $this->_engine->clearGroup($group);
DebugTimer::stop('Cache.clearGroup ' . $group);
return $result;
}
/**
* Magic __toString() method to get the CacheEngine's name
*
* @return string Returns the CacheEngine's name
*/
public function __toString()
{
if (!empty($this->_engine)) {
list($ns, $class) = namespaceSplit(get_class($this->_engine));
return str_replace('Engine', '', $class);
}
return $this->_config['className'];
}
}