17
17
18
18
use Cake \Cache \CacheEngine ;
19
19
use Cake \Cache \CacheRegistry ;
20
- use DebugKit \ DebugTimer ;
20
+ use Psr \ Log \ LoggerInterface ;
21
21
22
22
/**
23
23
* A spying proxy for cache engines.
@@ -40,27 +40,40 @@ class DebugEngine extends CacheEngine
40
40
*/
41
41
protected $ _engine ;
42
42
43
+ /**
44
+ * @var \Psr\Log\LoggerInterface
45
+ */
46
+ protected $ logger ;
47
+
48
+ /**
49
+ * @var string
50
+ */
51
+ protected $ name ;
52
+
43
53
/**
44
54
* Hit/miss metrics.
45
55
*
46
56
* @var mixed
47
57
*/
48
- protected $ _metrics = [
58
+ protected $ metrics = [
49
59
'set ' => 0 ,
50
60
'delete ' => 0 ,
51
- 'get ' => 0 ,
52
- 'hit ' => 0 ,
53
- 'miss ' => 0 ,
61
+ 'get hit ' => 0 ,
62
+ 'get miss ' => 0 ,
54
63
];
55
64
56
65
/**
57
66
* Constructor
58
67
*
59
68
* @param mixed $config Config data or the proxied adapter.
69
+ * @param string $name The name of the proxied cache engine.
70
+ * @param \Psr\Log\LoggerInterface $logger Logger for collecting cache operation logs.
60
71
*/
61
- public function __construct ($ config )
72
+ public function __construct ($ config, string $ name , LoggerInterface $ logger )
62
73
{
63
74
$ this ->_config = $ config ;
75
+ $ this ->logger = $ logger ;
76
+ $ this ->name = $ name ;
64
77
}
65
78
66
79
/**
@@ -100,7 +113,7 @@ public function engine()
100
113
*/
101
114
public function metrics ()
102
115
{
103
- return $ this ->_metrics ;
116
+ return $ this ->metrics ;
104
117
}
105
118
106
119
/**
@@ -109,20 +122,37 @@ public function metrics()
109
122
* @param string $metric The metric to increment.
110
123
* @return void
111
124
*/
112
- protected function _track ($ metric )
125
+ protected function track ($ metric )
113
126
{
114
- $ this ->_metrics [$ metric ]++;
127
+ $ this ->metrics [$ metric ]++;
128
+ }
129
+
130
+ /**
131
+ * Log a cache operation
132
+ *
133
+ * @param string $operation The operation performed.
134
+ * @param float $duration The duration of the operation.
135
+ * @param string|null $key The cache key.
136
+ * @return void
137
+ */
138
+ protected function log (string $ operation , float $ duration , ?string $ key = null ): void
139
+ {
140
+ $ key = $ key ? " ` {$ key }` " : '' ;
141
+ $ duration = number_format ($ duration , 5 );
142
+ $ this ->logger ->log ('info ' , ": {$ this ->name }: {$ operation }{$ key } - {$ duration }ms " );
115
143
}
116
144
117
145
/**
118
146
* {@inheritDoc}
119
147
*/
120
148
public function set ($ key , $ value , $ ttl = null ): bool
121
149
{
122
- $ this ->_track ('set ' );
123
- DebugTimer::start ('Cache.set ' . $ key );
150
+ $ start = microtime (true );
124
151
$ result = $ this ->_engine ->set ($ key , $ value , $ ttl );
125
- DebugTimer::stop ('Cache.set ' . $ key );
152
+ $ duration = microtime (true ) - $ start ;
153
+
154
+ $ this ->track ('set ' );
155
+ $ this ->log ('set ' , $ duration , $ key );
126
156
127
157
return $ result ;
128
158
}
@@ -132,10 +162,12 @@ public function set($key, $value, $ttl = null): bool
132
162
*/
133
163
public function setMultiple ($ data , $ ttl = null ): bool
134
164
{
135
- $ this ->_track ('set ' );
136
- DebugTimer::start ('Cache.setMultiple ' );
165
+ $ start = microtime (true );
137
166
$ result = $ this ->_engine ->setMultiple ($ data );
138
- DebugTimer::stop ('Cache.setMultiple ' );
167
+ $ duration = microtime (true ) - $ start ;
168
+
169
+ $ this ->track ('set ' );
170
+ $ this ->log ('setMultiple ' , $ duration );
139
171
140
172
return $ result ;
141
173
}
@@ -145,15 +177,16 @@ public function setMultiple($data, $ttl = null): bool
145
177
*/
146
178
public function get ($ key , $ default = null )
147
179
{
148
- $ this ->_track ('get ' );
149
- DebugTimer::start ('Cache.get ' . $ key );
180
+ $ start = microtime (true );
150
181
$ result = $ this ->_engine ->get ($ key , $ default );
151
- DebugTimer:: stop ( ' Cache.get ' . $ key ) ;
182
+ $ duration = microtime ( true ) - $ start ;
152
183
$ metric = 'hit ' ;
153
- if ($ result === false ) {
184
+ if ($ result === null ) {
154
185
$ metric = 'miss ' ;
155
186
}
156
- $ this ->_track ($ metric );
187
+
188
+ $ this ->track ("get {$ metric }" );
189
+ $ this ->log ('get ' , $ duration , $ key );
157
190
158
191
return $ result ;
159
192
}
@@ -163,10 +196,12 @@ public function get($key, $default = null)
163
196
*/
164
197
public function getMultiple ($ keys , $ default = null ): iterable
165
198
{
166
- $ this ->_track ('get ' );
167
- DebugTimer::start ('Cache.getMultiple ' );
199
+ $ start = microtime (true );
168
200
$ result = $ this ->_engine ->getMultiple ($ keys );
169
- DebugTimer::stop ('Cache.getMultiple ' );
201
+ $ duration = microtime (true ) - $ start ;
202
+
203
+ $ this ->track ('get hit ' );
204
+ $ this ->log ('getMultiple ' , $ duration );
170
205
171
206
return $ result ;
172
207
}
@@ -176,10 +211,12 @@ public function getMultiple($keys, $default = null): iterable
176
211
*/
177
212
public function increment (string $ key , int $ offset = 1 )
178
213
{
179
- $ this ->_track ('set ' );
180
- DebugTimer::start ('Cache.increment ' . $ key );
214
+ $ start = microtime (true );
181
215
$ result = $ this ->_engine ->increment ($ key , $ offset );
182
- DebugTimer::stop ('Cache.increment ' . $ key );
216
+ $ duration = microtime (true ) - $ start ;
217
+
218
+ $ this ->track ('set ' );
219
+ $ this ->log ('increment ' , $ duration , $ key );
183
220
184
221
return $ result ;
185
222
}
@@ -189,10 +226,12 @@ public function increment(string $key, int $offset = 1)
189
226
*/
190
227
public function decrement (string $ key , int $ offset = 1 )
191
228
{
192
- $ this ->_track ('set ' );
193
- DebugTimer::start ('Cache.decrement ' . $ key );
229
+ $ start = microtime (true );
194
230
$ result = $ this ->_engine ->decrement ($ key , $ offset );
195
- DebugTimer::stop ('Cache.decrement ' . $ key );
231
+ $ duration = microtime (true ) - $ start ;
232
+
233
+ $ this ->track ('set ' );
234
+ $ this ->log ('decrement ' , $ duration , $ key );
196
235
197
236
return $ result ;
198
237
}
@@ -202,10 +241,12 @@ public function decrement(string $key, int $offset = 1)
202
241
*/
203
242
public function delete ($ key ): bool
204
243
{
205
- $ this ->_track ('delete ' );
206
- DebugTimer::start ('Cache.delete ' . $ key );
244
+ $ start = microtime (true );
207
245
$ result = $ this ->_engine ->delete ($ key );
208
- DebugTimer::stop ('Cache.delete ' . $ key );
246
+ $ duration = microtime (true ) - $ start ;
247
+
248
+ $ this ->track ('delete ' );
249
+ $ this ->log ('delete ' , $ duration , $ key );
209
250
210
251
return $ result ;
211
252
}
@@ -215,10 +256,12 @@ public function delete($key): bool
215
256
*/
216
257
public function deleteMultiple ($ data ): bool
217
258
{
218
- $ this ->_track ('delete ' );
219
- DebugTimer::start ('Cache.deleteMultiple ' );
259
+ $ start = microtime (true );
220
260
$ result = $ this ->_engine ->deleteMultiple ($ data );
221
- DebugTimer::stop ('Cache.deleteMultiple ' );
261
+ $ duration = microtime (true ) - $ start ;
262
+
263
+ $ this ->track ('delete ' );
264
+ $ this ->log ('deleteMultiple ' , $ duration );
222
265
223
266
return $ result ;
224
267
}
@@ -228,10 +271,12 @@ public function deleteMultiple($data): bool
228
271
*/
229
272
public function clear (): bool
230
273
{
231
- $ this ->_track ('delete ' );
232
- DebugTimer::start ('Cache.clear ' );
274
+ $ start = microtime (true );
233
275
$ result = $ this ->_engine ->clear ();
234
- DebugTimer::stop ('Cache.clear ' );
276
+ $ duration = microtime (true ) - $ start ;
277
+
278
+ $ this ->track ('delete ' );
279
+ $ this ->log ('clear ' , $ duration );
235
280
236
281
return $ result ;
237
282
}
@@ -275,10 +320,12 @@ public function setConfig($key, $value = null, $merge = true)
275
320
*/
276
321
public function clearGroup (string $ group ): bool
277
322
{
278
- $ this ->_track ('delete ' );
279
- DebugTimer::start ('Cache.clearGroup ' . $ group );
323
+ $ start = microtime (true );
280
324
$ result = $ this ->_engine ->clearGroup ($ group );
281
- DebugTimer::stop ('Cache.clearGroup ' . $ group );
325
+ $ duration = microtime (true ) - $ start ;
326
+
327
+ $ this ->track ('delete ' );
328
+ $ this ->log ('clearGroup ' , $ duration , $ group );
282
329
283
330
return $ result ;
284
331
}
0 commit comments