From 87d8bb93ec81a8787d9f443df24a8f96ee6244c9 Mon Sep 17 00:00:00 2001 From: sagarnasit Date: Tue, 15 Jan 2019 20:05:20 +0530 Subject: [PATCH] Add cache flag option for page and object cache --- src/WordPress.php | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/src/WordPress.php b/src/WordPress.php index c8680ec..954a218 100644 --- a/src/WordPress.php +++ b/src/WordPress.php @@ -95,6 +95,12 @@ public function __construct() { * * [--cache] * : Use redis cache for WordPress. + * --- + * options: + * - all + * - object + * - page + * --- * * [--vip] * : Create WordPress VIP GO site using your vip repo which contains wp-content dir. Default it will use skeleton repo. @@ -251,7 +257,7 @@ public function create( $args, $assoc_args ) { } $this->site_data['site_fs_path'] = WEBROOT . $this->site_data['site_url']; - $this->cache_type = \EE\Utils\get_flag_value( $assoc_args, 'cache' ); + $this->cache_type = get_value_if_flag_isset( $assoc_args, 'cache', [ 'all', 'object', 'page' ], 'all' ); $this->site_data['site_ssl_wildcard'] = \EE\Utils\get_flag_value( $assoc_args, 'wildcard' ); $this->site_data['php_version'] = \EE\Utils\get_flag_value( $assoc_args, 'php', 'latest' ); $this->site_data['app_admin_url'] = \EE\Utils\get_flag_value( $assoc_args, 'title', $this->site_data['site_url'] ); @@ -344,10 +350,13 @@ private function enable_object_cache() { $redis_plugin_constant = 'docker-compose exec --user=\'www-data\' php wp config set --type=variable redis_server "array(\'host\'=> \'' . $redis_host . '\',\'port\'=> 6379,)" --raw'; $activate_wp_redis_plugin = "docker-compose exec --user='www-data' php wp plugin install wp-redis --activate"; $enable_redis_cache = "docker-compose exec --user='www-data' php wp redis enable"; + $obj_cache_key_prefix = $this->site_data['site_url'] . '_obj:'; + $add_cache_key_salt = "docker-compose exec --user='www-data' php wp config set WP_CACHE_KEY_SALT $obj_cache_key_prefix --add=true --type=constant"; $this->docker_compose_exec( $redis_plugin_constant, 'Unable to download or activate wp-redis plugin.' ); $this->docker_compose_exec( $activate_wp_redis_plugin, 'Unable to download or activate wp-redis plugin.' ); $this->docker_compose_exec( $enable_redis_cache, 'Unable to enable object cache' ); + $this->docker_compose_exec( $add_cache_key_salt, 'Unable to set cache key salt' ); } /** @@ -357,7 +366,6 @@ private function enable_page_cache() { $activate_nginx_helper = 'docker-compose exec --user=\'www-data\' php wp plugin install nginx-helper --activate'; $nginx_helper_fail_msg = 'Unable to download or activate nginx-helper plugin properly.'; $page_cache_key_prefix = $this->site_data['site_url'] . '_page:'; - $obj_cache_key_prefix = $this->site_data['site_url'] . '_obj:'; $redis_host = $this->site_data['cache_host']; $wp_cli_params = ( 'wp' === $this->site_data['app_sub_type'] ) ? 'option update' : 'network meta update 1'; @@ -393,14 +401,12 @@ private function enable_page_cache() { $add_hostname_constant = "docker-compose exec --user='www-data' php wp config set RT_WP_NGINX_HELPER_REDIS_HOSTNAME $redis_host --add=true --type=constant"; $add_port_constant = "docker-compose exec --user='www-data' php wp config set RT_WP_NGINX_HELPER_REDIS_PORT 6379 --add=true --type=constant"; $add_prefix_constant = "docker-compose exec --user='www-data' php wp config set RT_WP_NGINX_HELPER_REDIS_PREFIX $page_cache_key_prefix --add=true --type=constant"; - $add_cache_key_salt = "docker-compose exec --user='www-data' php wp config set WP_CACHE_KEY_SALT $obj_cache_key_prefix --add=true --type=constant"; $add_redis_maxttl = "docker-compose exec --user='www-data' php wp config set WP_REDIS_MAXTTL 14400 --add=true --type=constant"; $add_plugin_data = "docker-compose exec --user='www-data' php wp $wp_cli_params rt_wp_nginx_helper_options '$plugin_data' --format=json"; $this->docker_compose_exec( $add_hostname_constant, $nginx_helper_fail_msg ); $this->docker_compose_exec( $add_port_constant, $nginx_helper_fail_msg ); $this->docker_compose_exec( $add_prefix_constant, $nginx_helper_fail_msg ); - $this->docker_compose_exec( $add_cache_key_salt, $nginx_helper_fail_msg ); $this->docker_compose_exec( $activate_nginx_helper, $nginx_helper_fail_msg ); $this->docker_compose_exec( $add_plugin_data, $nginx_helper_fail_msg ); $this->docker_compose_exec( $add_redis_maxttl, $nginx_helper_fail_msg ); @@ -898,8 +904,14 @@ private function create_site( $assoc_args ) { if ( ! empty( $this->cache_type ) ) { - $this->enable_object_cache(); - $this->enable_page_cache(); + if ( 'all' === $this->cache_type ) { + $this->enable_object_cache(); + $this->enable_page_cache(); + } elseif ( 'object' === $this->cache_type ) { + $this->enable_object_cache(); + } elseif ( 'page' === $this->cache_type ) { + $this->enable_page_cache(); + } if ( $this->is_vip ) { EE::warning( 'Nginx-helper and wp-redis plugin is installed to enable cache. Please add it in your .gitignore to avoid it from git diff and commit' );