Merge pull request #16364 from frederic34/memcacheexpire

memcached can set expire
This commit is contained in:
Laurent Destailleur
2021-02-21 20:14:09 +01:00
committed by GitHub
3 changed files with 78 additions and 46 deletions

View File

@@ -52,10 +52,10 @@ 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;
// Enable caching of thirdrparty count Contacts
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_contacts_thirdparty_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
@@ -71,7 +71,7 @@ function societe_prepare_head(Societe $object)
$nbContact = $obj->nb;
}
dol_setcache($cachekey, $nbContact); // If setting cache fails, this is not a problem, so we do not test result.
dol_setcache($cachekey, $nbContact, 120); // 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;
@@ -129,22 +129,32 @@ function societe_prepare_head(Societe $object)
}
if (!empty($conf->projet->enabled) && (!empty($user->rights->projet->lire))) {
$nbNote = 0;
$sql = "SELECT COUNT(n.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."projet as n";
$sql .= " WHERE fk_soc = ".$object->id;
$sql .= " AND entity IN (".getEntity('project').")";
$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
$nbNote = $obj->nb;
$nbProject = 0;
// Enable caching of thirdrparty count projects
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_projects_thirdparty_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$nbProject = $dataretrieved;
} else {
dol_print_error($db);
$sql = "SELECT COUNT(n.rowid) as nb";
$sql .= " FROM ".MAIN_DB_PREFIX."projet as n";
$sql .= " WHERE fk_soc = ".$object->id;
$sql .= " AND entity IN (".getEntity('project').")";
$resql = $db->query($sql);
if ($resql) {
$obj = $db->fetch_object($resql);
$nbProject = $obj->nb;
} else {
dol_print_error($db);
}
dol_setcache($cachekey, $nbProject, 120); // If setting cache fails, this is not a problem, so we do not test result.
}
$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>';
if ($nbProject > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.$nbProject.'</span>';
}
$head[$h][2] = 'project';
$h++;
@@ -271,10 +281,9 @@ 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
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_notifications_thirdparty_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
@@ -290,7 +299,7 @@ function societe_prepare_head(Societe $object)
} else {
dol_print_error($db);
}
dol_setcache($cachekey, $nbNotif); // If setting cache fails, this is not a problem, so we do not test result.
dol_setcache($cachekey, $nbNotif, 120); // 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;
@@ -318,17 +327,28 @@ function societe_prepare_head(Societe $object)
$head[$h][2] = 'note';
$h++;
// Attached files
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
$upload_dir = $conf->societe->multidir_output[$object->entity]."/".$object->id;
$nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
$nbLinks = Link::count($db, $object->element, $object->id);
// Attached files and Links
$totalAttached = 0;
// Enable caching of thirdrparty count attached files and links
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_attached_thirdparty_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$totalAttached = $dataretrieved;
} else {
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
$upload_dir = $conf->societe->multidir_output[$object->entity]."/".$object->id;
$nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
$nbLinks = Link::count($db, $object->element, $object->id);
$totalAttached = $nbFiles + $nbLinks;
dol_setcache($cachekey, $totalAttached, 120); // If setting cache fails, this is not a problem, so we do not test result.
}
$head[$h][0] = DOL_URL_ROOT.'/societe/document.php?socid='.$object->id;
$head[$h][1] = $langs->trans("Documents");
if (($nbFiles + $nbLinks) > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
if (($totalAttached) > 0) {
$head[$h][1] .= '<span class="badge marginleftonlyshort">'.($totalAttached).'</span>';
}
$head[$h][2] = 'document';
$h++;
@@ -337,10 +357,9 @@ 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))) {
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$nbEvent = 0;
// Enable caching of thirdrparty count actioncomm
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'count_events_thirdparty_'.$object->id;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
@@ -356,7 +375,7 @@ function societe_prepare_head(Societe $object)
} else {
dol_syslog('Failed to count actioncomm '.$db->lasterror(), LOG_ERR);
}
dol_setcache($cachekey, $nbEvent); // If setting cache fails, this is not a problem, so we do not test result.
dol_setcache($cachekey, $nbEvent, 120); // If setting cache fails, this is not a problem, so we do not test result.
}
$head[$h][1] .= '/';

View File

@@ -2450,21 +2450,32 @@ function dol_print_email($email, $cid = 0, $socid = 0, $addlink = 0, $max = 64,
function getArrayOfSocialNetworks()
{
global $conf, $db;
$sql = "SELECT rowid, code, label, url, icon, active FROM ".MAIN_DB_PREFIX."c_socialnetworks";
$sql .= " WHERE entity=".$conf->entity;
$socialnetworks = array();
$resql = $db->query($sql);
if ($resql) {
while ($obj = $db->fetch_object($resql)) {
$socialnetworks[$obj->code] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'url' => $obj->url,
'icon' => $obj->icon,
'active' => $obj->active,
);
// Enable caching of array
require_once DOL_DOCUMENT_ROOT.'/core/lib/memory.lib.php';
$cachekey = 'socialnetworks_' . $conf->entity;
$dataretrieved = dol_getcache($cachekey);
if (!is_null($dataretrieved)) {
$socialnetworks = $dataretrieved;
} else {
$sql = "SELECT rowid, code, label, url, icon, active FROM ".MAIN_DB_PREFIX."c_socialnetworks";
$sql .= " WHERE entity=".$conf->entity;
$resql = $db->query($sql);
if ($resql) {
while ($obj = $db->fetch_object($resql)) {
$socialnetworks[$obj->code] = array(
'rowid' => $obj->rowid,
'label' => $obj->label,
'url' => $obj->url,
'icon' => $obj->icon,
'active' => $obj->active,
);
}
}
dol_setcache($cachekey, $socialnetworks); // If setting cache fails, this is not a problem, so we do not test result.
}
return $socialnetworks;
}

View File

@@ -61,10 +61,11 @@ $shmoffset = 1000; // Max number of entries found into a language file. If too l
*
* @param string $memoryid Memory id of shared area
* @param mixed $data Data to save. It must not be a null value.
* @param int $expire ttl in seconds, 0 never expire
* @return int <0 if KO, 0 if nothing is done, Nb of bytes written if OK
* @see dol_getcache()
*/
function dol_setcache($memoryid, $data)
function dol_setcache($memoryid, $data, $expire = 0)
{
global $conf;
$result = 0;
@@ -85,7 +86,7 @@ function dol_setcache($memoryid, $data)
$memoryid = session_name() . '_' . $memoryid;
//$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false);
$dolmemcache->add($memoryid, $data); // This fails if key already exists
$dolmemcache->add($memoryid, $data, $expire); // This fails if key already exists
$rescode = $dolmemcache->getResultCode();
if ($rescode == 0) {
return count($data);
@@ -104,7 +105,7 @@ function dol_setcache($memoryid, $data)
$memoryid = session_name() . '_' . $memoryid;
//$dolmemcache->setOption(Memcached::OPT_COMPRESSION, false);
$result = $dolmemcache->add($memoryid, $data); // This fails if key already exists
$result = $dolmemcache->add($memoryid, $data, false, $expire); // This fails if key already exists
if ($result) {
return count($data);
} else {
@@ -112,7 +113,7 @@ function dol_setcache($memoryid, $data)
}
} 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);
$result = dol_setshmop($memoryid, $data, $expire);
}
return $result;
@@ -226,9 +227,10 @@ function dol_listshmop()
*
* @param int $memoryid Memory id of shared area ('main', 'agenda', ...)
* @param string $data Data to save. Must be a not null value.
* @param int $expire ttl in seconds, 0 never expire
* @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, $expire)
{
global $shmkeys, $shmoffset;