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
|
<?php declare(strict_types = 1); /** * Object cache collector. * * @package query-monitor */
if ( ! defined( 'ABSPATH' ) ) { exit; }
/** * @extends QM_DataCollector<QM_Data_Cache> */ class QM_Collector_Cache extends QM_DataCollector {
public $id = 'cache';
public function get_storage(): QM_Data { return new QM_Data_Cache(); }
/** * @return void */ public function process() { global $wp_object_cache;
$this->data->has_object_cache = (bool) wp_using_ext_object_cache(); $this->data->cache_hit_percentage = 0;
if ( is_object( $wp_object_cache ) ) { $object_vars = get_object_vars( $wp_object_cache );
if ( array_key_exists( 'cache_hits', $object_vars ) ) { $this->data->stats['cache_hits'] = (int) $object_vars['cache_hits']; }
if ( array_key_exists( 'cache_misses', $object_vars ) ) { $this->data->stats['cache_misses'] = (int) $object_vars['cache_misses']; }
$stats = array();
if ( method_exists( $wp_object_cache, 'getStats' ) ) { $stats = $wp_object_cache->getStats(); } elseif ( array_key_exists( 'stats', $object_vars ) && is_array( $object_vars['stats'] ) ) { $stats = $object_vars['stats']; } elseif ( function_exists( 'wp_cache_get_stats' ) ) { $stats = wp_cache_get_stats(); }
if ( ! empty( $stats ) ) { if ( is_array( $stats ) && ! isset( $stats['get_hits'] ) && 1 === count( $stats ) ) { $first_server = reset( $stats ); if ( isset( $first_server['get_hits'] ) ) { $stats = $first_server; } }
foreach ( $stats as $key => $value ) { if ( ! is_scalar( $value ) ) { continue; } if ( ! is_string( $key ) ) { continue; } $this->data->stats[ $key ] = $value; } }
if ( ! isset( $this->data->stats['cache_hits'] ) ) { if ( isset( $this->data->stats['get_hits'] ) ) { $this->data->stats['cache_hits'] = (int) $this->data->stats['get_hits']; } }
if ( ! isset( $this->data->stats['cache_misses'] ) ) { if ( isset( $this->data->stats['get_misses'] ) ) { $this->data->stats['cache_misses'] = (int) $this->data->stats['get_misses']; } } }
if ( ! empty( $this->data->stats['cache_hits'] ) ) { $total = $this->data->stats['cache_hits'];
if ( ! empty( $this->data->stats['cache_misses'] ) ) { $total += $this->data->stats['cache_misses']; }
$this->data->cache_hit_percentage = ( 100 / $total ) * $this->data->stats['cache_hits']; }
$this->data->display_hit_rate_warning = ( 100 === $this->data->cache_hit_percentage );
if ( function_exists( 'extension_loaded' ) ) { $this->data->object_cache_extensions = array_map( 'extension_loaded', array( 'Afterburner' => 'afterburner', 'Relay' => 'relay', 'Redis' => 'redis', 'Memcached' => 'memcached', 'Memcache' => 'memcache', 'APCu' => 'apcu', ) ); $this->data->opcode_cache_extensions = array_map( 'extension_loaded', array( 'APC' => 'APC', 'Zend OPcache' => 'Zend OPcache', ) ); } else { $this->data->object_cache_extensions = array(); $this->data->opcode_cache_extensions = array(); }
$this->data->has_opcode_cache = array_filter( $this->data->opcode_cache_extensions ) ? true : false; }
}
/** * @param array<string, QM_Collector> $collectors * @param QueryMonitor $qm * @return array<string, QM_Collector> */ function register_qm_collector_cache( array $collectors, QueryMonitor $qm ) { $collectors['cache'] = new QM_Collector_Cache(); return $collectors; }
add_filter( 'qm/collectors', 'register_qm_collector_cache', 20, 2 );
|