From 2db62b7e8076145e45ebeab31f664b94ece111a9 Mon Sep 17 00:00:00 2001 From: "Laurent Destailleur (aka Eldy)" Date: Sun, 19 Jan 2025 20:56:16 +0100 Subject: [PATCH] Fix cache management --- htdocs/core/lib/website.lib.php | 35 ++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/htdocs/core/lib/website.lib.php b/htdocs/core/lib/website.lib.php index 2de1a8e1bd9..08d284e5a3d 100644 --- a/htdocs/core/lib/website.lib.php +++ b/htdocs/core/lib/website.lib.php @@ -618,7 +618,7 @@ function includeContainer($containerref, $once = 0, $cachedelay = 0) $fullpathcache = ''; // If we ask to use the cache delay if ($cachedelay > 0 && !getDolGlobalString("WEBSITE_DISABLE_CACHE_OF_CONTAINERS")) { - $fullpathcache = DOL_DATA_ROOT.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/temp/'.$websitekey.'-'.$websitepage->id.'-'.$containerref.'cache'; + $fullpathcache = DOL_DATA_ROOT.($conf->entity > 1 ? '/'.$conf->entity : '').'/website/temp/'.$websitekey.'-'.$websitepage->id.'-'.$containerref.'.cache'; } if (empty($includehtmlcontentopened)) { @@ -639,16 +639,27 @@ function includeContainer($containerref, $once = 0, $cachedelay = 0) $tmpoutput = ''; - if ($fullpathcache) { - // TODO Cache management - //$tmpoutput = file_get_contents($fullpathcache); + if ($cachedelay > 0 && $fullpathcache) { + if (is_file($fullpathcache)) { + // Get the last modification time of the file + $lastModifiedTime = dol_filemtime($fullpathcache); + + // Get the current time + $currentTime = time(); + + // Check if the file is not older than X seconds + if (($currentTime - $lastModifiedTime) <= $cachedelay) { + // The file is too recent + $tmpoutput = file_get_contents($fullpathcache); + } + } } - // file_get_contents is not possible because we must execute code with include - //$content = file_get_contents($fullpathfile); - //print preg_replace(array('/^.*]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/ - if (empty($tmpoutput)) { + // file_get_contents is not possible because we must execute code with include + //$content = file_get_contents($fullpathfile); + //print preg_replace(array('/^.*]*>/ims','/<\/body>.*$/ims'), array('', ''), $content);*/ + ob_start(); if ($once) { $res = @include_once $fullpathfile; @@ -658,13 +669,15 @@ function includeContainer($containerref, $once = 0, $cachedelay = 0) $tmpoutput = ob_get_contents(); ob_end_clean(); - print preg_replace(array('/^.*]*>/ims', '/<\/body>.*$/ims'), array('', ''), $tmpoutput); + $tmpoutput = preg_replace(array('/^.*]*>/ims', '/<\/body>.*$/ims'), array('', ''), $tmpoutput); - // Save the content into cache file - if ($fullpathcache) { + // Save the content into cache file if content is lower than 10M + if ($fullpathcache && strlen($tmpoutput) < 10000000) { file_put_contents($fullpathcache, $tmpoutput); dolChmod($fullpathcache); } + + return $tmpoutput; } if (!$res) {