From c4cb7c0a987ee21f2e86a03e93494530170cb285 Mon Sep 17 00:00:00 2001 From: John BOTELLA Date: Fri, 8 Mar 2024 16:55:53 +0100 Subject: [PATCH] Add cache feature on fetchObjectByElement --- htdocs/core/lib/functions.lib.php | 32 +++++++++++++++++++++++++++---- 1 file changed, 28 insertions(+), 4 deletions(-) diff --git a/htdocs/core/lib/functions.lib.php b/htdocs/core/lib/functions.lib.php index ce423636f28..cdca2411b67 100644 --- a/htdocs/core/lib/functions.lib.php +++ b/htdocs/core/lib/functions.lib.php @@ -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 {