enhance caching

This commit is contained in:
Frédéric FRANCE 2021-02-20 15:41:57 +01:00
parent a63daaa8e1
commit 99fa13cbad
No known key found for this signature in database
GPG Key ID: 06809324E4B2ABC1

View File

@ -7,7 +7,7 @@
* Copyright (C) 2013-2014 Juanjo Menent <jmenent@2byte.es> * Copyright (C) 2013-2014 Juanjo Menent <jmenent@2byte.es>
* Copyright (C) 2013 Christophe Battarel <contact@altairis.fr> * Copyright (C) 2013 Christophe Battarel <contact@altairis.fr>
* Copyright (C) 2013-2018 Alexandre Spangaro <aspangaro@open-dsi.fr> * Copyright (C) 2013-2018 Alexandre Spangaro <aspangaro@open-dsi.fr>
* Copyright (C) 2015-2019 Frédéric France <frederic.france@netlogic.fr> * Copyright (C) 2015-2021 Frédéric France <frederic.france@netlogic.fr>
* Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr> * Copyright (C) 2015 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
* Copyright (C) 2017 Rui Strecht <rui.strecht@aliartalentos.com> * Copyright (C) 2017 Rui Strecht <rui.strecht@aliartalentos.com>
* Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es> * Copyright (C) 2018 Ferran Marcet <fmarcet@2byte.es>
@ -53,20 +53,46 @@ function societe_prepare_head(Societe $object)
if (empty($conf->global->MAIN_SUPPORT_SHARED_CONTACT_BETWEEN_THIRDPARTIES)) { if (empty($conf->global->MAIN_SUPPORT_SHARED_CONTACT_BETWEEN_THIRDPARTIES)) {
if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->societe->contact->lire) { if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->societe->contact->lire) {
//$nbContact = count($object->liste_contact(-1,'internal')) + count($object->liste_contact(-1,'external')); //$nbContact = count($object->liste_contact(-1,'internal')) + count($object->liste_contact(-1,'external'));
$nbContact = 0; // TODO $nbContact = 0;
if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) {
$sql = "SELECT COUNT(p.rowid) as nb"; // Using a memcached/memcache server
$sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p"; $usecachekey = 'thirdparty_countcontact_'.$object->id;
$sql .= " WHERE p.fk_soc = ".$object->id; } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) {
$resql = $db->query($sql); // Using cache with shmop
if ($resql) { $usecachekey = 'thirdparty_countcontact_'.$object->id;
$obj = $db->fetch_object($resql); }
if ($obj) $nbContact = $obj->nb; if ($usecachekey) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$dataretrieved = dol_getcache($usecachekey);
if (is_array($dataretrieved) && count($dataretrieved)) {
$nbContact = $dataretrieved[$usecachekey];
$found = true;
}
}
if (!$found) {
$sql = "SELECT COUNT(p.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
$sql .= " WHERE p.fk_soc = ".$object->id;
$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
$nbContact = $obj->nb;
}
if ($usecachekey) {
$datatocache = array();
$datatocache[$usecachekey] = $nbContact;
$res_setcache = dol_setcache($usecachekey, $datatocache);
if ($res_setcache < 0) {
$error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache;
dol_syslog($error, LOG_ERR);
}
}
} }
$head[$h][0] = DOL_URL_ROOT.'/societe/contact.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/societe/contact.php?socid='.$object->id;
$head[$h][1] = $langs->trans('ContactsAddresses'); $head[$h][1] = $langs->trans('ContactsAddresses');
if ($nbContact > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>'; if ($nbContact > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
}
$head[$h][2] = 'contact'; $head[$h][2] = 'contact';
$h++; $h++;
} }
@ -74,7 +100,9 @@ function societe_prepare_head(Societe $object)
$head[$h][0] = DOL_URL_ROOT.'/societe/societecontact.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/societe/societecontact.php?socid='.$object->id;
$nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external')); $nbContact = count($object->liste_contact(-1, 'internal')) + count($object->liste_contact(-1, 'external'));
$head[$h][1] = $langs->trans("ContactsAddresses"); $head[$h][1] = $langs->trans("ContactsAddresses");
if ($nbContact > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>'; if ($nbContact > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbContact.'</span>';
}
$head[$h][2] = 'contact'; $head[$h][2] = 'contact';
$h++; $h++;
} }
@ -82,9 +110,15 @@ function societe_prepare_head(Societe $object)
if ($object->client == 1 || $object->client == 2 || $object->client == 3) { if ($object->client == 1 || $object->client == 2 || $object->client == 3) {
$head[$h][0] = DOL_URL_ROOT.'/comm/card.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/comm/card.php?socid='.$object->id;
$head[$h][1] = ''; $head[$h][1] = '';
if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && ($object->client == 2 || $object->client == 3)) $head[$h][1] .= $langs->trans("Prospect"); if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && ($object->client == 2 || $object->client == 3)) {
if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && $object->client == 3) $head[$h][1] .= ' | '; $head[$h][1] .= $langs->trans("Prospect");
if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && ($object->client == 1 || $object->client == 3)) $head[$h][1] .= $langs->trans("Customer"); }
if (empty($conf->global->SOCIETE_DISABLE_PROSPECTS) && empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && $object->client == 3) {
$head[$h][1] .= ' | ';
}
if (empty($conf->global->SOCIETE_DISABLE_CUSTOMERS) && ($object->client == 1 || $object->client == 3)) {
$head[$h][1] .= $langs->trans("Customer");
}
$head[$h][2] = 'customer'; $head[$h][2] = 'customer';
$h++; $h++;
@ -98,7 +132,9 @@ function societe_prepare_head(Societe $object)
} }
} }
$supplier_module_enabled = 0; $supplier_module_enabled = 0;
if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_proposal->enabled) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) $supplier_module_enabled = 1; if ((!empty($conf->fournisseur->enabled) && empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)) || !empty($conf->supplier_proposal->enabled) || !empty($conf->supplier_order->enabled) || !empty($conf->supplier_invoice->enabled)) {
$supplier_module_enabled = 1;
}
if ($supplier_module_enabled == 1 && $object->fournisseur && !empty($user->rights->fournisseur->lire)) { if ($supplier_module_enabled == 1 && $object->fournisseur && !empty($user->rights->fournisseur->lire)) {
$head[$h][0] = DOL_URL_ROOT.'/fourn/card.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/fourn/card.php?socid='.$object->id;
$head[$h][1] = $langs->trans("Supplier"); $head[$h][1] = $langs->trans("Supplier");
@ -107,8 +143,6 @@ function societe_prepare_head(Societe $object)
} }
if (!empty($conf->projet->enabled) && (!empty($user->rights->projet->lire))) { if (!empty($conf->projet->enabled) && (!empty($user->rights->projet->lire))) {
$head[$h][0] = DOL_URL_ROOT.'/societe/project.php?socid='.$object->id;
$head[$h][1] = $langs->trans("Projects");
$nbNote = 0; $nbNote = 0;
$sql = "SELECT COUNT(n.rowid) as nb"; $sql = "SELECT COUNT(n.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."projet as n"; $sql .= " FROM ".MAIN_DB_PREFIX."projet as n";
@ -116,17 +150,16 @@ function societe_prepare_head(Societe $object)
$sql .= " AND entity IN (".getEntity('project').")"; $sql .= " AND entity IN (".getEntity('project').")";
$resql = $db->query($sql); $resql = $db->query($sql);
if ($resql) { if ($resql) {
$num = $db->num_rows($resql); $obj = $db->fetch_object($resql);
$i = 0; $nbNote = $obj->nb;
while ($i < $num) {
$obj = $db->fetch_object($resql);
$nbNote = $obj->nb;
$i++;
}
} else { } else {
dol_print_error($db); dol_print_error($db);
} }
if ($nbNote > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>'; $head[$h][0] = DOL_URL_ROOT.'/societe/project.php?socid='.$object->id;
$head[$h][1] = $langs->trans("Projects");
if ($nbNote > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
}
$head[$h][2] = 'project'; $head[$h][2] = 'project';
$h++; $h++;
} }
@ -180,12 +213,16 @@ function societe_prepare_head(Societe $object)
//$title = $langs->trans("BankAccountsAndGateways"); //$title = $langs->trans("BankAccountsAndGateways");
$servicestatus = 0; $servicestatus = 0;
if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) $servicestatus = 1; if (!empty($conf->global->STRIPE_LIVE) && !GETPOST('forcesandbox', 'alpha')) {
$servicestatus = 1;
}
include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php'; include_once DOL_DOCUMENT_ROOT.'/societe/class/societeaccount.class.php';
$societeaccount = new SocieteAccount($db); $societeaccount = new SocieteAccount($db);
$stripecu = $societeaccount->getCustomerAccount($object->id, 'stripe', $servicestatus); // Get thirdparty cu_... $stripecu = $societeaccount->getCustomerAccount($object->id, 'stripe', $servicestatus); // Get thirdparty cu_...
if ($stripecu) $foundonexternalonlinesystem++; if ($stripecu) {
$foundonexternalonlinesystem++;
}
} }
$sql = "SELECT COUNT(n.rowid) as nb"; $sql = "SELECT COUNT(n.rowid) as nb";
@ -199,13 +236,8 @@ function societe_prepare_head(Societe $object)
$resql = $db->query($sql); $resql = $db->query($sql);
if ($resql) { if ($resql) {
$num = $db->num_rows($resql); $obj = $db->fetch_object($resql);
$i = 0; $nbBankAccount = $obj->nb;
while ($i < $num) {
$obj = $db->fetch_object($resql);
$nbBankAccount = $obj->nb;
$i++;
}
} else { } else {
dol_print_error($db); dol_print_error($db);
} }
@ -214,8 +246,11 @@ function societe_prepare_head(Societe $object)
$head[$h][0] = DOL_URL_ROOT.'/societe/paymentmodes.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/societe/paymentmodes.php?socid='.$object->id;
$head[$h][1] = $title; $head[$h][1] = $title;
if ($foundonexternalonlinesystem) $head[$h][1] .= '<span class="badge marginleftonlyshort">...</span>'; if ($foundonexternalonlinesystem) {
elseif ($nbBankAccount > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbBankAccount.'</span>'; $head[$h][1] .= '<span class="badge marginleftonlyshort">...</span>';
} elseif ($nbBankAccount > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbBankAccount.'</span>';
}
$head[$h][2] = 'rib'; $head[$h][2] = 'rib';
$h++; $h++;
} }
@ -229,17 +264,14 @@ function societe_prepare_head(Societe $object)
$sql .= " WHERE fk_soc = ".$object->id.' AND fk_website > 0'; $sql .= " WHERE fk_soc = ".$object->id.' AND fk_website > 0';
$resql = $db->query($sql); $resql = $db->query($sql);
if ($resql) { if ($resql) {
$num = $db->num_rows($resql); $obj = $db->fetch_object($resql);
$i = 0; $nbNote = $obj->nb;
while ($i < $num) {
$obj = $db->fetch_object($resql);
$nbNote = $obj->nb;
$i++;
}
} else { } else {
dol_print_error($db); dol_print_error($db);
} }
if ($nbNote > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>'; if ($nbNote > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
}
$head[$h][2] = 'website'; $head[$h][2] = 'website';
$h++; $h++;
} }
@ -253,26 +285,52 @@ function societe_prepare_head(Societe $object)
if ($user->socid == 0) { if ($user->socid == 0) {
// Notifications // Notifications
if (!empty($conf->notification->enabled)) { if (!empty($conf->notification->enabled)) {
$nbNote = 0; // Enable caching of thirdrparty count notifications
$sql = "SELECT COUNT(n.rowid) as nb"; $usecachekey = '';
$sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n"; $found = false;
$sql .= " WHERE fk_soc = ".$object->id; $nbNotif = 0;
$resql = $db->query($sql); if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) {
if ($resql) { // Using a memcached/memcache server
$num = $db->num_rows($resql); $usecachekey = 'thirdparty_countnotif_'.$object->id;
$i = 0; } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) {
while ($i < $num) { // Using cache with shmop
$obj = $db->fetch_object($resql); $usecachekey = 'thirdparty_countnotif_'.$object->id;
$nbNote = $obj->nb; }
$i++; if ($usecachekey) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$dataretrieved = dol_getcache($usecachekey);
if (is_array($dataretrieved) && count($dataretrieved)) {
$nbNotif = $dataretrieved[$usecachekey];
$found = true;
}
}
if (!$found) {
$sql = "SELECT COUNT(n.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n";
$sql .= " WHERE fk_soc = ".$object->id;
$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
$nbNotif = $obj->nb;
} else {
dol_print_error($db);
}
if ($usecachekey) {
$datatocache = array();
$datatocache[$usecachekey] = $nbNotif;
$res_setcache = dol_setcache($usecachekey, $datatocache);
if ($res_setcache < 0) {
$error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache;
dol_syslog($error, LOG_ERR);
}
} }
} else {
dol_print_error($db);
} }
$head[$h][0] = DOL_URL_ROOT.'/societe/notify/card.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/societe/notify/card.php?socid='.$object->id;
$head[$h][1] = $langs->trans("Notifications"); $head[$h][1] = $langs->trans("Notifications");
if ($nbNote > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>'; if ($nbNotif > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNotif.'</span>';
}
$head[$h][2] = 'notify'; $head[$h][2] = 'notify';
$h++; $h++;
} }
@ -283,7 +341,9 @@ function societe_prepare_head(Societe $object)
if (!empty($object->note_public)) $nbNote++; if (!empty($object->note_public)) $nbNote++;
$head[$h][0] = DOL_URL_ROOT.'/societe/note.php?id='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/societe/note.php?id='.$object->id;
$head[$h][1] = $langs->trans("Notes"); $head[$h][1] = $langs->trans("Notes");
if ($nbNote > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>'; if ($nbNote > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbNote.'</span>';
}
$head[$h][2] = 'note'; $head[$h][2] = 'note';
$h++; $h++;
@ -296,7 +356,9 @@ function societe_prepare_head(Societe $object)
$head[$h][0] = DOL_URL_ROOT.'/societe/document.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/societe/document.php?socid='.$object->id;
$head[$h][1] = $langs->trans("Documents"); $head[$h][1] = $langs->trans("Documents");
if (($nbFiles + $nbLinks) > 0) $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>'; if (($nbFiles + $nbLinks) > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
}
$head[$h][2] = 'document'; $head[$h][2] = 'document';
$h++; $h++;
} }
@ -304,15 +366,16 @@ function societe_prepare_head(Societe $object)
$head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id; $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id;
$head[$h][1] = $langs->trans("Events"); $head[$h][1] = $langs->trans("Events");
if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) { if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) {
// Enable caching of count actioncomm // Enable caching of thirdrparty count actioncomm
$usecachekey = ''; $usecachekey = '';
$found = false; $found = false;
$nbEvent = 0;
if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) { if (!empty($conf->memcached->enabled) && !empty($conf->global->MEMCACHED_SERVER)) {
// Using a memcached/memcache server // Using a memcached/memcache server
$usecachekey = 'count_event_'.$object->id; $usecachekey = 'thirdparty_countevent_'.$object->id;
} elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) { } elseif (isset($conf->global->MAIN_OPTIMIZE_SPEED) && ($conf->global->MAIN_OPTIMIZE_SPEED & 0x02)) {
// Using cache with shmop // Using cache with shmop
$usecachekey = 'count_event_'.$object->id; $usecachekey = 'thirdparty_countevent_'.$object->id;
} }
if ($usecachekey) { if ($usecachekey) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
@ -323,7 +386,6 @@ function societe_prepare_head(Societe $object)
} }
} }
if (!$found) { if (!$found) {
$nbEvent = 0;
$sql = "SELECT COUNT(id) as nb"; $sql = "SELECT COUNT(id) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
$sql .= " WHERE fk_soc = ".$object->id; $sql .= " WHERE fk_soc = ".$object->id;
@ -337,8 +399,8 @@ function societe_prepare_head(Societe $object)
if ($usecachekey) { if ($usecachekey) {
$datatocache = array(); $datatocache = array();
$datatocache[$usecachekey] = $nbEvent; $datatocache[$usecachekey] = $nbEvent;
$ressetcache = dol_setcache($usecachekey, $datatocache); $res_setcache = dol_setcache($usecachekey, $datatocache);
if ($ressetcache < 0) { if ($res_setcache < 0) {
$error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache; $error = 'Failed to set cache for usecachekey='.$usecachekey.' result='.$ressetcache;
dol_syslog($error, LOG_ERR); dol_syslog($error, LOG_ERR);
} }