diff --git a/htdocs/core/lib/company.lib.php b/htdocs/core/lib/company.lib.php index 238f069c919..1da9f5faedd 100644 --- a/htdocs/core/lib/company.lib.php +++ b/htdocs/core/lib/company.lib.php @@ -52,13 +52,15 @@ function societe_prepare_head(Societe $object) if (empty($conf->global->MAIN_SUPPORT_SHARED_CONTACT_BETWEEN_THIRDPARTIES)) { if (empty($conf->global->MAIN_DISABLE_CONTACTS_TAB) && $user->rights->societe->contact->lire) { + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + //$nbContact = count($object->liste_contact(-1,'internal')) + count($object->liste_contact(-1,'external')); $nbContact = 0; - $cachekey = 'thirdparty_countcontact_'.$object->id; - require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $cachekey = 'thirdparty_'.$object->id.'_countcontacts'; $dataretrieved = dol_getcache($cachekey); - if (is_array($dataretrieved) && count($dataretrieved)) { - $nbContact = $dataretrieved[$cachekey]; + + if (!is_null($dataretrieved)) { + $nbContact = $dataretrieved; } else { $sql = "SELECT COUNT(p.rowid) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p"; @@ -68,14 +70,10 @@ function societe_prepare_head(Societe $object) $obj = $db->fetch_object($resql); $nbContact = $obj->nb; } - $datatocache = array(); - $datatocache[$cachekey] = $nbContact; - $res_setcache = dol_setcache($cachekey, $datatocache); - if ($res_setcache < 0) { - $error = 'Failed to set cache for cachekey='.$cachekey.' result='.$res_setcache; - dol_syslog($error, LOG_ERR); - } + + dol_setcache($cachekey, $nbContact); // If setting cache fails, this is not a problem, so we do not test result. } + $head[$h][0] = DOL_URL_ROOT.'/societe/contact.php?socid='.$object->id; $head[$h][1] = $langs->trans('ContactsAddresses'); if ($nbContact > 0) { @@ -273,13 +271,14 @@ function societe_prepare_head(Societe $object) if ($user->socid == 0) { // Notifications if (!empty($conf->notification->enabled)) { + require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $nbNotif = 0; // Enable caching of thirdrparty count notifications - $cachekey = 'thirdparty_countnotif_'.$object->id; - require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + $cachekey = 'thirdparty_'.$object->id.'_countnotifications'; $dataretrieved = dol_getcache($cachekey); - if (is_array($dataretrieved) && count($dataretrieved)) { - $nbNotif = $dataretrieved[$cachekey]; + if (!is_null($dataretrieved)) { + $nbNotif = $dataretrieved; } else { $sql = "SELECT COUNT(n.rowid) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n"; @@ -291,14 +290,9 @@ function societe_prepare_head(Societe $object) } else { dol_print_error($db); } - $datatocache = array(); - $datatocache[$cachekey] = $nbNotif; - $res_setcache = dol_setcache($cachekey, $datatocache); - if ($res_setcache < 0) { - $error = 'Failed to set cache for cachekey='.$cachekey.' result='.$res_setcache; - dol_syslog($error, LOG_ERR); - } + dol_setcache($cachekey, $nbNotif); // If setting cache fails, this is not a problem, so we do not test result. } + $head[$h][0] = DOL_URL_ROOT.'/societe/notify/card.php?socid='.$object->id; $head[$h][1] = $langs->trans("Notifications"); if ($nbNotif > 0) { @@ -343,13 +337,14 @@ function societe_prepare_head(Societe $object) $head[$h][0] = DOL_URL_ROOT.'/societe/agenda.php?socid='.$object->id; $head[$h][1] = $langs->trans("Events"); if (!empty($conf->agenda->enabled) && (!empty($user->rights->agenda->myactions->read) || !empty($user->rights->agenda->allactions->read))) { - // Enable caching of thirdrparty count actioncomm - $nbEvent = 0; - $cachekey = 'thirdparty_countevent_'.$object->id; require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php'; + + $nbEvent = 0; + // Enable caching of thirdrparty count actioncomm + $cachekey = 'thirdparty_'.$object->id.'_countevents'; $dataretrieved = dol_getcache($cachekey); - if (is_array($dataretrieved) && count($dataretrieved)) { - $nbEvent = $dataretrieved[$cachekey]; + if (!is_null($dataretrieved)) { + $nbEvent = $dataretrieved; } else { $sql = "SELECT COUNT(id) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."actioncomm"; @@ -361,14 +356,9 @@ function societe_prepare_head(Societe $object) } else { dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR); } - $datatocache = array(); - $datatocache[$cachekey] = $nbEvent; - $res_setcache = dol_setcache($cachekey, $datatocache); - if ($res_setcache < 0) { - $error = 'Failed to set cache for cachekey='.$cachekey.' result='.$res_setcache; - dol_syslog($error, LOG_ERR); - } + dol_setcache($cachekey, $nbEvent); // If setting cache fails, this is not a problem, so we do not test result. } + $head[$h][1] .= '/'; $head[$h][1] .= $langs->trans("Agenda"); if ($nbEvent > 0) { diff --git a/htdocs/core/lib/memory.lib.php b/htdocs/core/lib/memory.lib.php index 11fa6082b05..818ff0bf171 100644 --- a/htdocs/core/lib/memory.lib.php +++ b/htdocs/core/lib/memory.lib.php @@ -88,7 +88,7 @@ function dol_setcache($memoryid, $data) } else { return -$rescode; } - } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) { + } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) { // This is a really not reliable cache ! Use Memcached instead. // Using a memcache server global $dolmemcache; if (empty($dolmemcache) || !is_object($dolmemcache)) { @@ -106,7 +106,7 @@ function dol_setcache($memoryid, $data) } else { return -1; } - } 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)) { // This is a really not reliable cache ! Use Memcached instead. // Using shmop $result = dol_setshmop($memoryid, $data); } @@ -140,14 +140,16 @@ function dol_getcache($memoryid) //print "Get memoryid=".$memoryid; $data = $m->get($memoryid); $rescode = $m->getResultCode(); - //print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n
"; + //print "memoryid=".$memoryid." - rescode=".$rescode." - count(response)=".count($data)."\n
"; //var_dump($data); if ($rescode == 0) { return $data; + } elseif ($rescode == 16) { // = Memcached::MEMCACHED_NOTFOUND but this constant doe snot exists. + return null; } else { return -$rescode; } - } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) { + } elseif (!empty($conf->memcached->enabled) && class_exists('Memcache')) { // This is a really not reliable cache ! Use Memcached instead. global $m; if (empty($m) || !is_object($m)) { $m = new Memcache(); @@ -164,9 +166,9 @@ function dol_getcache($memoryid) if ($data) { return $data; } else { - return -1; + return null; // There is no way to make a difference between NOTFOUND and error when using Memcache. So do not use it, use Memcached instead. } - } 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)) { // This is a really not reliable cache ! Use Memcached instead. // Using shmop $data = dol_getshmop($memoryid); return $data; @@ -186,7 +188,7 @@ function dol_getcache($memoryid) function dol_getshmopaddress($memoryid) { global $shmkeys, $shmoffset; - if (empty($shmkeys[$memoryid])) { + if (empty($shmkeys[$memoryid])) { // No room reserved for thid memoryid, no way to use cache return 0; } return $shmkeys[$memoryid] + $shmoffset; @@ -215,8 +217,8 @@ function dol_listshmop() * Save data into a memory area shared by all users, all sessions on server * * @param int $memoryid Memory id of shared area ('main', 'agenda', ...) - * @param string $data Data to save - * @return int <0 if KO, Nb of bytes written if OK + * @param string $data Data to save. Must be a not null value. + * @return int <0 if KO, 0=Caching not available, Nb of bytes written if OK */ function dol_setshmop($memoryid, $data) { @@ -225,6 +227,8 @@ function dol_setshmop($memoryid, $data) //print 'dol_setshmop memoryid='.$memoryid."
\n"; if (empty($shmkeys[$memoryid]) || !function_exists("shmop_write")) return 0; $shmkey = dol_getshmopaddress($memoryid); + if (empty($shmkey)) return 0; // No key reserved for this memoryid, we can't cache this memoryid + $newdata = serialize($data); $size = strlen($newdata); //print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes
\n"; @@ -247,14 +251,20 @@ function dol_setshmop($memoryid, $data) * Read a memory area shared by all users, all sessions on server * * @param string $memoryid Memory id of shared area ('main', 'agenda', ...) - * @return int <0 if KO, data if OK + * @return int <0 if KO, data if OK, Null if no cache enabled or not found */ function dol_getshmop($memoryid) { global $shmkeys, $shmoffset; - if (empty($shmkeys[$memoryid]) || !function_exists("shmop_open")) return 0; + $data = null; + + if (empty($shmkeys[$memoryid]) || !function_exists("shmop_open")) { + return null; + } $shmkey = dol_getshmopaddress($memoryid); + if (empty($shmkey)) return null; // No key reserved for this memoryid, we can't cache this memoryid + //print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."
\n"; $handle = @shmop_open($shmkey, 'a', 0, 0); if ($handle) { @@ -263,7 +273,7 @@ function dol_getshmop($memoryid) else return -1; shmop_close($handle); } else { - return -2; + return null; // Can't open existing block, so we suppose it was not created, so nothing were cached yet for the memoryid } return $data; }