Skip to content

Commit 37fe48b

Browse files
WordPress#718: object caching info
1 parent 31c6ee9 commit 37fe48b

File tree

3 files changed

+156
-0
lines changed

3 files changed

+156
-0
lines changed

plugins/performance-lab/includes/site-health/load.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,7 @@
2525
// AVIF Support site health check.
2626
require_once __DIR__ . '/avif-support/helper.php';
2727
require_once __DIR__ . '/avif-support/hooks.php';
28+
29+
// Object Cache Support site health info.
30+
require_once __DIR__ . '/object-cache/helper.php';
31+
require_once __DIR__ . '/object-cache/hooks.php';
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
<?php
2+
/**
3+
* Helper functions used for Object Cache Support Info.
4+
*
5+
* @package performance-lab
6+
* @since 2.1.0
7+
*/
8+
9+
if ( ! defined( 'ABSPATH' ) ) {
10+
exit; // Exit if accessed directly.
11+
}
12+
13+
/**
14+
* Callback for Object Cache Info fields.
15+
*
16+
* @return array Fields.
17+
* @since 3.3.0
18+
*/
19+
function object_cache_supported_fields() {
20+
return array(
21+
'extension' => array(
22+
'label' => __( 'Extension', 'performance-lab' ),
23+
'value' => wp_get_cache_type(),
24+
),
25+
'multiple_gets' => array(
26+
'label' => __( 'Multiple gets', 'performance-lab' ),
27+
'value' => wp_cache_supports( 'get_multiple' ) ? 'Enabled' : 'Disabled',
28+
),
29+
'multiple_sets' => array(
30+
'label' => __( 'Multiple sets', 'performance-lab' ),
31+
'value' => wp_cache_supports( 'set_multiple' ) ? 'Enabled' : 'Disabled',
32+
),
33+
'multiple_deletes' => array(
34+
'label' => __( 'Multiple deletes', 'performance-lab' ),
35+
'value' => wp_cache_supports( 'delete_multiple' ) ? 'Enabled' : 'Disabled',
36+
),
37+
'flush_group' => array(
38+
'label' => __( 'Flush group', 'performance-lab' ),
39+
'value' => wp_cache_supports( 'flush_group' ) ? 'Enabled' : 'Disabled',
40+
),
41+
);
42+
}
43+
44+
/**
45+
* Attempts to determine which object cache is being used.
46+
*
47+
* Note that the guesses made by this function are based on the WP_Object_Cache classes
48+
* that define the 3rd party object cache extension. Changes to those classes could render
49+
* problems with this function's ability to determine which object cache is being used.
50+
*
51+
* @return string
52+
* @since 3.3.0
53+
*/
54+
function wp_get_cache_type() {
55+
global $_wp_using_ext_object_cache, $wp_object_cache;
56+
57+
if ( ! empty( $_wp_using_ext_object_cache ) ) {
58+
// Test for Memcached PECL extension memcached object cache (https://github.com/tollmanz/wordpress-memcached-backend)
59+
if ( isset( $wp_object_cache->m ) && $wp_object_cache->m instanceof \Memcached ) {
60+
$message = 'Memcached';
61+
62+
// Test for Memcache PECL extension memcached object cache (https://wordpress.org/extend/plugins/memcached/)
63+
} elseif ( isset( $wp_object_cache->mc ) ) {
64+
$is_memcache = true;
65+
foreach ( $wp_object_cache->mc as $bucket ) {
66+
if ( ! $bucket instanceof \Memcache && ! $bucket instanceof \Memcached ) {
67+
$is_memcache = false;
68+
}
69+
}
70+
71+
if ( $is_memcache ) {
72+
$message = 'Memcache';
73+
}
74+
75+
// Test for Xcache object cache (https://plugins.svn.wordpress.org/xcache/trunk/object-cache.php)
76+
} elseif ( $wp_object_cache instanceof \XCache_Object_Cache ) {
77+
$message = 'Xcache';
78+
79+
// Test for WinCache object cache (https://wordpress.org/extend/plugins/wincache-object-cache-backend/)
80+
} elseif ( class_exists( 'WinCache_Object_Cache' ) ) {
81+
$message = 'WinCache';
82+
83+
// Test for APC object cache (https://wordpress.org/extend/plugins/apc/)
84+
} elseif ( class_exists( 'APC_Object_Cache' ) ) {
85+
$message = 'APC';
86+
87+
// Test for WP Redis (https://wordpress.org/plugins/wp-redis/)
88+
} elseif ( isset( $wp_object_cache->redis ) && $wp_object_cache->redis instanceof \Redis ) {
89+
$message = 'Redis';
90+
91+
// Test for Redis Object Cache (https://wordpress.org/plugins/redis-cache/)
92+
} elseif ( method_exists( $wp_object_cache, 'redis_instance' ) && method_exists( $wp_object_cache,
93+
'redis_status' ) ) {
94+
$message = 'Redis';
95+
96+
// Test for Object Cache Pro (https://objectcache.pro/)
97+
} elseif ( method_exists( $wp_object_cache, 'config' ) && method_exists( $wp_object_cache, 'connection' ) ) {
98+
$message = 'Redis';
99+
100+
// Test for WP LCache Object cache (https://github.com/lcache/wp-lcache)
101+
} elseif ( isset( $wp_object_cache->lcache ) && $wp_object_cache->lcache instanceof \LCache\Integrated ) {
102+
$message = 'WP LCache';
103+
104+
} elseif ( function_exists( 'w3_instance' ) ) {
105+
$config = w3_instance( 'W3_Config' );
106+
$message = 'Unknown';
107+
108+
if ( $config->get_boolean( 'objectcache.enabled' ) ) {
109+
$message = 'W3TC ' . $config->get_string( 'objectcache.engine' );
110+
}
111+
} else {
112+
$message = 'Unknown';
113+
}
114+
} else {
115+
$message = 'Disabled';
116+
}
117+
118+
return $message;
119+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
/**
3+
* Hook callbacks used for Site Health Info.
4+
*
5+
* @package performance-lab
6+
* @since 2.1.0
7+
*/
8+
9+
if ( ! defined( 'ABSPATH' ) ) {
10+
exit; // Exit if accessed directly.
11+
}
12+
13+
/**
14+
* Adds Object Cache module to Site Health Info.
15+
*
16+
* @param array $info Site Health Info.
17+
*
18+
* @return array Amended Info.
19+
* @since 3.3.0
20+
*
21+
*/
22+
function object_cache_supported_info( array $info ): array {
23+
$info['object_cache'] = array(
24+
'label' => __( 'Object Caching', 'performance-lab' ),
25+
'description' => __( 'Shows which features object cache supports and if object caching is in use.',
26+
'performance-lab' ),
27+
'fields' => object_cache_supported_fields(),
28+
);
29+
30+
return $info;
31+
}
32+
33+
add_filter( 'debug_information', 'object_cache_supported_info', 10, 1 );

0 commit comments

Comments
 (0)