Clean code for cache feature
This commit is contained in:
parent
e81b012489
commit
341cd06252
@ -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_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) {
|
||||||
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
|
||||||
|
|
||||||
//$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;
|
$nbContact = 0;
|
||||||
$cachekey = 'thirdparty_countcontact_'.$object->id;
|
$cachekey = 'thirdparty_'.$object->id.'_countcontacts';
|
||||||
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
|
|
||||||
$dataretrieved = dol_getcache($cachekey);
|
$dataretrieved = dol_getcache($cachekey);
|
||||||
if (is_array($dataretrieved) && count($dataretrieved)) {
|
|
||||||
$nbContact = $dataretrieved[$cachekey];
|
if (!is_null($dataretrieved)) {
|
||||||
|
$nbContact = $dataretrieved;
|
||||||
} else {
|
} else {
|
||||||
$sql = "SELECT COUNT(p.rowid) as nb";
|
$sql = "SELECT COUNT(p.rowid) as nb";
|
||||||
$sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
|
$sql .= " FROM ".MAIN_DB_PREFIX."socpeople as p";
|
||||||
@ -68,14 +70,10 @@ function societe_prepare_head(Societe $object)
|
|||||||
$obj = $db->fetch_object($resql);
|
$obj = $db->fetch_object($resql);
|
||||||
$nbContact = $obj->nb;
|
$nbContact = $obj->nb;
|
||||||
}
|
}
|
||||||
$datatocache = array();
|
|
||||||
$datatocache[$cachekey] = $nbContact;
|
dol_setcache($cachekey, $nbContact); // If setting cache fails, this is not a problem, so we do not test result.
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$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) {
|
if ($nbContact > 0) {
|
||||||
@ -273,13 +271,14 @@ 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)) {
|
||||||
|
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
|
||||||
|
|
||||||
$nbNotif = 0;
|
$nbNotif = 0;
|
||||||
// Enable caching of thirdrparty count notifications
|
// Enable caching of thirdrparty count notifications
|
||||||
$cachekey = 'thirdparty_countnotif_'.$object->id;
|
$cachekey = 'thirdparty_'.$object->id.'_countnotifications';
|
||||||
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
|
|
||||||
$dataretrieved = dol_getcache($cachekey);
|
$dataretrieved = dol_getcache($cachekey);
|
||||||
if (is_array($dataretrieved) && count($dataretrieved)) {
|
if (!is_null($dataretrieved)) {
|
||||||
$nbNotif = $dataretrieved[$cachekey];
|
$nbNotif = $dataretrieved;
|
||||||
} else {
|
} else {
|
||||||
$sql = "SELECT COUNT(n.rowid) as nb";
|
$sql = "SELECT COUNT(n.rowid) as nb";
|
||||||
$sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n";
|
$sql .= " FROM ".MAIN_DB_PREFIX."notify_def as n";
|
||||||
@ -291,14 +290,9 @@ function societe_prepare_head(Societe $object)
|
|||||||
} else {
|
} else {
|
||||||
dol_print_error($db);
|
dol_print_error($db);
|
||||||
}
|
}
|
||||||
$datatocache = array();
|
dol_setcache($cachekey, $nbNotif); // If setting cache fails, this is not a problem, so we do not test result.
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$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 ($nbNotif > 0) {
|
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][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 thirdrparty count actioncomm
|
|
||||||
$nbEvent = 0;
|
|
||||||
$cachekey = 'thirdparty_countevent_'.$object->id;
|
|
||||||
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
|
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);
|
$dataretrieved = dol_getcache($cachekey);
|
||||||
if (is_array($dataretrieved) && count($dataretrieved)) {
|
if (!is_null($dataretrieved)) {
|
||||||
$nbEvent = $dataretrieved[$cachekey];
|
$nbEvent = $dataretrieved;
|
||||||
} else {
|
} else {
|
||||||
$sql = "SELECT COUNT(id) as nb";
|
$sql = "SELECT COUNT(id) as nb";
|
||||||
$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
|
$sql .= " FROM ".MAIN_DB_PREFIX."actioncomm";
|
||||||
@ -361,14 +356,9 @@ function societe_prepare_head(Societe $object)
|
|||||||
} else {
|
} else {
|
||||||
dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR);
|
dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR);
|
||||||
}
|
}
|
||||||
$datatocache = array();
|
dol_setcache($cachekey, $nbEvent); // If setting cache fails, this is not a problem, so we do not test result.
|
||||||
$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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
$head[$h][1] .= '/';
|
$head[$h][1] .= '/';
|
||||||
$head[$h][1] .= $langs->trans("Agenda");
|
$head[$h][1] .= $langs->trans("Agenda");
|
||||||
if ($nbEvent > 0) {
|
if ($nbEvent > 0) {
|
||||||
|
|||||||
@ -88,7 +88,7 @@ function dol_setcache($memoryid, $data)
|
|||||||
} else {
|
} else {
|
||||||
return -$rescode;
|
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
|
// Using a memcache server
|
||||||
global $dolmemcache;
|
global $dolmemcache;
|
||||||
if (empty($dolmemcache) || !is_object($dolmemcache)) {
|
if (empty($dolmemcache) || !is_object($dolmemcache)) {
|
||||||
@ -106,7 +106,7 @@ function dol_setcache($memoryid, $data)
|
|||||||
} else {
|
} else {
|
||||||
return -1;
|
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
|
// Using shmop
|
||||||
$result = dol_setshmop($memoryid, $data);
|
$result = dol_setshmop($memoryid, $data);
|
||||||
}
|
}
|
||||||
@ -140,14 +140,16 @@ function dol_getcache($memoryid)
|
|||||||
//print "Get memoryid=".$memoryid;
|
//print "Get memoryid=".$memoryid;
|
||||||
$data = $m->get($memoryid);
|
$data = $m->get($memoryid);
|
||||||
$rescode = $m->getResultCode();
|
$rescode = $m->getResultCode();
|
||||||
//print "memoryid=".$memoryid." - rescode=".$rescode." - data=".count($data)."\n<br>";
|
//print "memoryid=".$memoryid." - rescode=".$rescode." - count(response)=".count($data)."\n<br>";
|
||||||
//var_dump($data);
|
//var_dump($data);
|
||||||
if ($rescode == 0) {
|
if ($rescode == 0) {
|
||||||
return $data;
|
return $data;
|
||||||
|
} elseif ($rescode == 16) { // = Memcached::MEMCACHED_NOTFOUND but this constant doe snot exists.
|
||||||
|
return null;
|
||||||
} else {
|
} else {
|
||||||
return -$rescode;
|
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;
|
global $m;
|
||||||
if (empty($m) || !is_object($m)) {
|
if (empty($m) || !is_object($m)) {
|
||||||
$m = new Memcache();
|
$m = new Memcache();
|
||||||
@ -164,9 +166,9 @@ function dol_getcache($memoryid)
|
|||||||
if ($data) {
|
if ($data) {
|
||||||
return $data;
|
return $data;
|
||||||
} else {
|
} 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
|
// Using shmop
|
||||||
$data = dol_getshmop($memoryid);
|
$data = dol_getshmop($memoryid);
|
||||||
return $data;
|
return $data;
|
||||||
@ -186,7 +188,7 @@ function dol_getcache($memoryid)
|
|||||||
function dol_getshmopaddress($memoryid)
|
function dol_getshmopaddress($memoryid)
|
||||||
{
|
{
|
||||||
global $shmkeys, $shmoffset;
|
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 0;
|
||||||
}
|
}
|
||||||
return $shmkeys[$memoryid] + $shmoffset;
|
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
|
* 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 int $memoryid Memory id of shared area ('main', 'agenda', ...)
|
||||||
* @param string $data Data to save
|
* @param string $data Data to save. Must be a not null value.
|
||||||
* @return int <0 if KO, Nb of bytes written if OK
|
* @return int <0 if KO, 0=Caching not available, Nb of bytes written if OK
|
||||||
*/
|
*/
|
||||||
function dol_setshmop($memoryid, $data)
|
function dol_setshmop($memoryid, $data)
|
||||||
{
|
{
|
||||||
@ -225,6 +227,8 @@ function dol_setshmop($memoryid, $data)
|
|||||||
//print 'dol_setshmop memoryid='.$memoryid."<br>\n";
|
//print 'dol_setshmop memoryid='.$memoryid."<br>\n";
|
||||||
if (empty($shmkeys[$memoryid]) || !function_exists("shmop_write")) return 0;
|
if (empty($shmkeys[$memoryid]) || !function_exists("shmop_write")) return 0;
|
||||||
$shmkey = dol_getshmopaddress($memoryid);
|
$shmkey = dol_getshmopaddress($memoryid);
|
||||||
|
if (empty($shmkey)) return 0; // No key reserved for this memoryid, we can't cache this memoryid
|
||||||
|
|
||||||
$newdata = serialize($data);
|
$newdata = serialize($data);
|
||||||
$size = strlen($newdata);
|
$size = strlen($newdata);
|
||||||
//print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
|
//print 'dol_setshmop memoryid='.$memoryid." shmkey=".$shmkey." newdata=".$size."bytes<br>\n";
|
||||||
@ -247,14 +251,20 @@ function dol_setshmop($memoryid, $data)
|
|||||||
* Read a memory area shared by all users, all sessions on server
|
* Read a memory area shared by all users, all sessions on server
|
||||||
*
|
*
|
||||||
* @param string $memoryid Memory id of shared area ('main', 'agenda', ...)
|
* @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)
|
function dol_getshmop($memoryid)
|
||||||
{
|
{
|
||||||
global $shmkeys, $shmoffset;
|
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);
|
$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."<br>\n";
|
//print 'dol_getshmop memoryid='.$memoryid." shmkey=".$shmkey."<br>\n";
|
||||||
$handle = @shmop_open($shmkey, 'a', 0, 0);
|
$handle = @shmop_open($shmkey, 'a', 0, 0);
|
||||||
if ($handle) {
|
if ($handle) {
|
||||||
@ -263,7 +273,7 @@ function dol_getshmop($memoryid)
|
|||||||
else return -1;
|
else return -1;
|
||||||
shmop_close($handle);
|
shmop_close($handle);
|
||||||
} else {
|
} 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;
|
return $data;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user