From cb093e7266b622b5657d63954b36eefbc2a3776d Mon Sep 17 00:00:00 2001 From: Hadi Nategh Date: Mon, 11 Aug 2014 12:14:27 +0000 Subject: [PATCH] Avoid to call mail constructor (openConnection is an expensive operation) when client-side ask for vacationNotice. -Check if there's no vacation cached then create mail object (try openConnection) and ask for vacation -Add account_lid to cached vacations to distinguished them in case if the mail account was a shared profile --- mail/inc/class.mail_sieve.inc.php | 5 +++- mail/inc/class.mail_ui.inc.php | 47 ++++++++++++++++--------------- 2 files changed, 29 insertions(+), 23 deletions(-) diff --git a/mail/inc/class.mail_sieve.inc.php b/mail/inc/class.mail_sieve.inc.php index 54a531b03a..dac08bf07b 100644 --- a/mail/inc/class.mail_sieve.inc.php +++ b/mail/inc/class.mail_sieve.inc.php @@ -621,7 +621,10 @@ class mail_sieve self::setAsyncJob($newVacation); } //Reset vacationNotice cache which is used in mail_ui get_rows - egw_cache::unsetCache(egw_cache::INSTANCE,'email', 'vacationNotice'+$icServer->acc_id); + $cachedVacations = egw_cache::getCache(egw_cache::INSTANCE, 'email', 'vacationNotice'+$GLOBALS['egw_info']['user']['account_lid']); + $cachedVacations = array($icServer->acc_id => $newVacation) + (array)$cachedVacations; + egw_cache::setCache(egw_cache::INSTANCE,'email', 'vacationNotice'+$GLOBALS['egw_info']['user']['account_lid'], $cachedVacations); + $msg = lang('Vacation notice sucessfully updated.'); } } diff --git a/mail/inc/class.mail_ui.inc.php b/mail/inc/class.mail_ui.inc.php index ce8597ac44..1563d8f824 100644 --- a/mail/inc/class.mail_ui.inc.php +++ b/mail/inc/class.mail_ui.inc.php @@ -2284,32 +2284,25 @@ class mail_ui /** * fetch vacation info from active Server using icServer object * + * @param array $cachedVacations an array of cached vacations for an user * @return array|boolean array with vacation on success or false on failure */ - function gatherVacation() + function gatherVacation($cachedVacations = array()) { - $vacation = $this->mail_bo->icServer->acc_sieve_enabled && ($this->mail_bo->icServer->acc_sieve_host||$this->mail_bo->icServer->acc_imap_host); + $isVacationEnabled = $this->mail_bo->icServer->acc_sieve_enabled && ($this->mail_bo->icServer->acc_sieve_host||$this->mail_bo->icServer->acc_imap_host); //error_log(__METHOD__.__LINE__.' Server:'.self::$icServerID.' Sieve Enabled:'.array2string($vacation)); - if ($vacation) + if ($isVacationEnabled) { $sieveServer = $this->mail_bo->icServer; try { - //Get vacation from cache if it's available - $vacationCached = egw_cache::getCache(egw_cache::INSTANCE, 'email', 'vacationNotice'+$sieveServer->acc_id); + $sieveServer->retrieveRules(); + $vacation = $sieveServer->getVacation(); - if (!$vacationCached) - { - $sieveServer->retrieveRules(); - $vacation = $sieveServer->getVacation(); - // Set vacation to the instance cache for particular account - egw_cache::setCache(egw_cache::INSTANCE, 'email', 'vacationNotice'+$sieveServer->acc_id, $vacation); - } - else - { - return $vacationCached; - } + $cachedVacations = array($sieveServer->acc_id => $vacation) + $cachedVacations; + // Set vacation to the instance cache for particular account with expiration of one day + egw_cache::setCache(egw_cache::INSTANCE, 'email', 'vacationNotice'+$GLOBALS['egw_info']['user']['account_lid'], $cachedVacations, 60*60*24); } catch (PEAR_Exception $ex) { $this->callWizard($ex->getMessage(), true, 'error'); } @@ -3909,13 +3902,23 @@ class mail_ui * if other than active profile; nothing is done! * @return nothing */ - function ajax_refreshVacationNotice($icServerID=null) + public static function ajax_refreshVacationNotice($icServerID=null) { - //error_log(__METHOD__.__LINE__.array2string($icServerId)); - if (empty($icServerID)) $icServerID = $this->mail_bo->profileID; - if ($icServerID != $this->mail_bo->profileID) return; - $vacation = $this->gatherVacation(); - //error_log(__METHOD__.__LINE__.array2string($vacation)); + //Get vacation from cache if it's available + $cachedVacations = egw_cache::getCache(egw_cache::INSTANCE, 'email', 'vacationNotice'+$GLOBALS['egw_info']['user']['account_lid']); + $vacation = $cachedVacations[$icServerID]; + + if (!$vacation) + { + // Create mail app object + $mail = new mail_ui(); + + if (empty($icServerID)) $icServerID = $mail->mail_bo->profileID; + if ($icServerID != $mail->mail_bo->profileID) return; + + $vacation = $mail->gatherVacation($cachedVacations); + } + if($vacation) { if (is_array($vacation) && ($vacation['status'] == 'on' || $vacation['status']=='by_date')) {