Add cache feature on fetchObjectByElement

This commit is contained in:
John BOTELLA 2024-03-08 16:55:53 +01:00
parent d1c980b0be
commit c4cb7c0a98

View file

@ -12377,12 +12377,14 @@ function getElementProperties($elementType)
* @param int $element_id Element id (Use this or element_id but not both)
* @param string $element_type Element type ('module' or 'myobject@mymodule' or 'mymodule_myobject')
* @param string $element_ref Element ref (Use this or element_id but not both)
* @param int $useCache if you want to store object in cache or get it from cache 0 => no use cache , 1 use cache, 2 force reload cache
* @param int $maxCacheByType number of object in cache for this element type
* @return int|object object || 0 || <0 if error
* @see getElementProperties()
*/
function fetchObjectByElement($element_id, $element_type, $element_ref = '')
function fetchObjectByElement($element_id, $element_type, $element_ref = '', $useCache = 0, $maxCacheByType = 10)
{
global $db;
global $db, $globalCacheForGetObjectFromCache;
$ret = 0;
@ -12401,11 +12403,19 @@ function fetchObjectByElement($element_id, $element_type, $element_ref = '')
}
if (is_array($element_prop) && (empty($element_prop['module']) || $ismodenabled)) {
if ($useCache === 1
&& !empty($globalCacheForGetObjectFromCache[$element_type])
&& !empty($globalCacheForGetObjectFromCache[$element_type][$element_id])
&& is_object($globalCacheForGetObjectFromCache[$element_type][$element_id])
) {
return $globalCacheForGetObjectFromCache[$element_type][$element_id];
}
dol_include_once('/'.$element_prop['classpath'].'/'.$element_prop['classfile'].'.class.php');
if (class_exists($element_prop['classname'])) {
$classname = $element_prop['classname'];
$objecttmp = new $classname($db);
$className = $element_prop['classname'];
$objecttmp = new $className($db);
if ($element_id > 0 || !empty($element_ref)) {
$ret = $objecttmp->fetch($element_id, $element_ref);
@ -12413,6 +12423,20 @@ function fetchObjectByElement($element_id, $element_type, $element_ref = '')
if (empty($objecttmp->module)) {
$objecttmp->module = $element_prop['module'];
}
if ($useCache > 0) {
if (!isset($globalCacheForGetObjectFromCache[$element_type])) {
$globalCacheForGetObjectFromCache[$element_type] = [];
}
// Manage cache limit
if (! empty($globalCacheForGetObjectFromCache[$element_type]) && is_array($globalCacheForGetObjectFromCache[$element_type]) && count($globalCacheForGetObjectFromCache[$element_type]) >= $maxCacheByType) {
array_shift($globalCacheForGetObjectFromCache[$element_type]);
}
$globalCacheForGetObjectFromCache[$element_type][$element_id] = $objecttmp;
}
return $objecttmp;
}
} else {