From 454e21c47eff8d3c04695fc69999718259a2a88e Mon Sep 17 00:00:00 2001 From: Aleksander Machniak Date: Fri, 25 Nov 2022 12:03:29 +0100 Subject: [PATCH] Performance: Cache DAV home(s) discovery, skip redundant DAV request --- .../carddav/carddav_contacts_driver.php | 30 +++++++++++++------ plugins/libkolab/lib/kolab_dav_client.php | 26 ++++++++++++++++ 2 files changed, 47 insertions(+), 9 deletions(-) diff --git a/plugins/kolab_addressbook/drivers/carddav/carddav_contacts_driver.php b/plugins/kolab_addressbook/drivers/carddav/carddav_contacts_driver.php index 1a656be7..006a54e2 100644 --- a/plugins/kolab_addressbook/drivers/carddav/carddav_contacts_driver.php +++ b/plugins/kolab_addressbook/drivers/carddav/carddav_contacts_driver.php @@ -27,6 +27,7 @@ class carddav_contacts_driver { protected $plugin; protected $rc; + protected $sources; public function __construct($plugin) { @@ -37,17 +38,21 @@ class carddav_contacts_driver /** * List addressbook sources (folders) */ - public static function list_folders() + public function list_folders() { + if (isset($this->sources)) { + return $this->sources; + } + $storage = self::get_storage(); - $sources = []; + $this->sources = []; // get all folders that have "contact" type foreach ($storage->get_folders('contact') as $folder) { - $sources[$folder->id] = new carddav_contacts($folder); + $this->sources[$folder->id] = new carddav_contacts($folder); } - return $sources; + return $this->sources; } /** @@ -57,8 +62,12 @@ class carddav_contacts_driver * * @return ?carddav_contacts */ - public static function get_address_book($id) + public function get_address_book($id) { + if (isset($this->sources[$id])) { + return $this->sources[$id]; + } + $storage = self::get_storage(); $folder = $storage->get_folder($id, 'contact'); @@ -89,6 +98,8 @@ class carddav_contacts_driver { $storage = self::get_storage(); + $this->sources = null; + return $storage->folder_delete($folder, 'contact'); } @@ -146,10 +157,11 @@ class carddav_contacts_driver $type = !empty($prop['id']) ? 'update' : 'create'; - if ( - ($result = $storage->folder_update($prop)) - && ($abook = $this->get_address_book($prop['id'] ?: $result)) - ) { + $this->sources = null; + + $result = $storage->folder_update($prop); + + if ($result && ($abook = $this->get_address_book($prop['id'] ?: $result))) { $abook->id = $prop['id'] ?: $result; $props = $this->abook_prop($abook->id, $abook); diff --git a/plugins/libkolab/lib/kolab_dav_client.php b/plugins/libkolab/lib/kolab_dav_client.php index 79c23be8..1659372b 100644 --- a/plugins/libkolab/lib/kolab_dav_client.php +++ b/plugins/libkolab/lib/kolab_dav_client.php @@ -123,6 +123,14 @@ class kolab_dav_client */ public function discover($component = 'VEVENT') { + if ($cache = $this->get_cache()) { + $cache_key = "discover.{$component}." . md5($this->url); + + if ($response = $cache->get($cache_key)) { + return $response; + } + } + $roots = [ 'VEVENT' => 'calendars', 'VTODO' => 'calendars', @@ -202,6 +210,10 @@ class kolab_dav_client $root_href = '/' . $roots[$component] . '/' . rawurlencode($this->user); } + if ($cache) { + $cache->set($cache_key, $root_href); + } + return $root_href; } @@ -773,4 +785,18 @@ class kolab_dav_client rcube::raise_error($e, true, true); } } + + /** + * Return caching object if enabled + */ + protected function get_cache() + { + $rcube = rcube::get_instance(); + if ($cache_type = $rcube->config->get('dav_cache', 'db')) { + $cache_ttl = $rcube->config->get('dav_cache_ttl', '10m'); + $cache_name = 'DAV'; + + return $rcube->get_cache($cache_name, $cache_type, $cache_ttl); + } + } }