7
7
use Illuminate \Cache \Events \{
8
8
CacheFlushed ,
9
9
CacheFlushFailed ,
10
+ CacheFlushing ,
10
11
CacheHit ,
11
12
CacheMissed ,
13
+ ForgettingKey ,
12
14
KeyForgetFailed ,
13
15
KeyForgotten ,
14
16
KeyWriteFailed ,
15
17
KeyWritten ,
18
+ RetrievingKey ,
19
+ WritingKey ,
16
20
};
17
21
use Illuminate \Events \Dispatcher ;
18
22
@@ -23,21 +27,24 @@ class CacheCollector extends TimeDataCollector
23
27
/** @var bool */
24
28
protected $ collectValues ;
25
29
30
+ /** @var array */
31
+ protected $ eventStarts = [];
32
+
26
33
/** @var array */
27
34
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] ,
36
43
];
37
44
38
45
public function __construct ($ requestStartTime , $ collectValues )
39
46
{
40
- parent ::__construct ();
47
+ parent ::__construct ($ requestStartTime );
41
48
42
49
$ this ->collectValues = $ collectValues ;
43
50
}
@@ -46,8 +53,7 @@ public function onCacheEvent($event)
46
53
{
47
54
$ class = get_class ($ event );
48
55
$ params = get_object_vars ($ event );
49
-
50
- $ label = $ this ->classMap [$ class ];
56
+ $ label = $ this ->classMap [$ class ][0 ];
51
57
52
58
if (isset ($ params ['value ' ])) {
53
59
if ($ this ->collectValues ) {
@@ -61,7 +67,6 @@ public function onCacheEvent($event)
61
67
}
62
68
}
63
69
64
-
65
70
if (!empty ($ params ['key ' ] ?? null ) && in_array ($ label , ['hit ' , 'written ' ])) {
66
71
$ params ['delete ' ] = route ('debugbar.cache.delete ' , [
67
72
'key ' => urlencode ($ params ['key ' ]),
@@ -70,14 +75,38 @@ public function onCacheEvent($event)
70
75
}
71
76
72
77
$ 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 );
74
94
}
75
95
76
96
public function subscribe (Dispatcher $ dispatcher )
77
97
{
78
98
foreach (array_keys ($ this ->classMap ) as $ eventClass ) {
79
99
$ dispatcher ->listen ($ eventClass , [$ this , 'onCacheEvent ' ]);
80
100
}
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
+ }
81
110
}
82
111
83
112
public function collect ()
0 commit comments