From bae9f0852f89908b9635d320eb9bb86ba1bd19a5 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 6 Nov 2016 00:51:23 -0400 Subject: [PATCH 1/3] misc performance: cache contact details by url during script --- include/Contact.php | 50 ++++++++++++++++++++++++++++++++------------- 1 file changed, 36 insertions(+), 14 deletions(-) diff --git a/include/Contact.php b/include/Contact.php index 9cbfdaa17..23b5bbe5b 100644 --- a/include/Contact.php +++ b/include/Contact.php @@ -196,6 +196,7 @@ function unmark_for_death($contact) { * @brief Get contact data for a given profile link * * The function looks at several places (contact table and gcontact table) for the contact + * It caches its result for the same script execution to prevent duplicate calls * * @param string $url The profile link * @param int $uid User id @@ -204,8 +205,15 @@ function unmark_for_death($contact) { * @return array Contact data */ function get_contact_details_by_url($url, $uid = -1, $default = array()) { - if ($uid == -1) + static $cache = array(); + + if ($uid == -1) { $uid = local_user(); + } + + if (isset($cache[$url][$uid])) { + return $cache[$url][$uid]; + } // Fetch contact data from the contact table for the given user $r = q("SELECT `id`, `id` AS `cid`, 0 AS `gid`, 0 AS `zid`, `uid`, `url`, `nurl`, `alias`, `network`, `name`, `nick`, `addr`, `location`, `about`, `xmpp`, @@ -229,10 +237,13 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) { if ($r) { // If there is more than one entry we filter out the connector networks - if (count($r) > 1) - foreach ($r AS $id => $result) - if ($result["network"] == NETWORK_STATUSNET) + if (count($r) > 1) { + foreach ($r AS $id => $result) { + if ($result["network"] == NETWORK_STATUSNET) { unset($r[$id]); + } + } + } $profile = array_shift($r); @@ -251,31 +262,40 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) { $profile["bd"] = $current_year."-".$month."-".$day; $current = $current_year."-".$current_month."-".$current_day; - if ($profile["bd"] < $current) + if ($profile["bd"] < $current) { $profile["bd"] = (++$current_year)."-".$month."-".$day; - } else + } + } else { $profile["bd"] = "0000-00-00"; - } else + } + } else { $profile = $default; + } - if (($profile["photo"] == "") AND isset($default["photo"])) + if (($profile["photo"] == "") AND isset($default["photo"])) { $profile["photo"] = $default["photo"]; + } - if (($profile["name"] == "") AND isset($default["name"])) + if (($profile["name"] == "") AND isset($default["name"])) { $profile["name"] = $default["name"]; + } - if (($profile["network"] == "") AND isset($default["network"])) + if (($profile["network"] == "") AND isset($default["network"])) { $profile["network"] = $default["network"]; + } - if (($profile["thumb"] == "") AND isset($profile["photo"])) + if (($profile["thumb"] == "") AND isset($profile["photo"])) { $profile["thumb"] = $profile["photo"]; + } - if (($profile["micro"] == "") AND isset($profile["thumb"])) + if (($profile["micro"] == "") AND isset($profile["thumb"])) { $profile["micro"] = $profile["thumb"]; + } if ((($profile["addr"] == "") OR ($profile["name"] == "")) AND ($profile["gid"] != 0) AND - in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) + in_array($profile["network"], array(NETWORK_DFRN, NETWORK_DIASPORA, NETWORK_OSTATUS))) { proc_run(PRIORITY_LOW, "include/update_gcontact.php", $profile["gid"]); + } // Show contact details of Diaspora contacts only if connected if (($profile["cid"] == 0) AND ($profile["network"] == NETWORK_DIASPORA)) { @@ -285,7 +305,9 @@ function get_contact_details_by_url($url, $uid = -1, $default = array()) { $profile["birthday"] = "0000-00-00"; } - return($profile); + $cache[$url][$uid] = $profile; + + return $profile; } if (! function_exists('contact_photo_menu')) { From 7b9470b804c7b8a449b491482bcd8c537f7eb96e Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 6 Nov 2016 00:52:31 -0400 Subject: [PATCH 2/3] misc performance: remove extraneous session write DB query --- include/session.php | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/include/session.php b/include/session.php index 763b05f48..8cce1488b 100644 --- a/include/session.php +++ b/include/session.php @@ -71,14 +71,9 @@ function ref_session_write($id, $data) { if ($session_exists) { $r = q("UPDATE `session` - SET `data` = '%s' - WHERE `data` != '%s' AND `sid` = '%s'", - dbesc($data), dbesc($data), dbesc($id)); - - $r = q("UPDATE `session` - SET `expire` = '%s' - WHERE `expire` != '%s' AND `sid` = '%s'", - dbesc($expire), dbesc($expire), dbesc($id)); + SET `data` = '%s', `expire` = '%s' + WHERE `sid` = '%s'", + dbesc($data), dbesc($expire), dbesc($id)); } else { $r = q("INSERT INTO `session` SET `sid` = '%s', `expire` = '%s', `data` = '%s'", From 67cdbe45389ed7fc5124c48b6508b00298fe5516 Mon Sep 17 00:00:00 2001 From: Hypolite Petovan Date: Sun, 6 Nov 2016 01:00:17 -0400 Subject: [PATCH 3/3] misc performance: restore session write condition, keep 1 query --- include/session.php | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/include/session.php b/include/session.php index 8cce1488b..8a1a3c55d 100644 --- a/include/session.php +++ b/include/session.php @@ -72,8 +72,9 @@ function ref_session_write($id, $data) { if ($session_exists) { $r = q("UPDATE `session` SET `data` = '%s', `expire` = '%s' - WHERE `sid` = '%s'", - dbesc($data), dbesc($expire), dbesc($id)); + WHERE `sid` = '%s' + AND (`data` != '%s' OR `expire` != '%s'", + dbesc($data), dbesc($expire), dbesc($id), dbesc($data), dbesc($expire)); } else { $r = q("INSERT INTO `session` SET `sid` = '%s', `expire` = '%s', `data` = '%s'",