diff --git a/inc-cache.php3 b/inc-cache.php3 index 0924f8858a3120dd6e530ce74a8e454b67066ee7..3dc69bd1048affa976ed599272f02cf171796602 100644 --- a/inc-cache.php3 +++ b/inc-cache.php3 @@ -41,18 +41,32 @@ function utiliser_cache($chemin_cache, $delais) { global $HTTP_SERVER_VARS, $HTTP_POST_VARS; global $lastmodified; - // a priori cache + // A priori cache $use_cache = true; - // si le cache existe, verifier sa date - if (file_exists($chemin_cache)) { + // Existence du fichier + if (!file_exists($chemin_cache)) { + if (file_exists($chemin_cache.'.NEW')) { + // Deuxieme acces : le fichier est marque comme utilise + @rename($chemin_cache.'.NEW', $chemin_cache); + clearstatcache(); + } + else { + // Double verification (cas renommage entre les deux file_exists) + clearstatcache(); + $use_cache = file_exists($chemin_cache); + } + } + + // Date de creation du fichier + if ($use_cache) { $t = filemtime($chemin_cache); $ledelais = time() - $t; $use_cache &= ($ledelais < $delais AND $ledelais >= 0); + if (!$use_cache) @unlink($chemin_cache); // Inclusions multiples : derniere modification if ($lastmodified < $t) $lastmodified = $t; - } else - $use_cache = false; + } // recalcul obligatoire $use_cache &= ($GLOBALS['recalcul'] != 'oui'); @@ -76,6 +90,7 @@ function ecrire_fichier_cache($fichier, $contenu) { global $flag_flock; $fichier_tmp = $fichier.'_tmp'; + $fichier = $fichier.'.NEW'; $f = fopen($fichier_tmp, "wb"); if (!$f) return; @@ -92,6 +107,7 @@ function ecrire_fichier_cache($fichier, $contenu) { @unlink($fichier); rename($fichier_tmp, $fichier); if ($GLOBALS['flag_apc']) apc_rm($fichier); + return $fichier; } @@ -134,7 +150,7 @@ function purger_repertoire($dir, $age, $regexp = '') { if ($regexp AND !ereg($regexp, $fichier)) continue; $chemin = "$dir/$fichier"; if (is_file($chemin)) { - if (($t - filemtime($chemin)) > $age) { + if (($t - filemtime($chemin)) > $age OR ereg('\.NEW$', $fichier)) { @unlink($chemin); $query = "DELETE FROM spip_forum_cache WHERE fichier='$fichier'"; spip_query($query); diff --git a/inc-public-global.php3 b/inc-public-global.php3 index 82278ae1b067c58023709f1d2395b23e9f4ee4ec..7a6a02b7c491c4cba314511419994b6f47c701fa 100644 --- a/inc-public-global.php3 +++ b/inc-public-global.php3 @@ -32,7 +32,7 @@ function inclure_fichier($fond, $delais, $contexte_inclus = "") { $timer = ceil(1000 * ($timer_b[0] + $timer_b[1] - $timer_a[0] - $timer_a[1])); $taille = ceil(strlen($page) / 1024); spip_log("inclus ($timer ms): $chemin_cache ($taille ko, delai: $delais s)"); - ecrire_fichier_cache($chemin_cache, $page); + $chemin_cache = ecrire_fichier_cache($chemin_cache, $page); } } return $chemin_cache; @@ -79,7 +79,7 @@ if ($ajout_forum) { if (!$use_cache) { $lastmodified = time(); - if (($lastmodified - lire_meta('date_purge_cache')) > 24 * 3600) { + if (($lastmodified - lire_meta('date_purge_cache')) > 3600) { ecrire_meta('date_purge_cache', $lastmodified); $f = fopen('CACHE/.purge', 'w'); fclose($f); @@ -110,7 +110,7 @@ if (!$use_cache) { $texte = "<"."?php @header (\"Location: $url\"); ?".">"; $calculer_cache = false; spip_log("redirection: $url"); - ecrire_fichier_cache($chemin_cache, $texte); + $chemin_cache = ecrire_fichier_cache($chemin_cache, $texte); } } @@ -123,7 +123,7 @@ if (!$use_cache) { $timer = ceil(1000 * ($timer_b[0] + $timer_b[1] - $timer_a[0] - $timer_a[1])); $taille = ceil(strlen($page) / 1024); spip_log("calcul ($timer ms): $chemin_cache ($taille ko, delai: $delais s)"); - ecrire_fichier_cache($chemin_cache, $page); + $chemin_cache = ecrire_fichier_cache($chemin_cache, $page); } } } @@ -164,7 +164,7 @@ else { // envoyer la page if (file_exists($chemin_cache) && ($HTTP_SERVER_VARS['REQUEST_METHOD'] != 'HEAD')) { - include ($chemin_cache); + include($chemin_cache); } @@ -261,8 +261,8 @@ if (!$timeout AND lire_meta('quoi_de_neuf') == 'oui' AND $jours_neuf = lire_meta // -// Faire du menage dans le cache (effacer les fichiers tres anciens) -// Se declenche une fois par jour quand le cache n'est pas recalcule +// Faire du menage dans le cache (effacer les fichiers tres anciens ou inutilises) +// Se declenche une fois par heure quand le cache n'est pas recalcule // if (!$timeout AND $use_cache AND file_exists('CACHE/.purge2')) { include_ecrire('inc_connect.php3'); @@ -286,12 +286,13 @@ if (!$timeout AND $use_cache AND file_exists('CACHE/.purge2')) { if (!$timeout AND $use_cache AND file_exists('CACHE/.purge')) { include_ecrire('inc_connect.php3'); if ($db_ok) { + $dir = 'CACHE/'.dechex((time() / 3600) & 0xF); unlink('CACHE/.purge'); - spip_log("purge cache niveau 1"); + spip_log("purge cache niveau 1: $dir"); $f = fopen('CACHE/.purge2', 'w'); fclose($f); include_local ("inc-cache.php3"); - purger_repertoire('CACHE', 14 * 24 * 3600); + purger_repertoire($dir, 14 * 24 * 3600); $timeout = true; } }