From e65cb1ec34eb0b2ed730db63aa74e950ee7db945 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Mon, 8 Jul 2024 19:24:21 +0800 Subject: [PATCH 01/12] poc 1 --- admin/class-phpredis-purger.php | 220 +--------------------------- admin/class-predis-purger.php | 213 +-------------------------- admin/class-redis-purge-traits.php | 227 +++++++++++++++++++++++++++++ composer.json | 2 +- includes/class-nginx-helper.php | 2 + 5 files changed, 234 insertions(+), 430 deletions(-) create mode 100644 admin/class-redis-purge-traits.php diff --git a/admin/class-phpredis-purger.php b/admin/class-phpredis-purger.php index b297d6a3..6626c727 100644 --- a/admin/class-phpredis-purger.php +++ b/admin/class-phpredis-purger.php @@ -18,6 +18,8 @@ */ class PhpRedis_Purger extends Purger { + use Redis_Purge_Traits; + /** * PHP Redis api object. * @@ -80,222 +82,4 @@ public function __construct() { } - /** - * Purge all cache. - */ - public function purge_all() { - - global $nginx_helper_admin; - - $prefix = trim( $nginx_helper_admin->options['redis_prefix'] ); - - $this->log( '* * * * *' ); - - // If Purge Cache link click from network admin then purge all. - if ( is_network_admin() ) { - - $total_keys_purged = $this->delete_keys_by_wildcard( $prefix . '*' ); - $this->log( '* Purged Everything! * ' ); - - } else { // Else purge only site specific cache. - - $parse = wp_parse_url( get_home_url() ); - $parse['path'] = empty( $parse['path'] ) ? '/' : $parse['path']; - $total_keys_purged = $this->delete_keys_by_wildcard( $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'] . '*' ); - $this->log( '* ' . get_home_url() . ' Purged! * ' ); - - } - - if ( $total_keys_purged ) { - $this->log( "Total {$total_keys_purged} urls purged." ); - } else { - $this->log( 'No Cache found.' ); - } - - $this->log( '* * * * *' ); - - /** - * Fire an action after the Redis cache has been purged. - * - * @since 2.1.0 - */ - do_action( 'rt_nginx_helper_after_redis_purge_all' ); - } - - /** - * Purge url. - * - * @param string $url URL to purge. - * @param bool $feed Feed or not. - */ - public function purge_url( $url, $feed = true ) { - - global $nginx_helper_admin; - - /** - * Filters the URL to be purged. - * - * @since 2.1.0 - * - * @param string $url URL to be purged. - */ - $url = apply_filters( 'rt_nginx_helper_purge_url', $url ); - - $parse = wp_parse_url( $url ); - - if ( ! isset( $parse['path'] ) ) { - $parse['path'] = ''; - } - - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; - - /** - * To delete device type caches such as `--mobile`, `--desktop`, `--lowend`, etc. - * This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache. - * - * For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL. - * Add this filter in separate plugin or simply in theme's function.php file: - * ``` - * add_filter( 'rt_nginx_helper_purge_url', function( $url ) { - * $url = $url . '--*'; - * return $url; - * }); - * ``` - * - * Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted. - * - * @since 2.1.0 - */ - if ( strpos( $_url_purge_base, '*' ) === false ) { - - $status = $this->delete_single_key( $_url_purge_base ); - - if ( $status ) { - $this->log( '- Purge URL | ' . $_url_purge_base ); - } else { - $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); - } - } else { - - $status = $this->delete_keys_by_wildcard( $_url_purge_base ); - - if ( $status ) { - $this->log( '- Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' ); - } else { - $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); - } - } - - $this->log( '* * * * *' ); - - } - - /** - * Custom purge urls. - */ - public function custom_purge_urls() { - - global $nginx_helper_admin; - - $parse = wp_parse_url( home_url() ); - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; - - $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? - explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); - - /** - * Allow plugins/themes to modify/extend urls. - * - * @param array $purge_urls URLs which needs to be purged. - * @param bool $wildcard If wildcard in url is allowed or not. default true. - */ - $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); - - if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { - - foreach ( $purge_urls as $purge_url ) { - - $purge_url = trim( $purge_url ); - - if ( strpos( $purge_url, '*' ) === false ) { - - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_single_key( $purge_url ); - - if ( $status ) { - $this->log( '- Purge URL | ' . $purge_url ); - } else { - $this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' ); - } - } else { - - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_keys_by_wildcard( $purge_url ); - - if ( $status ) { - $this->log( '- Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' ); - } else { - $this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' ); - } - } - } - } - - } - - /** - * Single Key Delete Example - * e.g. $key can be nginx-cache:httpGETexample.com/ - * - * @param string $key Key. - * - * @return int - */ - public function delete_single_key( $key ) { - - try { - return $this->redis_object->del( $key ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - - /** - * Delete Keys by wildcard. - * e.g. $key can be nginx-cache:httpGETexample.com* - * - * Lua Script block to delete multiple keys using wildcard - * Script will return count i.e. number of keys deleted - * if return value is 0, that means no matches were found - * - * Call redis eval and return value from lua script - * - * @param string $pattern pattern. - * - * @return mixed - */ - public function delete_keys_by_wildcard( $pattern ) { - - // Lua Script. - $lua = <<redis_object->eval( $lua, array( $pattern ), 1 ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - } diff --git a/admin/class-predis-purger.php b/admin/class-predis-purger.php index 1f4771f2..874ba3de 100644 --- a/admin/class-predis-purger.php +++ b/admin/class-predis-purger.php @@ -14,6 +14,8 @@ */ class Predis_Purger extends Purger { + use Redis_Purge_Traits; + /** * Predis api object. * @@ -69,215 +71,4 @@ public function __construct() { } - /** - * Purge all. - */ - public function purge_all() { - - global $nginx_helper_admin; - - $prefix = trim( $nginx_helper_admin->options['redis_prefix'] ); - - $this->log( '* * * * *' ); - - // If Purge Cache link click from network admin then purge all. - if ( is_network_admin() ) { - - $this->delete_keys_by_wildcard( $prefix . '*' ); - $this->log( '* Purged Everything! * ' ); - - } else { // Else purge only site specific cache. - - $parse = wp_parse_url( get_home_url() ); - $parse['path'] = empty( $parse['path'] ) ? '/' : $parse['path']; - $this->delete_keys_by_wildcard( $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'] . '*' ); - $this->log( '* ' . get_home_url() . ' Purged! * ' ); - - } - - $this->log( '* * * * *' ); - - /** - * Fire an action after the Redis cache has been purged. - * - * @since 2.1.0 - */ - do_action( 'rt_nginx_helper_after_redis_purge_all' ); - } - - /** - * Purge url. - * - * @param string $url URL. - * @param bool $feed Feed or not. - */ - public function purge_url( $url, $feed = true ) { - - global $nginx_helper_admin; - - /** - * Filters the URL to be purged. - * - * @since 2.1.0 - * - * @param string $url URL to be purged. - */ - $url = apply_filters( 'rt_nginx_helper_purge_url', $url ); - - $this->log( '- Purging URL | ' . $url ); - - $parse = wp_parse_url( $url ); - - if ( ! isset( $parse['path'] ) ) { - $parse['path'] = ''; - } - - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; - - /** - * To delete device type caches such as `--mobile`, `--desktop`, `--lowend`, etc. - * This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache. - * - * For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL. - * Add this filter in separate plugin or simply in theme's function.php file: - * ``` - * add_filter( 'rt_nginx_helper_purge_url', function( $url ) { - * $url = $url . '--*'; - * return $url; - * }); - * ``` - * - * Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted. - * - * @since 2.1.0 - */ - if ( strpos( $_url_purge_base, '*' ) === false ) { - - $status = $this->delete_single_key( $_url_purge_base ); - - if ( $status ) { - $this->log( '- Purge URL | ' . $_url_purge_base ); - } else { - $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); - } - } else { - - $status = $this->delete_keys_by_wildcard( $_url_purge_base ); - - if ( $status ) { - $this->log( '- Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' ); - } else { - $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); - } - } - - } - - /** - * Custom purge urls. - */ - public function custom_purge_urls() { - - global $nginx_helper_admin; - - $parse = wp_parse_url( home_url() ); - $prefix = $nginx_helper_admin->options['redis_prefix']; - $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; - - $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? - explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); - - /** - * Allow plugins/themes to modify/extend urls. - * - * @param array $purge_urls URLs which needs to be purged. - * @param bool $wildcard If wildcard in url is allowed or not. default true. - */ - $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); - - if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { - - foreach ( $purge_urls as $purge_url ) { - - $purge_url = trim( $purge_url ); - - if ( strpos( $purge_url, '*' ) === false ) { - - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_single_key( $purge_url ); - if ( $status ) { - $this->log( '- Purge URL | ' . $purge_url ); - } else { - $this->log( '- Not Found | ' . $purge_url, 'ERROR' ); - } - } else { - - $purge_url = $_url_purge_base . $purge_url; - $status = $this->delete_keys_by_wildcard( $purge_url ); - - if ( $status ) { - $this->log( '- Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' ); - } else { - $this->log( '- Not Found | ' . $purge_url, 'ERROR' ); - } - } - } - } - - } - - /** - * Single Key Delete Example - * e.g. $key can be nginx-cache:httpGETexample.com/ - * - * @param string $key Key to delete cache. - * - * @return mixed - */ - public function delete_single_key( $key ) { - - try { - return $this->redis_object->executeRaw( array( 'DEL', $key ) ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - - /** - * Delete Keys by wildcard. - * e.g. $key can be nginx-cache:httpGETexample.com* - * - * Lua Script block to delete multiple keys using wildcard - * Script will return count i.e. number of keys deleted - * if return value is 0, that means no matches were found - * - * Call redis eval and return value from lua script - * - * @param string $pattern Pattern. - * - * @return mixed - */ - public function delete_keys_by_wildcard( $pattern ) { - - // Lua Script. - $lua = <<redis_object->eval( $lua, 1, $pattern ); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - } diff --git a/admin/class-redis-purge-traits.php b/admin/class-redis-purge-traits.php new file mode 100644 index 00000000..3b8855ae --- /dev/null +++ b/admin/class-redis-purge-traits.php @@ -0,0 +1,227 @@ +options['redis_prefix'] ); + + $this->log( '* * * * *' ); + + // If Purge Cache link click from network admin then purge all. + if ( is_network_admin() ) { + + $total_keys_purged = $this->delete_keys_by_wildcard( $prefix . '*' ); + $this->log( '* Purged Everything! * ' ); + + } else { // Else purge only site specific cache. + + $parse = wp_parse_url( get_home_url() ); + $parse['path'] = empty( $parse['path'] ) ? '/' : $parse['path']; + $total_keys_purged = $this->delete_keys_by_wildcard( $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path'] . '*' ); + $this->log( '* ' . get_home_url() . ' Purged! * ' ); + + } + + if ( $total_keys_purged ) { + $this->log( "Total {$total_keys_purged} urls purged." ); + } else { + $this->log( 'No Cache found.' ); + } + + $this->log( '* * * * *' ); + + /** + * Fire an action after the Redis cache has been purged. + * + * @since 2.1.0 + */ + do_action( 'rt_nginx_helper_after_redis_purge_all' ); + } + + /** + * Purge url. + * + * @param string $url URL to purge. + * @param bool $feed Feed or not. + */ + public function purge_url( $url, $feed = true ) { + + global $nginx_helper_admin; + + /** + * Filters the URL to be purged. + * + * @since 2.1.0 + * + * @param string $url URL to be purged. + */ + $url = apply_filters( 'rt_nginx_helper_purge_url', $url ); + + $parse = wp_parse_url( $url ); + + if ( ! isset( $parse['path'] ) ) { + $parse['path'] = ''; + } + + $prefix = $nginx_helper_admin->options['redis_prefix']; + $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host'] . $parse['path']; + + /** + * To delete device type caches such as `--mobile`, `--desktop`, `--lowend`, etc. + * This would need $url above to be changed with this filter `rt_nginx_helper_purge_url` by cache key that Nginx sets while generating cache. + * + * For example: If page is accessed from desktop, then cache will be generated by appending `--desktop` to current URL. + * Add this filter in separate plugin or simply in theme's function.php file: + * ``` + * add_filter( 'rt_nginx_helper_purge_url', function( $url ) { + * $url = $url . '--*'; + * return $url; + * }); + * ``` + * + * Regardless of what key / suffix is being to store `$device_type` cache , it will be deleted. + * + * @since 2.1.0 + */ + if ( strpos( $_url_purge_base, '*' ) === false ) { + + $status = $this->delete_single_key( $_url_purge_base ); + + if ( $status ) { + $this->log( '- Purge URL | ' . $_url_purge_base ); + } else { + $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); + } + } else { + + $status = $this->delete_keys_by_wildcard( $_url_purge_base ); + + if ( $status ) { + $this->log( '- Purge Wild Card URL | ' . $_url_purge_base . ' | ' . $status . ' url purged' ); + } else { + $this->log( '- Cache Not Found | ' . $_url_purge_base, 'ERROR' ); + } + } + + $this->log( '* * * * *' ); + + } + + /** + * Custom purge urls. + */ + public function custom_purge_urls() { + + global $nginx_helper_admin; + + $parse = wp_parse_url( home_url() ); + $prefix = $nginx_helper_admin->options['redis_prefix']; + $_url_purge_base = $prefix . $parse['scheme'] . 'GET' . $parse['host']; + + $purge_urls = isset( $nginx_helper_admin->options['purge_url'] ) && ! empty( $nginx_helper_admin->options['purge_url'] ) ? + explode( "\r\n", $nginx_helper_admin->options['purge_url'] ) : array(); + + /** + * Allow plugins/themes to modify/extend urls. + * + * @param array $purge_urls URLs which needs to be purged. + * @param bool $wildcard If wildcard in url is allowed or not. default true. + */ + $purge_urls = apply_filters( 'rt_nginx_helper_purge_urls', $purge_urls, true ); + + if ( is_array( $purge_urls ) && ! empty( $purge_urls ) ) { + + foreach ( $purge_urls as $purge_url ) { + + $purge_url = trim( $purge_url ); + + if ( strpos( $purge_url, '*' ) === false ) { + + $purge_url = $_url_purge_base . $purge_url; + $status = $this->delete_single_key( $purge_url ); + + if ( $status ) { + $this->log( '- Purge URL | ' . $purge_url ); + } else { + $this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' ); + } + } else { + + $purge_url = $_url_purge_base . $purge_url; + $status = $this->delete_keys_by_wildcard( $purge_url ); + + if ( $status ) { + $this->log( '- Purge Wild Card URL | ' . $purge_url . ' | ' . $status . ' url purged' ); + } else { + $this->log( '- Cache Not Found | ' . $purge_url, 'ERROR' ); + } + } + } + } + + } + + /** + * Single Key Delete Example + * e.g. $key can be nginx-cache:httpGETexample.com/ + * + * @param string $key Key. + * + * @return int + */ + public function delete_single_key( $key ) { + + try { + if ( class_exists( 'Redis' ) ) { + return $this->redis_object->del($key); + } else { + return $this->redis_object->executeRaw( array( 'DEL', $key ) ); + } + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + + } + + /** + * Delete Keys by wildcard. + * e.g. $key can be nginx-cache:httpGETexample.com* + * + * Lua Script block to delete multiple keys using wildcard + * Script will return count i.e. number of keys deleted + * if return value is 0, that means no matches were found + * + * Call redis eval and return value from lua script + * + * @param string $pattern pattern. + * + * @return mixed + */ + public function delete_keys_by_wildcard( $pattern ) { + + // Lua Script. + $lua = <<redis_object->eval( $lua, array( $pattern ), 1 ); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + + } + +} \ No newline at end of file diff --git a/composer.json b/composer.json index f215446d..f0742c19 100644 --- a/composer.json +++ b/composer.json @@ -19,7 +19,7 @@ "source": "https://github.com/rtCamp/nginx-helper/" }, "require": { - "php": ">=5.3.2", + "php": ">=5.4", "composer/installers": "^1.0" }, "require-dev": { diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index f96e6d04..d131280a 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -173,6 +173,8 @@ private function define_admin_hooks() { // Defines global variables. if ( ! empty( $nginx_helper_admin->options['cache_method'] ) && 'enable_redis' === $nginx_helper_admin->options['cache_method'] ) { + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purge-traits.php'; + if ( class_exists( 'Redis' ) ) { // Use PHP5-Redis extension if installed. require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-phpredis-purger.php'; From 18dc9e22e6f0eb4721d6a7a7e927aecb5899f1b4 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Mon, 8 Jul 2024 19:40:33 +0800 Subject: [PATCH 02/12] release-relay-b --- admin/class-phpredis-purger.php | 85 ------------ admin/class-predis-purger.php | 74 ---------- ...urge-traits.php => class-redis-purger.php} | 129 +++++++++++++++++- includes/class-nginx-helper.php | 15 +- 4 files changed, 131 insertions(+), 172 deletions(-) delete mode 100644 admin/class-phpredis-purger.php delete mode 100644 admin/class-predis-purger.php rename admin/{class-redis-purge-traits.php => class-redis-purger.php} (64%) diff --git a/admin/class-phpredis-purger.php b/admin/class-phpredis-purger.php deleted file mode 100644 index 6626c727..00000000 --- a/admin/class-phpredis-purger.php +++ /dev/null @@ -1,85 +0,0 @@ -redis_object = new Redis(); - - /*Composer sets default to version that doesn't allow modern php*/ - $redis_connection_others_array = array(); - - $path = $nginx_helper_admin->options['redis_unix_socket']; - - if ( $path ) { - $host = $path; - $port = 0; - } else { - $host = $nginx_helper_admin->options['redis_hostname']; - $port = $nginx_helper_admin->options['redis_port']; - } - - $username = $nginx_helper_admin->options['redis_username']; - $password = $nginx_helper_admin->options['redis_password']; - - if ( $username && $password ) { - $redis_connection_others_array['auth'] = [$username, $password]; - } - - $this->redis_object->connect( - $host, - $port, - 5, - '', - 100, - 1.5, - $redis_connection_others_array - ); - - $redis_database = $nginx_helper_admin->options['redis_database']; - - $this->redis_object->select($redis_database); - - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - -} diff --git a/admin/class-predis-purger.php b/admin/class-predis-purger.php deleted file mode 100644 index 874ba3de..00000000 --- a/admin/class-predis-purger.php +++ /dev/null @@ -1,74 +0,0 @@ -options['redis_unix_socket']; - $username = $nginx_helper_admin->options['redis_username']; - $password = $nginx_helper_admin->options['redis_password']; - $predis_connection_array['database'] = $nginx_helper_admin->options['redis_database']; - - if ( $path ) { - $predis_connection_array['path'] = $path; - } else { - $predis_connection_array['host'] = $nginx_helper_admin->options['redis_hostname'];; - $predis_connection_array['port'] = $nginx_helper_admin->options['redis_port']; - } - - if ( $username && $password ) { - $predis_connection_array['username'] = $username; - $predis_connection_array['password'] = $password; - } - - // redis server parameter. - $this->redis_object = new Predis\Client( $predis_connection_array ); - - try { - $this->redis_object->connect(); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); - } - - } - -} diff --git a/admin/class-redis-purge-traits.php b/admin/class-redis-purger.php similarity index 64% rename from admin/class-redis-purge-traits.php rename to admin/class-redis-purger.php index 3b8855ae..a9f7e6cd 100644 --- a/admin/class-redis-purge-traits.php +++ b/admin/class-redis-purger.php @@ -1,6 +1,129 @@ php_redis_connector = $php_redis_connector; + + if ( 'phpredis' === $this->php_redis_connector ) { + + try { + + $this->redis_object = new Redis(); + + /*Composer sets default to version that doesn't allow modern php*/ + $redis_connection_others_array = array(); + + $path = $nginx_helper_admin->options['redis_unix_socket']; + + if ($path) { + $host = $path; + $port = 0; + } else { + $host = $nginx_helper_admin->options['redis_hostname']; + $port = $nginx_helper_admin->options['redis_port']; + } + + $username = $nginx_helper_admin->options['redis_username']; + $password = $nginx_helper_admin->options['redis_password']; + + if ($username && $password) { + $redis_connection_others_array['auth'] = [$username, $password]; + } + + $this->redis_object->connect( + $host, + $port, + 5, + '', + 100, + 1.5, + $redis_connection_others_array + ); + + $redis_database = $nginx_helper_admin->options['redis_database']; + + $this->redis_object->select($redis_database); + + } catch (Exception $e) { + $this->log($e->getMessage(), 'ERROR'); + } + + } else { + + if ( ! class_exists( 'Predis\Autoloader' ) ) { + require_once NGINX_HELPER_BASEPATH . 'admin/predis.php'; + } + + Predis\Autoloader::register(); + + /*Composer sets default to version that doesn't allow modern php*/ + $predis_connection_array = array(); + + $path = $nginx_helper_admin->options['redis_unix_socket']; + $username = $nginx_helper_admin->options['redis_username']; + $password = $nginx_helper_admin->options['redis_password']; + $predis_connection_array['database'] = $nginx_helper_admin->options['redis_database']; + + if ( $path ) { + $predis_connection_array['path'] = $path; + } else { + $predis_connection_array['host'] = $nginx_helper_admin->options['redis_hostname'];; + $predis_connection_array['port'] = $nginx_helper_admin->options['redis_port']; + } + + if ( $username && $password ) { + $predis_connection_array['username'] = $username; + $predis_connection_array['password'] = $password; + } + + // redis server parameter. + $this->redis_object = new Predis\Client( $predis_connection_array ); + + try { + $this->redis_object->connect(); + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); + } + + } + + } /** * Purge all cache. @@ -178,7 +301,7 @@ public function custom_purge_urls() { public function delete_single_key( $key ) { try { - if ( class_exists( 'Redis' ) ) { + if ( 'phpredis' === $this->php_redis_connector ) { return $this->redis_object->del($key); } else { return $this->redis_object->executeRaw( array( 'DEL', $key ) ); @@ -224,4 +347,4 @@ public function delete_keys_by_wildcard( $pattern ) { } -} \ No newline at end of file +} diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index d131280a..930f6d09 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -173,19 +173,14 @@ private function define_admin_hooks() { // Defines global variables. if ( ! empty( $nginx_helper_admin->options['cache_method'] ) && 'enable_redis' === $nginx_helper_admin->options['cache_method'] ) { - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purge-traits.php'; - if ( class_exists( 'Redis' ) ) { // Use PHP5-Redis extension if installed. - - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-phpredis-purger.php'; - $nginx_purger = new PhpRedis_Purger(); - + $php_redis_connector = 'phpredis'; } else { - - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-predis-purger.php'; - $nginx_purger = new Predis_Purger(); - + $php_redis_connector = 'predis'; } + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purger.php'; + $nginx_purger = new Redis_Purger( $php_redis_connector ); + } else { require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-fastcgi-purger.php'; From 3b14c234e95b472e776d7dba778e594e8fce2a3e Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Mon, 8 Jul 2024 23:42:01 +0800 Subject: [PATCH 03/12] relay --- admin/class-nginx-helper-admin.php | 121 +++++++++----- admin/class-redis-purger.php | 157 ++++++++++-------- .../partials/nginx-helper-general-options.php | 70 ++++++-- includes/class-nginx-helper.php | 13 +- 4 files changed, 239 insertions(+), 122 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 0dffdfd9..35975d40 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -280,17 +280,18 @@ public function nginx_helper_default_settings() { 'redis_username' => '', 'redis_password' => '', 'redis_database' => 0, + 'redis_timeout' => 5, + 'redis_read_timeout' => 1.5, 'purge_url' => '', - 'auth_enabled_by_constant' => 0, - 'cache_method_set_by_constant' => 0, - 'purge_method_set_by_constant' => 0, - 'redis_hostname_set_by_constant' => 0, - 'redis_port_set_by_constant' => 0, - 'redis_unix_socket_set_by_constant' => 0, - 'redis_prefix_set_by_constant' => 0, - 'redis_database_set_by_constant' => 0, - 'redis_username_set_by_constant' => 0, - 'redis_password_socket_set_by_constant' => 0, + 'cache_method_set_by_constant' => false, + 'purge_method_set_by_constant' => false, + 'redis_hostname_set_by_constant' => false, + 'redis_port_set_by_constant' => false, + 'redis_unix_socket_set_by_constant' => false, + 'redis_prefix_set_by_constant' => false, + 'redis_database_set_by_constant' => false, + 'redis_username_set_by_constant' => false, + 'redis_password_socket_set_by_constant' => false, 'homepage_purge_post_type_exceptions' => array(), ); @@ -317,19 +318,28 @@ public function nginx_helper_settings() { $this->nginx_helper_default_settings() ); - if ( defined( 'RT_WP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS' ) ) { - $data['homepage_purge_post_type_exceptions'] = RT_WP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS; + if ( defined( 'GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS' ) ) { + $data['homepage_purge_post_type_exceptions'] = GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS; } - if ( defined( 'RT_WP_NGINX_HELPER_PURGE_METHOD' ) ) { + if ( defined( 'GP_NGINX_HELPER_PURGE_METHOD' ) ) { + $data['purge_method'] = GP_NGINX_HELPER_PURGE_METHOD; + $data['purge_method_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_PURGE_METHOD' ) ) { $data['purge_method'] = RT_WP_NGINX_HELPER_PURGE_METHOD; - $data['purge_method_set_by_constant'] = 1; + $data['purge_method_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; } - if ( defined( 'RT_WP_NGINX_HELPER_CACHE_METHOD' ) ) { + if ( defined( 'GP_NGINX_HELPER_CACHE_METHOD' ) ) { + $data['cache_method'] = GP_NGINX_HELPER_CACHE_METHOD; + $data['cache_method_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + if ( 'enable_fastcgi' === GP_NGINX_HELPER_CACHE_METHOD ) { + $data['enable_purge'] = 1; + return $data; + } + } elseif ( defined( 'RT_WP_NGINX_HELPER_CACHE_METHOD' ) ) { $data['cache_method'] = RT_WP_NGINX_HELPER_CACHE_METHOD; - $data['cache_method_set_by_constant'] = 1; - + $data['cache_method_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; if ( 'enable_fastcgi' === RT_WP_NGINX_HELPER_CACHE_METHOD ) { $data['enable_purge'] = 1; return $data; @@ -343,43 +353,80 @@ public function nginx_helper_settings() { $redis_port = false; $redis_unix_socket = false; - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) ) { + if ( defined( 'GP_NGINX_HELPER_REDIS_PREFIX' ) ) { + $redis_prefix = GP_NGINX_HELPER_REDIS_PREFIX; + $data['redis_prefix_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) ) { $redis_prefix = RT_WP_NGINX_HELPER_REDIS_PREFIX; - $data['redis_prefix'] = $redis_prefix; - $data['redis_prefix_set_by_constant'] = 1; + $data['redis_prefix_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; } + if ( $redis_prefix ) { + $data['redis_prefix'] = $redis_prefix; + } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) ) { + if ( defined( 'GP_NGINX_HELPER_REDIS_PORT' ) ) { + $redis_port = GP_NGINX_HELPER_REDIS_PORT; + $data['redis_port_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) ) { $redis_port = RT_WP_NGINX_HELPER_REDIS_PORT; - $data['redis_port'] = $redis_port; - $data['redis_port_set_by_constant'] = 1; + $data['redis_port_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; } + if ( $redis_port ) { + $data['redis_port'] = $redis_port; + } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { + if ( defined( 'GP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { + $redis_hostname = GP_NGINX_HELPER_REDIS_HOSTNAME; + $data['redis_hostname_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_HOSTNAME'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { $redis_hostname = RT_WP_NGINX_HELPER_REDIS_HOSTNAME; - $data['redis_hostname'] = $redis_hostname; - $data['redis_hostname_set_by_constant'] = 1; + $data['redis_hostname_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME'; } + if ( $redis_hostname ) { + $data['redis_hostname'] = $redis_hostname; + } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { + if ( defined( 'GP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { + $redis_unix_socket = GP_NGINX_HELPER_REDIS_UNIX_SOCKET; + $data['redis_unix_socket_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_HOSTNAME'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { $redis_unix_socket = RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET; + $data['redis_unix_socket_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME'; + } + if ( $redis_unix_socket ) { $data['redis_unix_socket'] = $redis_unix_socket; - $data['redis_unix_socket_set_by_constant'] = 1; + } + + if ( defined( 'GP_NGINX_HELPER_REDIS_DATABASE' ) ) { + $data['redis_database'] = GP_NGINX_HELPER_REDIS_DATABASE; + $data['redis_database_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_DATABASE'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_DATABASE' ) ) { + $data['redis_database'] = RT_WP_NGINX_HELPER_REDIS_DATABASE; + $data['redis_database_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_DATABASE'; + } + + if ( defined( 'GP_NGINX_HELPER_REDIS_USERNAME' ) ) { + $data['redis_username'] = GP_NGINX_HELPER_REDIS_USERNAME; + $data['redis_username_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_USERNAME'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_USERNAME' ) ) { + $data['redis_username'] = RT_WP_NGINX_HELPER_REDIS_USERNAME; + $data['redis_username_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_USERNAME'; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_DATABASE' ) ) { - $data['redis_database'] = RT_WP_NGINX_HELPER_REDIS_DATABASE; - $data['redis_database_set_by_constant'] = 1; + if ( defined( 'GP_NGINX_HELPER_REDIS_PASSWORD' ) ) { + $data['redis_password'] = GP_NGINX_HELPER_REDIS_PASSWORD; + $data['redis_password_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_PASSWORD'; + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PASSWORD' ) ) { + $data['redis_password'] = RT_WP_NGINX_HELPER_REDIS_PASSWORD; + $data['redis_password_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_PASSWORD'; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_USERNAME' ) ) { - $data['redis_username'] = RT_WP_NGINX_HELPER_REDIS_USERNAME; - $data['redis_username_set_by_constant'] = 1; + if ( defined( 'GP_NGINX_HELPER_REDIS_TIMEOUT' ) ) { + $data['redis_timeout'] = GP_NGINX_HELPER_REDIS_TIMEOUT; } - if ( defined( 'RT_WP_NGINX_HELPER_REDIS_PASSWORD' ) ) { - $data['redis_password'] = RT_WP_NGINX_HELPER_REDIS_PASSWORD; - $data['redis_password_set_by_constant'] = 1; + if ( defined( 'GP_NGINX_HELPER_REDIS_READ_TIMEOUT' ) ) { + $data['redis_read_timeout'] = GP_NGINX_HELPER_REDIS_READ_TIMEOUT; } if ( $redis_prefix && $redis_hostname && $redis_port ) { diff --git a/admin/class-redis-purger.php b/admin/class-redis-purger.php index a9f7e6cd..88c7b713 100644 --- a/admin/class-redis-purger.php +++ b/admin/class-redis-purger.php @@ -27,102 +27,123 @@ class Redis_Purger extends Purger { */ public $redis_object; - public $php_redis_connector; + public $php_redis_client; /** * Initialize the class and set its properties. * * @since 2.0.0 */ - public function __construct( $php_redis_connector ) { + public function __construct( $php_redis_client = 'phpredis' ) { global $nginx_helper_admin; - $this->php_redis_connector = $php_redis_connector; + $this->php_redis_client = $php_redis_client; - if ( 'phpredis' === $this->php_redis_connector ) { + $connection_array = []; + $path = $nginx_helper_admin->options['redis_unix_socket']; + $username = $nginx_helper_admin->options['redis_username']; + $password = $nginx_helper_admin->options['redis_password']; + $redis_database = $nginx_helper_admin->options['redis_database']; - try { + try { - $this->redis_object = new Redis(); + switch ( $php_redis_client ) { - /*Composer sets default to version that doesn't allow modern php*/ - $redis_connection_others_array = array(); + case 'predis': - $path = $nginx_helper_admin->options['redis_unix_socket']; + if ( ! class_exists('Predis\Autoloader')) { + require_once NGINX_HELPER_BASEPATH . 'admin/predis.php'; + } - if ($path) { - $host = $path; - $port = 0; - } else { - $host = $nginx_helper_admin->options['redis_hostname']; - $port = $nginx_helper_admin->options['redis_port']; - } + Predis\Autoloader::register(); - $username = $nginx_helper_admin->options['redis_username']; - $password = $nginx_helper_admin->options['redis_password']; + if ($path) { + $connection_array['path'] = $path; + } else { + $connection_array['host'] = $nginx_helper_admin->options['redis_hostname'];; + $connection_array['port'] = $nginx_helper_admin->options['redis_port']; + } - if ($username && $password) { - $redis_connection_others_array['auth'] = [$username, $password]; - } + if ($username && $password) { + $connection_array['username'] = $username; + $connection_array['password'] = $password; + } - $this->redis_object->connect( - $host, - $port, - 5, - '', - 100, - 1.5, - $redis_connection_others_array - ); + $connection_array['timeout'] = $nginx_helper_admin->options['redis_timeout']; + $connection_array['read_write_timeout'] = $nginx_helper_admin->options['redis_read_timeout']; - $redis_database = $nginx_helper_admin->options['redis_database']; + $this->redis_object = new Predis\Client($connection_array); - $this->redis_object->select($redis_database); + $this->redis_object->connect(); - } catch (Exception $e) { - $this->log($e->getMessage(), 'ERROR'); - } + break; - } else { + case 'relay': - if ( ! class_exists( 'Predis\Autoloader' ) ) { - require_once NGINX_HELPER_BASEPATH . 'admin/predis.php'; - } + $this->redis_object = new Relay\Relay; - Predis\Autoloader::register(); + if ($path) { + $host = $path; + $port = -1; + } else { + $host = $nginx_helper_admin->options['redis_hostname']; + $port = $nginx_helper_admin->options['redis_port']; + } - /*Composer sets default to version that doesn't allow modern php*/ - $predis_connection_array = array(); + $this->redis_object->connect( + $host, + $port, + $nginx_helper_admin->options['redis_timeout'], + '', + 300, + $nginx_helper_admin->options['redis_read_timeout'] + ); + + if ($username && $password) { + $this->redis_object->auth([$username, $password]); + } - $path = $nginx_helper_admin->options['redis_unix_socket']; - $username = $nginx_helper_admin->options['redis_username']; - $password = $nginx_helper_admin->options['redis_password']; - $predis_connection_array['database'] = $nginx_helper_admin->options['redis_database']; + $this->redis_object->select($redis_database); - if ( $path ) { - $predis_connection_array['path'] = $path; - } else { - $predis_connection_array['host'] = $nginx_helper_admin->options['redis_hostname'];; - $predis_connection_array['port'] = $nginx_helper_admin->options['redis_port']; - } + break; - if ( $username && $password ) { - $predis_connection_array['username'] = $username; - $predis_connection_array['password'] = $password; - } + case 'phpredis': - // redis server parameter. - $this->redis_object = new Predis\Client( $predis_connection_array ); + default: - try { - $this->redis_object->connect(); - } catch ( Exception $e ) { - $this->log( $e->getMessage(), 'ERROR' ); + $this->redis_object = new Redis(); + + if ($path) { + $host = $path; + $port = -1; + } else { + $host = $nginx_helper_admin->options['redis_hostname']; + $port = $nginx_helper_admin->options['redis_port']; + } + + if ($username && $password) { + $connection_array['auth'] = [$username, $password]; + } + + $this->redis_object->connect( + $host, + $port, + $nginx_helper_admin->options['redis_timeout'], + '', + 300, + $nginx_helper_admin->options['redis_read_timeout'], + $connection_array + ); + + $this->redis_object->select($redis_database); + + break; } + } catch ( Exception $e ) { + $this->log( $e->getMessage(), 'ERROR' ); } - } /** @@ -287,7 +308,6 @@ public function custom_purge_urls() { } } } - } /** @@ -301,15 +321,15 @@ public function custom_purge_urls() { public function delete_single_key( $key ) { try { - if ( 'phpredis' === $this->php_redis_connector ) { - return $this->redis_object->del($key); - } else { + if ( 'predis' === $this->php_redis_client ) { return $this->redis_object->executeRaw( array( 'DEL', $key ) ); + } else { + return $this->redis_object->del($key); } } catch ( Exception $e ) { $this->log( $e->getMessage(), 'ERROR' ); + return 187; } - } /** @@ -343,6 +363,7 @@ public function delete_keys_by_wildcard( $pattern ) { return $this->redis_object->eval( $lua, array( $pattern ), 1 ); } catch ( Exception $e ) { $this->log( $e->getMessage(), 'ERROR' ); + return 187; } } diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index 6d0ff5da..52c332cd 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -196,7 +196,8 @@ echo '

'; esc_html_e( sprintf( - __("Set by wp-config.php constant: define( 'RT_WP_NGINX_HELPER_CACHE_METHOD', '%s' );", 'nginx-helper'), + __("Set by wp-config.php constant: define( '%s', '%s' );", 'nginx-helper'), + $cache_method_set_by_constant, $cache_method ) ); @@ -244,7 +245,8 @@ echo '

'; esc_html_e( sprintf( - __("Set by wp-config.php constant: define( 'RT_WP_NGINX_HELPER_PURGE_METHOD', '%s' );", 'nginx-helper'), + __("Set by wp-config.php constant: define( '%s', '%s' );", 'nginx-helper'), + $purge_method_set_by_constant, $purge_method ) ); @@ -380,14 +382,23 @@ if ( $redis_hostname_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_HOSTNAME', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_hostname_set_by_constant + ) + ); echo '

'; - } if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( 'Ignored! - UNIX socket is set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } else { @@ -412,14 +423,24 @@ if ( $redis_port_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_PORT', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_port_set_by_constant + ) + ); echo '

'; } if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( 'Ignored! - UNIX socket is set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } else { @@ -444,7 +465,12 @@ if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_UNIX_SOCKET', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } @@ -459,7 +485,12 @@ if ( $redis_prefix_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_PREFIX', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_prefix_set_by_constant + ) + ); echo '

'; } @@ -474,7 +505,12 @@ if ( $redis_database_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_DATABASE', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_database_set_by_constant + ) + ); echo '

'; } @@ -492,7 +528,12 @@ if ( $redis_username_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_USERNAME', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_username_set_by_constant + ) + ); echo '

'; } @@ -510,7 +551,12 @@ if ( $redis_password_set_by_constant ) { echo '

'; - esc_html_e( 'Set by wp-config.php constant: RT_WP_NGINX_HELPER_REDIS_PASSWORD', 'nginx-helper' ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_password_set_by_constant + ) + ); echo '

'; } diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index 930f6d09..ecb797fe 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -173,13 +173,16 @@ private function define_admin_hooks() { // Defines global variables. if ( ! empty( $nginx_helper_admin->options['cache_method'] ) && 'enable_redis' === $nginx_helper_admin->options['cache_method'] ) { - if ( class_exists( 'Redis' ) ) { // Use PHP5-Redis extension if installed. - $php_redis_connector = 'phpredis'; + if ( class_exists( 'Relay' ) ) { + $php_redis_client = 'relay'; + } elseif ( class_exists( 'Redis' ) ) { + $php_redis_client = 'phpredis'; } else { - $php_redis_connector = 'predis'; + $php_redis_client = 'predis'; } - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purger.php'; - $nginx_purger = new Redis_Purger( $php_redis_connector ); + + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purger.php'; + $nginx_purger = new Redis_Purger( $php_redis_client ); } else { From 845f6aef7774ea7035cbddf4eb9070e94a0f3095 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 00:39:59 +0800 Subject: [PATCH 04/12] formatting --- admin/class-nginx-helper-admin.php | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 35975d40..4be723a1 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -320,12 +320,12 @@ public function nginx_helper_settings() { if ( defined( 'GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS' ) ) { $data['homepage_purge_post_type_exceptions'] = GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS; - } + } if ( defined( 'GP_NGINX_HELPER_PURGE_METHOD' ) ) { $data['purge_method'] = GP_NGINX_HELPER_PURGE_METHOD; $data['purge_method_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; - } elseif ( defined( 'RT_WP_NGINX_HELPER_PURGE_METHOD' ) ) { + } elseif ( defined( 'RT_WP_NGINX_HELPER_PURGE_METHOD' ) ) { $data['purge_method'] = RT_WP_NGINX_HELPER_PURGE_METHOD; $data['purge_method_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; } @@ -337,7 +337,7 @@ public function nginx_helper_settings() { $data['enable_purge'] = 1; return $data; } - } elseif ( defined( 'RT_WP_NGINX_HELPER_CACHE_METHOD' ) ) { + } elseif ( defined( 'RT_WP_NGINX_HELPER_CACHE_METHOD' ) ) { $data['cache_method'] = RT_WP_NGINX_HELPER_CACHE_METHOD; $data['cache_method_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; if ( 'enable_fastcgi' === RT_WP_NGINX_HELPER_CACHE_METHOD ) { @@ -356,24 +356,24 @@ public function nginx_helper_settings() { if ( defined( 'GP_NGINX_HELPER_REDIS_PREFIX' ) ) { $redis_prefix = GP_NGINX_HELPER_REDIS_PREFIX; $data['redis_prefix_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; - } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) ) { + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PREFIX' ) ) { $redis_prefix = RT_WP_NGINX_HELPER_REDIS_PREFIX; $data['redis_prefix_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; } if ( $redis_prefix ) { - $data['redis_prefix'] = $redis_prefix; - } + $data['redis_prefix'] = $redis_prefix; + } if ( defined( 'GP_NGINX_HELPER_REDIS_PORT' ) ) { $redis_port = GP_NGINX_HELPER_REDIS_PORT; $data['redis_port_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; - } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) ) { + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PORT' ) ) { $redis_port = RT_WP_NGINX_HELPER_REDIS_PORT; $data['redis_port_set_by_constant'] = 'RT_WP_NGINX_HELPER_PURGE_METHOD'; } if ( $redis_port ) { - $data['redis_port'] = $redis_port; - } + $data['redis_port'] = $redis_port; + } if ( defined( 'GP_NGINX_HELPER_REDIS_HOSTNAME' ) ) { $redis_hostname = GP_NGINX_HELPER_REDIS_HOSTNAME; @@ -383,8 +383,8 @@ public function nginx_helper_settings() { $data['redis_hostname_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME'; } if ( $redis_hostname ) { - $data['redis_hostname'] = $redis_hostname; - } + $data['redis_hostname'] = $redis_hostname; + } if ( defined( 'GP_NGINX_HELPER_REDIS_UNIX_SOCKET' ) ) { $redis_unix_socket = GP_NGINX_HELPER_REDIS_UNIX_SOCKET; @@ -394,8 +394,8 @@ public function nginx_helper_settings() { $data['redis_unix_socket_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_HOSTNAME'; } if ( $redis_unix_socket ) { - $data['redis_unix_socket'] = $redis_unix_socket; - } + $data['redis_unix_socket'] = $redis_unix_socket; + } if ( defined( 'GP_NGINX_HELPER_REDIS_DATABASE' ) ) { $data['redis_database'] = GP_NGINX_HELPER_REDIS_DATABASE; @@ -408,7 +408,7 @@ public function nginx_helper_settings() { if ( defined( 'GP_NGINX_HELPER_REDIS_USERNAME' ) ) { $data['redis_username'] = GP_NGINX_HELPER_REDIS_USERNAME; $data['redis_username_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_USERNAME'; - } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_USERNAME' ) ) { + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_USERNAME' ) ) { $data['redis_username'] = RT_WP_NGINX_HELPER_REDIS_USERNAME; $data['redis_username_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_USERNAME'; } @@ -416,7 +416,7 @@ public function nginx_helper_settings() { if ( defined( 'GP_NGINX_HELPER_REDIS_PASSWORD' ) ) { $data['redis_password'] = GP_NGINX_HELPER_REDIS_PASSWORD; $data['redis_password_set_by_constant'] = 'GP_NGINX_HELPER_REDIS_PASSWORD'; - } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PASSWORD' ) ) { + } elseif ( defined( 'RT_WP_NGINX_HELPER_REDIS_PASSWORD' ) ) { $data['redis_password'] = RT_WP_NGINX_HELPER_REDIS_PASSWORD; $data['redis_password_set_by_constant'] = 'RT_WP_NGINX_HELPER_REDIS_PASSWORD'; } From f67fe691a112a7c5b91046016159871543117bf7 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 00:43:05 +0800 Subject: [PATCH 05/12] formatting2 --- admin/partials/nginx-helper-general-options.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index 52c332cd..a62ca8c9 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -382,12 +382,12 @@ if ( $redis_hostname_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), - $redis_hostname_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_hostname_set_by_constant + ) + ); echo '

'; } if ( $redis_unix_socket_set_by_constant ) { From 3a69e4ded5dfbdba27c1aa82164445ef41bfbe3a Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 00:47:08 +0800 Subject: [PATCH 06/12] formatting3 --- .../partials/nginx-helper-general-options.php | 26 +++++++++---------- 1 file changed, 13 insertions(+), 13 deletions(-) diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index a62ca8c9..c8e8a76c 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -393,12 +393,12 @@ if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), - $redis_unix_socket_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } else { @@ -423,12 +423,12 @@ if ( $redis_port_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), - $redis_port_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_port_set_by_constant + ) + ); echo '

'; } @@ -487,7 +487,7 @@ echo '

'; esc_html_e( sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), + __("Set by wp-config.php constant: %s", 'nginx-helper'), $redis_prefix_set_by_constant ) ); From a3f58f0b368f1623a77e80c6fcfd131270a569d1 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 00:50:25 +0800 Subject: [PATCH 07/12] formatting4 --- .../partials/nginx-helper-general-options.php | 72 +++++++++---------- 1 file changed, 36 insertions(+), 36 deletions(-) diff --git a/admin/partials/nginx-helper-general-options.php b/admin/partials/nginx-helper-general-options.php index c8e8a76c..cfa25304 100644 --- a/admin/partials/nginx-helper-general-options.php +++ b/admin/partials/nginx-helper-general-options.php @@ -435,12 +435,12 @@ if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), - $redis_unix_socket_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Ignored! - UNIX socket is set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } else { @@ -465,12 +465,12 @@ if ( $redis_unix_socket_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), - $redis_unix_socket_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_unix_socket_set_by_constant + ) + ); echo '

'; } @@ -485,12 +485,12 @@ if ( $redis_prefix_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), - $redis_prefix_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_prefix_set_by_constant + ) + ); echo '

'; } @@ -505,12 +505,12 @@ if ( $redis_database_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), - $redis_database_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_database_set_by_constant + ) + ); echo '

'; } @@ -528,12 +528,12 @@ if ( $redis_username_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), - $redis_username_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_username_set_by_constant + ) + ); echo '

'; } @@ -551,12 +551,12 @@ if ( $redis_password_set_by_constant ) { echo '

'; - esc_html_e( - sprintf( - __("Set by wp-config.php constant: %s", 'nginx-helper'), - $redis_password_set_by_constant - ) - ); + esc_html_e( + sprintf( + __("Set by wp-config.php constant: %s", 'nginx-helper'), + $redis_password_set_by_constant + ) + ); echo '

'; } From 35f1755a62481b3a2aee904de6a8a15bef248911 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 00:53:10 +0800 Subject: [PATCH 08/12] formatting5 --- includes/class-nginx-helper.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/includes/class-nginx-helper.php b/includes/class-nginx-helper.php index ecb797fe..e923edc8 100644 --- a/includes/class-nginx-helper.php +++ b/includes/class-nginx-helper.php @@ -174,14 +174,14 @@ private function define_admin_hooks() { if ( ! empty( $nginx_helper_admin->options['cache_method'] ) && 'enable_redis' === $nginx_helper_admin->options['cache_method'] ) { if ( class_exists( 'Relay' ) ) { - $php_redis_client = 'relay'; + $php_redis_client = 'relay'; } elseif ( class_exists( 'Redis' ) ) { - $php_redis_client = 'phpredis'; + $php_redis_client = 'phpredis'; } else { - $php_redis_client = 'predis'; + $php_redis_client = 'predis'; } - require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purger.php'; + require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purger.php'; $nginx_purger = new Redis_Purger( $php_redis_client ); } else { From 6c2e5857d7a4d04241336df62b7f075e4386426f Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 19:37:07 +0800 Subject: [PATCH 09/12] filters and constants --- admin/class-nginx-helper-admin.php | 24 ++++++++++++++---------- admin/class-purger.php | 22 ++++++++++++---------- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 4be723a1..6b901126 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -293,6 +293,7 @@ public function nginx_helper_default_settings() { 'redis_username_set_by_constant' => false, 'redis_password_socket_set_by_constant' => false, 'homepage_purge_post_type_exceptions' => array(), + 'post_type_purge_exceptions' => array(), ); } @@ -322,6 +323,10 @@ public function nginx_helper_settings() { $data['homepage_purge_post_type_exceptions'] = GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS; } + if ( defined( 'GP_NGINX_HELPER_POST_TYPE_PURGE_EXCEPTIONS' ) ) { + $data['post_type_purge_exceptions'] = GP_NGINX_HELPER_POST_TYPE_PURGE_EXCEPTIONS; + } + if ( defined( 'GP_NGINX_HELPER_PURGE_METHOD' ) ) { $data['purge_method'] = GP_NGINX_HELPER_PURGE_METHOD; $data['purge_method_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; @@ -719,19 +724,18 @@ public function update_map() { */ public function set_future_post_option_on_future_status( $new_status, $old_status, $post ) { - global $blog_id, $nginx_purger; - - $exclude_post_types = apply_filters( 'rt_nginx_helper_exclude_post_types', array( 'nav_menu_item' ) ); + global $blog_id, $nginx_purger, $nginx_helper_admin; - $post_type = $post->post_type; + $purge_exceptions = $nginx_helper_admin->options['post_type_purge_exceptions']; + $purge_exceptions[] = 'nav_menu_item'; + $exclude_post_types = apply_filters( 'rt_nginx_helper_exclude_post_types', $purge_exceptions ); + $post_type = $post->post_type; if ( in_array( $post_type, $exclude_post_types, true ) ) { - if ( 'nav_menu_item' !== $post_type ) { - $nginx_purger->log('* * * * *'); - $nginx_purger->log('* Post Type update - ' . $post_type . ' - purge trigger excluded...'); - $nginx_purger->log('* Filter: -> rt_nginx_helper_exclude_post_types'); - $nginx_purger->log('* * * * *'); - } + $nginx_purger->log('* * * * *'); + $nginx_purger->log('* Post Type update - ' . $post_type . ' - purge trigger excluded...'); + $nginx_purger->log('* Filter: -> rt_nginx_helper_exclude_post_types'); + $nginx_purger->log('* * * * *'); return; } diff --git a/admin/class-purger.php b/admin/class-purger.php index 683afd50..419a5d31 100644 --- a/admin/class-purger.php +++ b/admin/class-purger.php @@ -88,19 +88,18 @@ public function purge_post_on_comment_change( $newstatus, $oldstatus, $comment ) $_comment_id = $comment->comment_ID; $_post_type = get_post_type( $_post_id ); - $exclude_post_types = apply_filters( 'rt_nginx_helper_comment_change_exclude_post_types', array() ); + $purge_exceptions = $nginx_helper_admin->options['post_type_purge_exceptions']; + $exclude_post_types = apply_filters( 'rt_nginx_helper_comment_change_exclude_post_types', $purge_exceptions ); + + $this->log( '* * * * *' ); if ( in_array( $_post_type, $exclude_post_types, true ) ) { - if ( 'nav_menu_item' !== $_post_type ) { - $this->log( '* * * * *' ); - $this->log('* Post Type comment update - ' . $_post_type . ' - purge trigger excluded...'); - $this->log('* Filter: rt_nginx_helper_comment_change_exclude_post_types'); - $this->log( '* * * * *' ); - } + $this->log('* Post Type comment update - ' . $_post_type . ' - purge trigger excluded...'); + $this->log('* Filter: rt_nginx_helper_comment_change_exclude_post_types'); + $this->log( '* * * * *' ); return; } - $this->log( '* * * * *' ); $this->log( '* Blog :: ' . addslashes( get_bloginfo( 'name' ) ) . ' ( ' . $blog_id . ' ). ' ); $this->log( '* Post :: ' . get_the_title( $_post_id ) . ' ( ' . $_post_id . ' ) ' ); $this->log( "* Comment :: $_comment_id." ); @@ -190,13 +189,16 @@ public function purge_post( $post_id ) { # [ 'post_type1', 'post_type1' ] # we can also do exceptions by post/page slug too + $purge_exceptions = $nginx_helper_admin->options['homepage_purge_post_type_exceptions']; + $exclude_post_types = apply_filters( 'gp_nginx_helper_post_type_comment_change_homepage_purge_exceptions', $purge_exceptions ); + if ( 1 === (int) $nginx_helper_admin->options['purge_homepage_on_edit'] ) { - if ( ! in_array( $_post_type, $nginx_helper_admin->options['homepage_purge_post_type_exceptions'], true ) ) { + if ( ! in_array( $_post_type, $exclude_post_types, true ) ) { $this->_purge_homepage(); } else { $this->log('* * * * *'); $this->log('* Post Type update - ' . $_post_type . ' - homepage purge trigger excluded...'); - $this->log('* ' . $_post_type . ' in constant array: RT_WP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS'); + $this->log('* ' . $_post_type . ' in constant array: GP_NGINX_HELPER_HOMEPAGE_PURGE_EXCEPTIONS'); $this->log('* * * * *'); } } From 4709b6aa3acd392ab07ddfd21eeaae9a3e17f1a8 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 21:27:42 +0800 Subject: [PATCH 10/12] relay --- admin/class-nginx-helper-admin.php | 5 +++++ admin/class-redis-purger.php | 14 +++++++++----- includes/class-nginx-helper.php | 12 ++++++------ 3 files changed, 20 insertions(+), 11 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 6b901126..0b729cf7 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -273,6 +273,7 @@ public function nginx_helper_default_settings() { 'purge_page_on_new_comment' => 1, 'purge_page_on_deleted_comment' => 1, 'purge_feeds' => 1, + 'relay_client' => 0, 'redis_hostname' => '127.0.0.1', 'redis_port' => '6379', 'redis_prefix' => 'nginx-cache:', @@ -358,6 +359,10 @@ public function nginx_helper_settings() { $redis_port = false; $redis_unix_socket = false; + if ( defined( 'GP_NGINX_HELPER_USE_RELAY' && in_array('relay', get_loaded_extensions(), true) ) ) { + $data['redis_client'] = 1; + } + if ( defined( 'GP_NGINX_HELPER_REDIS_PREFIX' ) ) { $redis_prefix = GP_NGINX_HELPER_REDIS_PREFIX; $data['redis_prefix_set_by_constant'] = 'GP_NGINX_HELPER_PURGE_METHOD'; diff --git a/admin/class-redis-purger.php b/admin/class-redis-purger.php index 88c7b713..289207c2 100644 --- a/admin/class-redis-purger.php +++ b/admin/class-redis-purger.php @@ -27,18 +27,18 @@ class Redis_Purger extends Purger { */ public $redis_object; - public $php_redis_client; + public $redis_client; /** * Initialize the class and set its properties. * * @since 2.0.0 */ - public function __construct( $php_redis_client = 'phpredis' ) { + public function __construct( $redis_client = 'phpredis' ) { global $nginx_helper_admin; - $this->php_redis_client = $php_redis_client; + $this->redis_client = $redis_client; $connection_array = []; $path = $nginx_helper_admin->options['redis_unix_socket']; @@ -48,7 +48,7 @@ public function __construct( $php_redis_client = 'phpredis' ) { try { - switch ( $php_redis_client ) { + switch ( $redis_client ) { case 'predis': @@ -320,8 +320,10 @@ public function custom_purge_urls() { */ public function delete_single_key( $key ) { + $this->log( '- Redis Client: ' . $this->redis_client ); + try { - if ( 'predis' === $this->php_redis_client ) { + if ( 'predis' === $this->redis_client ) { return $this->redis_object->executeRaw( array( 'DEL', $key ) ); } else { return $this->redis_object->del($key); @@ -348,6 +350,8 @@ public function delete_single_key( $key ) { */ public function delete_keys_by_wildcard( $pattern ) { + $this->log( '- Redis Client: ' . $this->redis_client ); + // Lua Script. $lua = <<options['cache_method'] ) && 'enable_redis' === $nginx_helper_admin->options['cache_method'] ) { - if ( class_exists( 'Relay' ) ) { - $php_redis_client = 'relay'; - } elseif ( class_exists( 'Redis' ) ) { - $php_redis_client = 'phpredis'; + if ( $nginx_helper_admin->options['relay_client'] ) { + $redis_client = 'relay'; + } elseif ( class_exists( 'Redis' ) ) { + $redis_client = 'phpredis'; } else { - $php_redis_client = 'predis'; + $redis_client = 'predis'; } require_once plugin_dir_path( dirname( __FILE__ ) ) . 'admin/class-redis-purger.php'; - $nginx_purger = new Redis_Purger( $php_redis_client ); + $nginx_purger = new Redis_Purger( $redis_client ); } else { From 712f13257416580d2dee2606842569fe92ca816d Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 22:25:06 +0800 Subject: [PATCH 11/12] relay constant --- admin/class-nginx-helper-admin.php | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/admin/class-nginx-helper-admin.php b/admin/class-nginx-helper-admin.php index 0b729cf7..f8d873df 100644 --- a/admin/class-nginx-helper-admin.php +++ b/admin/class-nginx-helper-admin.php @@ -273,7 +273,7 @@ public function nginx_helper_default_settings() { 'purge_page_on_new_comment' => 1, 'purge_page_on_deleted_comment' => 1, 'purge_feeds' => 1, - 'relay_client' => 0, + 'relay_client' => false, 'redis_hostname' => '127.0.0.1', 'redis_port' => '6379', 'redis_prefix' => 'nginx-cache:', @@ -359,8 +359,10 @@ public function nginx_helper_settings() { $redis_port = false; $redis_unix_socket = false; - if ( defined( 'GP_NGINX_HELPER_USE_RELAY' && in_array('relay', get_loaded_extensions(), true) ) ) { - $data['redis_client'] = 1; + if ( defined( 'GP_NGINX_HELPER_USE_RELAY' ) ) { + if (in_array( 'relay', get_loaded_extensions())) { + $data['relay_client'] = GP_NGINX_HELPER_USE_RELAY; + } } if ( defined( 'GP_NGINX_HELPER_REDIS_PREFIX' ) ) { From 92f6cf7f83a3d91cc39040a444f8ac30b6b95195 Mon Sep 17 00:00:00 2001 From: Jeff Cleverley Date: Tue, 9 Jul 2024 23:59:07 +0800 Subject: [PATCH 12/12] disable relay cache for clearing --- admin/class-redis-purger.php | 2 ++ 1 file changed, 2 insertions(+) diff --git a/admin/class-redis-purger.php b/admin/class-redis-purger.php index 289207c2..87646496 100644 --- a/admin/class-redis-purger.php +++ b/admin/class-redis-purger.php @@ -106,6 +106,8 @@ public function __construct( $redis_client = 'phpredis' ) { $this->redis_object->select($redis_database); + $this->redis_object->setOption(\Relay\Relay::OPT_USE_CACHE, false); + break; case 'phpredis':