Performance: Cache DAV home(s) discovery, skip redundant DAV request

This commit is contained in:
Aleksander Machniak 2022-11-25 12:03:29 +01:00
parent 3d373079d4
commit 454e21c47e
2 changed files with 47 additions and 9 deletions

View file

@ -27,6 +27,7 @@ class carddav_contacts_driver
{ {
protected $plugin; protected $plugin;
protected $rc; protected $rc;
protected $sources;
public function __construct($plugin) public function __construct($plugin)
{ {
@ -37,17 +38,21 @@ class carddav_contacts_driver
/** /**
* List addressbook sources (folders) * List addressbook sources (folders)
*/ */
public static function list_folders() public function list_folders()
{ {
if (isset($this->sources)) {
return $this->sources;
}
$storage = self::get_storage(); $storage = self::get_storage();
$sources = []; $this->sources = [];
// get all folders that have "contact" type // get all folders that have "contact" type
foreach ($storage->get_folders('contact') as $folder) { 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 * @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(); $storage = self::get_storage();
$folder = $storage->get_folder($id, 'contact'); $folder = $storage->get_folder($id, 'contact');
@ -89,6 +98,8 @@ class carddav_contacts_driver
{ {
$storage = self::get_storage(); $storage = self::get_storage();
$this->sources = null;
return $storage->folder_delete($folder, 'contact'); return $storage->folder_delete($folder, 'contact');
} }
@ -146,10 +157,11 @@ class carddav_contacts_driver
$type = !empty($prop['id']) ? 'update' : 'create'; $type = !empty($prop['id']) ? 'update' : 'create';
if ( $this->sources = null;
($result = $storage->folder_update($prop))
&& ($abook = $this->get_address_book($prop['id'] ?: $result)) $result = $storage->folder_update($prop);
) {
if ($result && ($abook = $this->get_address_book($prop['id'] ?: $result))) {
$abook->id = $prop['id'] ?: $result; $abook->id = $prop['id'] ?: $result;
$props = $this->abook_prop($abook->id, $abook); $props = $this->abook_prop($abook->id, $abook);

View file

@ -123,6 +123,14 @@ class kolab_dav_client
*/ */
public function discover($component = 'VEVENT') 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 = [ $roots = [
'VEVENT' => 'calendars', 'VEVENT' => 'calendars',
'VTODO' => 'calendars', 'VTODO' => 'calendars',
@ -202,6 +210,10 @@ class kolab_dav_client
$root_href = '/' . $roots[$component] . '/' . rawurlencode($this->user); $root_href = '/' . $roots[$component] . '/' . rawurlencode($this->user);
} }
if ($cache) {
$cache->set($cache_key, $root_href);
}
return $root_href; return $root_href;
} }
@ -773,4 +785,18 @@ class kolab_dav_client
rcube::raise_error($e, true, true); 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);
}
}
} }