77use Illuminate \Cache \Events \{
88 CacheFlushed ,
99 CacheFlushFailed ,
10+ CacheFlushing ,
1011 CacheHit ,
1112 CacheMissed ,
13+ ForgettingKey ,
1214 KeyForgetFailed ,
1315 KeyForgotten ,
1416 KeyWriteFailed ,
1517 KeyWritten ,
18+ RetrievingKey ,
19+ WritingKey ,
1620};
1721use Illuminate \Events \Dispatcher ;
1822
@@ -23,21 +27,24 @@ class CacheCollector extends TimeDataCollector
2327 /** @var bool */
2428 protected $ collectValues ;
2529
30+ /** @var array */
31+ protected $ eventStarts = [];
32+
2633 /** @var array */
2734 protected $ classMap = [
28- CacheHit::class => 'hit ' ,
29- CacheMissed::class => 'missed ' ,
30- CacheFlushed::class => 'flushed ' ,
31- CacheFlushFailed::class => 'flush_failed ' ,
32- KeyWritten::class => 'written ' ,
33- KeyWriteFailed::class => 'write_failed ' ,
34- KeyForgotten::class => 'forgotten ' ,
35- KeyForgetFailed::class => 'forget_failed ' ,
35+ CacheHit::class => [ 'hit ' , RetrievingKey::class] ,
36+ CacheMissed::class => [ 'missed ' , RetrievingKey::class] ,
37+ CacheFlushed::class => [ 'flushed ' , CacheFlushing::class] ,
38+ CacheFlushFailed::class => [ 'flush_failed ' , CacheFlushing::class] ,
39+ KeyWritten::class => [ 'written ' , WritingKey::class] ,
40+ KeyWriteFailed::class => [ 'write_failed ' , WritingKey::class] ,
41+ KeyForgotten::class => [ 'forgotten ' , ForgettingKey::class] ,
42+ KeyForgetFailed::class => [ 'forget_failed ' , ForgettingKey::class] ,
3643 ];
3744
3845 public function __construct ($ requestStartTime , $ collectValues )
3946 {
40- parent ::__construct ();
47+ parent ::__construct ($ requestStartTime );
4148
4249 $ this ->collectValues = $ collectValues ;
4350 }
@@ -46,8 +53,7 @@ public function onCacheEvent($event)
4653 {
4754 $ class = get_class ($ event );
4855 $ params = get_object_vars ($ event );
49-
50- $ label = $ this ->classMap [$ class ];
56+ $ label = $ this ->classMap [$ class ][0 ];
5157
5258 if (isset ($ params ['value ' ])) {
5359 if ($ this ->collectValues ) {
@@ -61,7 +67,6 @@ public function onCacheEvent($event)
6167 }
6268 }
6369
64-
6570 if (!empty ($ params ['key ' ] ?? null ) && in_array ($ label , ['hit ' , 'written ' ])) {
6671 $ params ['delete ' ] = route ('debugbar.cache.delete ' , [
6772 'key ' => urlencode ($ params ['key ' ]),
@@ -70,14 +75,38 @@ public function onCacheEvent($event)
7075 }
7176
7277 $ time = microtime (true );
73- $ this ->addMeasure ($ label . "\t" . ($ params ['key ' ] ?? '' ), $ time , $ time , $ params );
78+ $ startHashKey = $ this ->getEventHash ($ this ->classMap [$ class ][1 ] ?? '' , $ params );
79+ $ startTime = $ this ->eventStarts [$ startHashKey ] ?? $ time ;
80+ $ this ->addMeasure ($ label . "\t" . ($ params ['key ' ] ?? '' ), $ startTime , $ time , $ params );
81+ }
82+
83+ public function onStartCacheEvent ($ event )
84+ {
85+ $ startHashKey = $ this ->getEventHash (get_class ($ event ), get_object_vars ($ event ));
86+ $ this ->eventStarts [$ startHashKey ] = microtime (true );
87+ }
88+
89+ private function getEventHash (string $ class , array $ params ): string
90+ {
91+ unset($ params ['value ' ]);
92+
93+ return $ class . ': ' . substr (hash ('sha256 ' , json_encode ($ params )), 0 , 12 );
7494 }
7595
7696 public function subscribe (Dispatcher $ dispatcher )
7797 {
7898 foreach (array_keys ($ this ->classMap ) as $ eventClass ) {
7999 $ dispatcher ->listen ($ eventClass , [$ this , 'onCacheEvent ' ]);
80100 }
101+
102+ $ startEvents = array_unique (array_filter (array_map (
103+ fn ($ values ) => $ values [1 ] ?? null ,
104+ array_values ($ this ->classMap )
105+ )));
106+
107+ foreach ($ startEvents as $ eventClass ) {
108+ $ dispatcher ->listen ($ eventClass , [$ this , 'onStartCacheEvent ' ]);
109+ }
81110 }
82111
83112 public function collect ()
0 commit comments