From 77f3ef93eb1da502c66a79b207e46fbd29b76c5e Mon Sep 17 00:00:00 2001 From: Michael M Slusarz Date: Tue, 10 Feb 2009 00:14:28 -0700 Subject: [PATCH] Cache STATUS responses. --- framework/Imap_Client/lib/Horde/Imap/Client.php | 2 + .../Imap_Client/lib/Horde/Imap/Client/Base.php | 64 +++++++++++++++++++++- .../Imap_Client/lib/Horde/Imap/Client/Socket.php | 8 --- 3 files changed, 63 insertions(+), 11 deletions(-) diff --git a/framework/Imap_Client/lib/Horde/Imap/Client.php b/framework/Imap_Client/lib/Horde/Imap/Client.php index 4c7751709..1d060b270 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client.php @@ -54,6 +54,8 @@ * secure - (string) Use SSL or TLS to connect. * VALUES: false, 'ssl', 'tls'. * DEFAULT: No encryption + * statuscache - (boolean) Cache STATUS responses? + * DEFAULT: True * timeout - (integer) Connection timeout, in seconds. * DEFAULT: 10 seconds * diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Base.php b/framework/Imap_Client/lib/Horde/Imap/Client/Base.php index aab964f5d..edff7f0be 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Base.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Base.php @@ -83,6 +83,13 @@ abstract class Horde_Imap_Client_Base protected $_debug = null; /** + * Temp array (destroyed at end of process). + * + * @var array + */ + protected $_temp = array(); + + /** * Constructs a new Horde_Imap_Client object. * * @param array $params A hash containing configuration parameters. @@ -108,6 +115,10 @@ abstract class Horde_Imap_Client_Base $params['timeout'] = 10; } + if (!isset($params['statuscache'])) { + $params['statuscache'] = true; + } + if (empty($params['cache'])) { $params['cache'] = array('fields' => array()); } elseif (empty($params['cache']['fields'])) { @@ -145,8 +156,9 @@ abstract class Horde_Imap_Client_Base { $this->_closeDebug(); - // Don't store Horde_Imap_Client_Cache object. + // Don't store Horde_Imap_Client_Cache object or temp data. $this->_cache = null; + $this->_temp = array(); // Encrypt password in serialized object. if (!isset($this->_params['_passencrypt'])) { @@ -620,6 +632,7 @@ abstract class Horde_Imap_Client_Base $this->_openMailbox($mailbox, $mode); $this->_selected = $mailbox; $this->_mode = $mode; + unset($this->_temp['statuscache'][$mailbox]); } } @@ -919,8 +932,40 @@ abstract class Horde_Imap_Client_Base */ public function status($mailbox, $flags = Horde_Imap_Client::STATUS_ALL) { + $unselected_flags = array( + 'messages' => Horde_Imap_Client::STATUS_MESSAGES, + 'recent' => Horde_Imap_Client::STATUS_RECENT, + 'unseen' => Horde_Imap_Client::STATUS_UNSEEN, + 'uidnext' => Horde_Imap_Client::STATUS_UIDNEXT, + 'uidvalidity' => Horde_Imap_Client::STATUS_UIDVALIDITY + ); + if ($flags & Horde_Imap_Client::STATUS_ALL) { - $flags |= Horde_Imap_Client::STATUS_MESSAGES | Horde_Imap_Client::STATUS_RECENT | Horde_Imap_Client::STATUS_UNSEEN | Horde_Imap_Client::STATUS_UIDNEXT | Horde_Imap_Client::STATUS_UIDVALIDITY; + foreach ($unselected_flags as $val) { + $flags |= $val; + } + } + + $mailbox = Horde_Imap_Client_Utf7imap::Utf8ToUtf7Imap($mailbox); + $curr_mbox = ($this->currentMailbox() == $mailbox); + $ret = array(); + + /* Check for cached information. */ + if (!$curr_mbox && + $this->_params['statuscache'] && + isset($this->_temp['statuscache'][$mailbox])) { + $ptr = &$this->_temp['statuscache'][$mailbox]; + + foreach ($unselected_flags as $key => $val) { + if (($flags & $val) && isset($ptr[$key])) { + $ret[$key] = $ptr[$key]; + $flags &= ~$val; + } + } + } + + if (!$flags) { + return $ret; } /* STATUS_PERMFLAGS requires a read/write mailbox. */ @@ -928,7 +973,20 @@ abstract class Horde_Imap_Client_Base $this->openMailbox($mailbox, Horde_Imap_Client::OPEN_READWRITE); } - return $this->_status(Horde_Imap_Client_Utf7imap::Utf8ToUtf7Imap($mailbox), $flags); + $ret = array_merge($ret, $this->_status($mailbox, $flags)); + + if ($this->currentMailbox() != $mailbox) { + if (!isset($this->_temp['statuscache'])) { + $this->_temp['statuscache'] = array(); + } + $ptr = &$this->_temp['statuscache']; + + $ptr[$mailbox] = isset($ptr[$mailbox]) + ? array_merge($ptr[$mailbox], $ret) + : $ret; + } + + return $ret; } /** diff --git a/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php b/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php index 540b7fff2..368f152a0 100644 --- a/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php +++ b/framework/Imap_Client/lib/Horde/Imap/Client/Socket.php @@ -84,13 +84,6 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base protected $_stream = null; /** - * Temp array (destroyed at end of process). - * - * @var array - */ - protected $_temp = array(); - - /** * Destructor. */ public function __destruct() @@ -106,7 +99,6 @@ class Horde_Imap_Client_Socket extends Horde_Imap_Client_Base public function __sleep() { $this->logout(); - $this->_temp = array(); $this->_tag = 0; parent::__sleep(); return array_diff(array_keys(get_class_vars(__CLASS__)), array('encryptKey')); -- 2.11.0