From 12fd543ba98c4f93fd3d9e5a015a27131686fc9c Mon Sep 17 00:00:00 2001 From: Charlie Root Date: Fri, 22 Jul 2022 21:32:26 +0000 Subject: [PATCH] use file-based cache for profile/interface list --- php/config.php | 98 +++++++++++++++++++++++++++++++++++++++--------- public/index.php | 2 - 2 files changed, 81 insertions(+), 19 deletions(-) diff --git a/php/config.php b/php/config.php index df732dc0..31190cbe 100644 --- a/php/config.php +++ b/php/config.php @@ -4,6 +4,8 @@ require_once("cbsd.php"); class Config { + private $_workdir=''; + /* Список языков, используемых в проекте */ public static $languages=array( 'en'=>'English', @@ -185,22 +187,84 @@ class Config public $os_interfaces=array(); function __construct(){ - $res=CBSD::run('get_bhyve_profiles src=vm clonos=1', array()); - if($res['retval']==0){ - $this->os_types=$this->create_bhyve_profiles($res); - } - - $res1=CBSD::run('get_bhyve_profiles src=cloud', array()); - if($res1['retval']==0){ - $this->os_types_obtain=$this->create_bhyve_profiles($res1); - } - - $res2=CBSD::run('get_interfaces', array()); - if($res2['retval']==0){ - $this->os_interfaces=$this->create_interfaces($res2); - } - } - + + $this->_workdir=getenv('WORKDIR'); + $vm_profile_list_file=$this->_workdir.'/tmp/bhyve-vm.json'; + $cloud_profile_list_file=$this->_workdir.'/tmp/bhyve-cloud.json'; + $interface_list_file=$this->_workdir.'/tmp/interfaces.json'; + + /// check for file/cache first + // vm profile (ISO) + $vm_profile_list_file_size=0; + if(file_exists($vm_profile_list_file)) { + $vm_profile_list_file_size=filesize($vm_profile_list_file); + if( $vm_profile_list_file_size < 16 ) { + // probably empty, renew needed + $vm_profile_list_file_size=0; + } + } + + // cloud profile + $cloud_profile_list_file_size=0; + // check for file/cache first + if(file_exists($cloud_profile_list_file)) { + $cloud_profile_list_file_size=filesize($cloud_profile_list_file); + if( $cloud_profile_list_file_size < 16 ) { + // probably empty, renew needed + $cloud_profile_list_file_size=0; + } + } + + // interface profile + $interface_list_file_size=0; + // check for file/cache first + if(file_exists($interface_list_file)) { + $interface_list_file_size=filesize($interface_list_file); + if( $interface_list_file_size < 4 ) { + // probably empty, renew needed + $interface_list_file_size=0; + } + } + + /// load vm profile list + // vm profile (ISO) + if ($vm_profile_list_file_size > 0 ) { + Utils::clonos_syslog("config.php: (cached) found vm_profile cache file/size: $vm_profile_list_file exist/$vm_profile_list_file_size"); + $res['message']=file_get_contents($vm_profile_list_file); + $this->os_types=$this->create_bhyve_profiles($res); + } else { + Utils::clonos_syslog("config.php: vm_profile cache file not found: $vm_profile_list_file"); + $res=CBSD::run('get_bhyve_profiles src=vm clonos=1', array()); + if($res['retval']==0){ + $this->os_types=$this->create_bhyve_profiles($res); + } + } + + // cloud profile + if ($cloud_profile_list_file_size > 0 ) { + Utils::clonos_syslog("config.php: (cached) found cloud_profile cache file/size: $cloud_profile_list_file exist/$cloud_profile_list_file_size"); + $res1['message']=file_get_contents($cloud_profile_list_file); + $this->os_types_obtain=$this->create_bhyve_profiles($res1); + } else { + $res1=CBSD::run('get_bhyve_profiles src=cloud', array()); + if($res1['retval']==0){ + $this->os_types_obtain=$this->create_bhyve_profiles($res1); + } + } + + // interfaces + if ($interface_list_file_size > 0 ) { + Utils::clonos_syslog("config.php: (cached) found interfaces cache file/size: $interface_list_file exist/$interface_list_file_size"); + $res2['message']=file_get_contents($interface_list_file); + $this->os_interfaces=$this->create_interfaces($res2); + } else { + $res2=CBSD::run('get_interfaces', array()); + if($res2['retval']==0){ + $this->os_interfaces=$this->create_interfaces($res2); + } + } + } + function create_bhyve_profiles($info){ $os_names = array(); $res=json_decode($info['message'],true); @@ -220,7 +284,7 @@ class Config function create_interfaces($info){ $res=json_decode($info['message'],true); if(!is_null($res) && $res != false){ - return $res; + return $res['interfaces']; } else { return array(); } diff --git a/public/index.php b/public/index.php index 4254cc23..adc41b54 100644 --- a/public/index.php +++ b/public/index.php @@ -35,8 +35,6 @@ $file_path=$_ds.$root.$_ds.'pages'.$_ds.$uri.$_ds; $file_name=$file_path.$lang.'.index.php'; $json_name=$file_path.'a.json.php'; -Utils::clonos_syslog("my json_name:". $json_name); - if(empty($uri)){ header('Location: /'.$menu->first_key.'/',true); exit;