1717
1818use Cake \Cache \CacheEngine ;
1919use Cake \Cache \CacheRegistry ;
20- use DebugKit \ DebugTimer ;
20+ use Psr \ Log \ LoggerInterface ;
2121
2222/**
2323 * A spying proxy for cache engines.
@@ -40,27 +40,40 @@ class DebugEngine extends CacheEngine
4040 */
4141 protected $ _engine ;
4242
43+ /**
44+ * @var \Psr\Log\LoggerInterface
45+ */
46+ protected $ logger ;
47+
48+ /**
49+ * @var string
50+ */
51+ protected $ name ;
52+
4353 /**
4454 * Hit/miss metrics.
4555 *
4656 * @var mixed
4757 */
48- protected $ _metrics = [
58+ protected $ metrics = [
4959 'set ' => 0 ,
5060 'delete ' => 0 ,
51- 'get ' => 0 ,
52- 'hit ' => 0 ,
53- 'miss ' => 0 ,
61+ 'get hit ' => 0 ,
62+ 'get miss ' => 0 ,
5463 ];
5564
5665 /**
5766 * Constructor
5867 *
5968 * @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.
6071 */
61- public function __construct ($ config )
72+ public function __construct ($ config, string $ name , LoggerInterface $ logger )
6273 {
6374 $ this ->_config = $ config ;
75+ $ this ->logger = $ logger ;
76+ $ this ->name = $ name ;
6477 }
6578
6679 /**
@@ -100,7 +113,7 @@ public function engine()
100113 */
101114 public function metrics ()
102115 {
103- return $ this ->_metrics ;
116+ return $ this ->metrics ;
104117 }
105118
106119 /**
@@ -109,20 +122,37 @@ public function metrics()
109122 * @param string $metric The metric to increment.
110123 * @return void
111124 */
112- protected function _track ($ metric )
125+ protected function track ($ metric )
113126 {
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 " );
115143 }
116144
117145 /**
118146 * {@inheritDoc}
119147 */
120148 public function set ($ key , $ value , $ ttl = null ): bool
121149 {
122- $ this ->_track ('set ' );
123- DebugTimer::start ('Cache.set ' . $ key );
150+ $ start = microtime (true );
124151 $ 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 );
126156
127157 return $ result ;
128158 }
@@ -132,10 +162,12 @@ public function set($key, $value, $ttl = null): bool
132162 */
133163 public function setMultiple ($ data , $ ttl = null ): bool
134164 {
135- $ this ->_track ('set ' );
136- DebugTimer::start ('Cache.setMultiple ' );
165+ $ start = microtime (true );
137166 $ 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 );
139171
140172 return $ result ;
141173 }
@@ -145,15 +177,16 @@ public function setMultiple($data, $ttl = null): bool
145177 */
146178 public function get ($ key , $ default = null )
147179 {
148- $ this ->_track ('get ' );
149- DebugTimer::start ('Cache.get ' . $ key );
180+ $ start = microtime (true );
150181 $ result = $ this ->_engine ->get ($ key , $ default );
151- DebugTimer:: stop ( ' Cache.get ' . $ key ) ;
182+ $ duration = microtime ( true ) - $ start ;
152183 $ metric = 'hit ' ;
153- if ($ result === false ) {
184+ if ($ result === null ) {
154185 $ metric = 'miss ' ;
155186 }
156- $ this ->_track ($ metric );
187+
188+ $ this ->track ("get {$ metric }" );
189+ $ this ->log ('get ' , $ duration , $ key );
157190
158191 return $ result ;
159192 }
@@ -163,10 +196,12 @@ public function get($key, $default = null)
163196 */
164197 public function getMultiple ($ keys , $ default = null ): iterable
165198 {
166- $ this ->_track ('get ' );
167- DebugTimer::start ('Cache.getMultiple ' );
199+ $ start = microtime (true );
168200 $ 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 );
170205
171206 return $ result ;
172207 }
@@ -176,10 +211,12 @@ public function getMultiple($keys, $default = null): iterable
176211 */
177212 public function increment (string $ key , int $ offset = 1 )
178213 {
179- $ this ->_track ('set ' );
180- DebugTimer::start ('Cache.increment ' . $ key );
214+ $ start = microtime (true );
181215 $ 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 );
183220
184221 return $ result ;
185222 }
@@ -189,10 +226,12 @@ public function increment(string $key, int $offset = 1)
189226 */
190227 public function decrement (string $ key , int $ offset = 1 )
191228 {
192- $ this ->_track ('set ' );
193- DebugTimer::start ('Cache.decrement ' . $ key );
229+ $ start = microtime (true );
194230 $ 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 );
196235
197236 return $ result ;
198237 }
@@ -202,10 +241,12 @@ public function decrement(string $key, int $offset = 1)
202241 */
203242 public function delete ($ key ): bool
204243 {
205- $ this ->_track ('delete ' );
206- DebugTimer::start ('Cache.delete ' . $ key );
244+ $ start = microtime (true );
207245 $ 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 );
209250
210251 return $ result ;
211252 }
@@ -215,10 +256,12 @@ public function delete($key): bool
215256 */
216257 public function deleteMultiple ($ data ): bool
217258 {
218- $ this ->_track ('delete ' );
219- DebugTimer::start ('Cache.deleteMultiple ' );
259+ $ start = microtime (true );
220260 $ 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 );
222265
223266 return $ result ;
224267 }
@@ -228,10 +271,12 @@ public function deleteMultiple($data): bool
228271 */
229272 public function clear (): bool
230273 {
231- $ this ->_track ('delete ' );
232- DebugTimer::start ('Cache.clear ' );
274+ $ start = microtime (true );
233275 $ result = $ this ->_engine ->clear ();
234- DebugTimer::stop ('Cache.clear ' );
276+ $ duration = microtime (true ) - $ start ;
277+
278+ $ this ->track ('delete ' );
279+ $ this ->log ('clear ' , $ duration );
235280
236281 return $ result ;
237282 }
@@ -275,10 +320,12 @@ public function setConfig($key, $value = null, $merge = true)
275320 */
276321 public function clearGroup (string $ group ): bool
277322 {
278- $ this ->_track ('delete ' );
279- DebugTimer::start ('Cache.clearGroup ' . $ group );
323+ $ start = microtime (true );
280324 $ 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 );
282329
283330 return $ result ;
284331 }
0 commit comments