From 06196eb278daae52ee7de682d234e62b31ad5640 Mon Sep 17 00:00:00 2001 From: Mehmood Ahmad <31419912+mehmoodak@users.noreply.github.com> Date: Thu, 14 Sep 2023 01:08:00 +0500 Subject: [PATCH] =?UTF-8?q?=F0=9F=91=8C=20IMPROVE:=20use=20clearstatcache?= =?UTF-8?q?=20to=20clear=20the=20cache=20of=20a=20specific=20file=20instea?= =?UTF-8?q?d=20whole=20cache?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- integrations/integration-vip-config.php | 26 ++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/integrations/integration-vip-config.php b/integrations/integration-vip-config.php index 94b426e295..23edb9c08a 100644 --- a/integrations/integration-vip-config.php +++ b/integrations/integration-vip-config.php @@ -78,25 +78,29 @@ private function set_config( string $slug ): void { * @return null|mixed */ protected function get_vip_config_from_file( string $slug ) { - $config_file_path = ABSPATH . 'config/integrations-config/' . $slug . '-config.php'; + $config_file_directory = ABSPATH . 'config/integrations-config'; + $config_file_name = $slug . '-config.php'; + $config_file_path = $config_file_directory . '/' . $config_file_name; /** - * Clear realpath cache to get new path else we can get cached path which can be non-existant - * because k8s configmaps updates the file via symlink instead of actually replacing the file and - * PHP cache can hold a reference to the old symlink. + * Clear cache to always read data from latest config file. * - * Did tried using `clearstatcache( true, $config_file_path )` but it was giving old symlink, - * `is_readable()` does return correct value (false) for this case but we prefer - * to get the config which `clearstatcache( true )` does return because it gives us new symlink. + * Kubernetes ConfigMap updates the file via symlink instead of actually replacing the file and + * PHP cache can hold a reference to the old symlink that can cause fatal if we use require + * on it. */ - clearstatcache( true ); - $config_real_path = realpath( $config_file_path ); + if ( false === file_get_contents( $config_file_path ) ) { + clearstatcache( true, $config_file_directory . '/' . $config_file_name ); + // Clears cache for files created by k8s ConfigMap. + clearstatcache( true, $config_file_directory . '/..data' ); + clearstatcache( true, $config_file_directory . '/..data/' . $config_file_name ); + } - if ( ! is_readable( $config_real_path ) ) { + if ( ! is_readable( $config_file_path ) ) { return null; } - return require $config_real_path; + return require $config_file_path; } /**