Last active
June 14, 2023 01:51
-
-
Save timnolte/267e2a502f05156c0fe2a0d7028c2854 to your computer and use it in GitHub Desktop.
WP Redis Plugin Patch
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
diff --git a/object-cache.php b/object-cache.php | |
index 05c6526..8bfb7cb 100644 | |
--- a/object-cache.php | |
+++ b/object-cache.php | |
@@ -1238,29 +1238,33 @@ class WP_Object_Cache { | |
* with defaults applied. | |
*/ | |
public function build_client_parameters( $redis_server ) { | |
+ // Default Redis port. | |
+ $port = 6379; | |
+ // Default Redis database number. | |
+ $database = 0; | |
+ | |
if ( empty( $redis_server ) ) { | |
// Attempt to automatically load Pantheon's Redis config from the env. | |
if ( isset( $_SERVER['CACHE_HOST'] ) ) { | |
$redis_server = [ | |
'host' => wp_strip_all_tags( $_SERVER['CACHE_HOST'] ), | |
- 'port' => isset( $_SERVER['CACHE_PORT'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PORT'] ) : 0, | |
- 'auth' => isset( $_SERVER['CACHE_PASSWORD'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PASSWORD'] ) : '', | |
- 'database' => isset( $_SERVER['CACHE_DB'] ) ? wp_strip_all_tags( $_SERVER['CACHE_DB'] ) : 0, | |
+ 'port' => ! empty( $_SERVER['CACHE_PORT'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PORT'] ) : $port, | |
+ 'auth' => ! empty( $_SERVER['CACHE_PASSWORD'] ) ? wp_strip_all_tags( $_SERVER['CACHE_PASSWORD'] ) : null, | |
+ 'database' => ! empty( $_SERVER['CACHE_DB'] ) ? wp_strip_all_tags( $_SERVER['CACHE_DB'] ) : $database, | |
]; | |
} else { | |
$redis_server = [ | |
'host' => '127.0.0.1', | |
- 'port' => 6379, | |
- 'database' => 0, | |
+ 'port' => $port, | |
+ 'database' => $database, | |
]; | |
} | |
} | |
if ( file_exists( $redis_server['host'] ) && 'socket' === filetype( $redis_server['host'] ) ) { // unix socket connection. | |
// port must be null or socket won't connect. | |
+ unset( $redis_server['port'] ); | |
$port = null; | |
- } else { // tcp connection. | |
- $port = ! empty( $redis_server['port'] ) ? $redis_server['port'] : 6379; | |
} | |
$defaults = [ | |
@@ -1272,7 +1276,7 @@ class WP_Object_Cache { | |
// 1s timeout, 100ms delay between reconnections. | |
// merging the defaults with the original $redis_server enables any custom parameters to get sent downstream to the redis client. | |
- return array_replace_recursive( $redis_server, $defaults ); | |
+ return array_replace_recursive( $defaults, $redis_server ); | |
} | |
/** | |
diff --git a/wp-redis.php b/wp-redis.php | |
index 58f3d1c..b4812f4 100644 | |
--- a/wp-redis.php | |
+++ b/wp-redis.php | |
@@ -35,21 +35,25 @@ if ( defined( 'WP_CLI' ) && WP_CLI && ! class_exists( 'WP_Redis_CLI_Command' ) ) | |
*/ | |
function wp_redis_get_info() { | |
global $wp_object_cache, $redis_server; | |
+ // Default Redis port. | |
+ $port = 6379; | |
+ // Default Redis database number. | |
+ $database = 0; | |
if ( empty( $redis_server ) ) { | |
// Attempt to automatically load Pantheon's Redis config from the env. | |
- if ( isset( $_SERVER['CACHE_HOST'] ) && isset( $_SERVER['CACHE_PORT'] ) && isset( $_SERVER['CACHE_PASSWORD'] ) && isset( $_SERVER['CACHE_DB'] ) ) { | |
+ if ( isset( $_SERVER['CACHE_HOST'] ) ) { | |
$redis_server = [ | |
'host' => sanitize_text_field( $_SERVER['CACHE_HOST'] ), | |
- 'port' => sanitize_text_field( $_SERVER['CACHE_PORT'] ), | |
- 'auth' => sanitize_text_field( $_SERVER['CACHE_PASSWORD'] ), | |
- 'database' => sanitize_text_field( $_SERVER['CACHE_DB'] ), | |
+ 'port' => ! empty( $_SERVER['CACHE_PORT'] ) ? sanitize_text_field( $_SERVER['CACHE_PORT'] ) : $port, | |
+ 'auth' => ! empty( $_SERVER['CACHE_PASSWORD'] ) ? sanitize_text_field( $_SERVER['CACHE_PASSWORD'] ) : null, | |
+ 'database' => ! empty( $_SERVER['CACHE_DB'] ) ? sanitize_text_field( $_SERVER['CACHE_DB'] ) : $database, | |
]; | |
} else { | |
$redis_server = [ | |
'host' => '127.0.0.1', | |
- 'port' => 6379, | |
- 'database' => 0, | |
+ 'port' => $port, | |
+ 'database' => $database, | |
]; | |
} | |
} | |
@@ -73,7 +77,7 @@ function wp_redis_get_info() { | |
} else { | |
$uptime_in_days .= ' days'; | |
} | |
- $database = ! empty( $redis_server['database'] ) ? $redis_server['database'] : 0; | |
+ $database = ! empty( $redis_server['database'] ) ? $redis_server['database'] : $database; | |
$key_count = 0; | |
if ( isset( $info[ 'db' . $database ] ) && preg_match( '#keys=([\d]+)#', $info[ 'db' . $database ], $matches ) ) { | |
$key_count = $matches[1]; | |
@@ -86,8 +90,12 @@ function wp_redis_get_info() { | |
'instantaneous_ops' => $info['instantaneous_ops_per_sec'] . '/sec', | |
'lifetime_hitrate' => round( ( $info['keyspace_hits'] / ( $info['keyspace_hits'] + $info['keyspace_misses'] ) * 100 ), 2 ) . '%', | |
'redis_host' => $redis_server['host'], | |
- 'redis_port' => ! empty( $redis_server['port'] ) ? $redis_server['port'] : 6379, | |
- 'redis_auth' => ! empty( $redis_server['auth'] ) ? $redis_server['auth'] : '', | |
+ 'redis_port' => ! empty( $redis_server['port'] ) ? $redis_server['port'] : $port, | |
+ 'redis_auth' => ! empty( $redis_server['auth'] ) ? $redis_server['auth'] : null, | |
'redis_database' => $database, | |
+ 'redis_version' => $info['redis_version'], | |
+ 'redis_mode' => $info['redis_mode'], | |
+ 'maxclients' => $info['maxclients'], | |
+ 'connected_clients' => $info['connected_clients'], | |
]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment