diff --git a/livewhale-client-modules/ems/global.application.ems.php b/livewhale-client-modules/ems/global.application.ems.php index 3c077fb..036dcb3 100644 --- a/livewhale-client-modules/ems/global.application.ems.php +++ b/livewhale-client-modules/ems/global.application.ems.php @@ -535,20 +535,20 @@ public function onChangeDatabaseHost($before_host, $after_host) { // switches ho public function onBeforeLogin() { // on before login global $_LW; $ts=strtotime('-1 month'); // set TS for 1 month ago -foreach(['reservations', 'attachments'] as $type) { - if (is_dir($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations')) { - if (!file_exists($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations/last_check') || filemtime($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations/last_check')<$ts) { // once a month - touch($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations/last_check'); // mark as cleaned - if ($res_dirs=@scandir($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations')) { // clear cached reservations and attachments not updated within the last 1 month +foreach(['reservations', 'attachments', 'udfs'] as $type) { + if (is_dir($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'')) { + if (!file_exists($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'/last_check') || filemtime($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'/last_check')<$ts) { // once a month + touch($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'/last_check'); // mark as cleaned + if ($res_dirs=@scandir($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'')) { // clear cached items not updated within the last 1 month foreach($res_dirs as $dir) { if ($dir[0]!='.') { - if (is_dir($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations/'.$dir)) { - if ($files=@scandir($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations/'.$dir)) { + if (is_dir($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'/'.$dir)) { + if ($files=@scandir($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'/'.$dir)) { foreach($files as $file) { if ($file[0]!='.') { - if ($file_ts=@filemtime($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations/'.$dir.'/'.$file)) { + if ($file_ts=@filemtime($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'/'.$dir.'/'.$file)) { if ($file_ts<$ts) { - @unlink($_LW->INCLUDES_DIR_PATH.'/data/ems/reservations/'.$dir.'/'.$file); + @unlink($_LW->INCLUDES_DIR_PATH.'/data/ems/'.$type.'/'.$dir.'/'.$file); }; }; }; diff --git a/livewhale-client-modules/ems/includes/class.ems_rest.php b/livewhale-client-modules/ems/includes/class.ems_rest.php index 8994bdd..f9e4b81 100644 --- a/livewhale-client-modules/ems/includes/class.ems_rest.php +++ b/livewhale-client-modules/ems/includes/class.ems_rest.php @@ -545,6 +545,23 @@ public function getReservationById($id) { // fetches a reservation's info public function getUDFs($username, $password, $parent_id, $parent_type) { // fetches EMS UDFs global $_LW; +static $map; +if (!isset($map)) { + $map=[]; +}; +if (isset($map[$parent_type][$parent_id])) { // return cached response if possible + return $map[$parent_type][$parent_id]; +}; +$cache_key=hash('md5', $parent_type.$parent_id); // hash the cache key +$cache_path=$_LW->INCLUDES_DIR_PATH.'/data/ems/udfs/'.$cache_key[0].$cache_key[1].'/'.$cache_key; // get the cache path +if (@filemtime($cache_path)>$_SERVER['REQUEST_TIME']-3600) { // return cached response if possible + if ($output=@file_get_contents($cache_path)) { + if ($output=@unserialize($output)) { + $map[$parent_type][$parent_id]=$output; + return $output; + }; + }; +}; $output=[]; $params=['pageSize'=>2000]; if (!in_array($parent_type,['bookings', 'reservations'])) { @@ -577,6 +594,15 @@ public function getUDFs($username, $password, $parent_id, $parent_type) { // fet }; }; }; + foreach([$_LW->INCLUDES_DIR_PATH.'/data/ems', $_LW->INCLUDES_DIR_PATH.'/data/ems/udfs', $_LW->INCLUDES_DIR_PATH.'/data/ems/udfs/'.$cache_key[0].$cache_key[1]] as $dir) { + if (!is_dir($dir)) { + @mkdir($dir); + }; + }; + if (is_dir($_LW->INCLUDES_DIR_PATH.'/data/ems/udfs/'.$cache_key[0].$cache_key[1])) { + @file_put_contents($cache_path, serialize($output), LOCK_EX); // file cache the reservation + }; + $map[$parent_type][$parent_id]=$output; // static cache the reservation }; return $output; }