-
-
Save johnulist/219c79cae22ddad74f21b361e3eeb410 to your computer and use it in GitHub Desktop.
Script pour vider le cache de prestashop 1.7
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php declare(strict_types=1); | |
/** | |
* Script pour vider le cache de prestashop. | |
* | |
* Résoud le problème de lenteur lié a l'exces de ressource consommé par le vidage du cache. | |
* Script a placer à la racine de l'installation de prestashop. | |
* | |
* Fonctionnement | |
* 1 - création d'un dossier vide destiné a être le nouveau dossier de cache. | |
* 2 - renomage du dossier de cache actuel | |
* 3 - renomage du nouveau dossier (1) pour devenir le dossier de cache | |
* | |
* Note : pas de traitement de droits car le script tourne sous le bon utilisateur pour ne pas avoir besoin de les changer. | |
* | |
* @author Sébastien Monterisi <[email protected]> | |
*/ | |
// config | |
$future_cache_dir = __DIR__ . '/var/cache_new'; | |
$current_cache_dir = __DIR__ . '/var/cache'; | |
$old_cache_dir = __DIR__ . '/var/cache_old'; | |
define('key','mettre_une_clé_ici'); | |
try | |
{ | |
ob_start(); | |
// 0 - vérif clé de sécurité | |
if($_GET['k'] !== key) { | |
throw new RuntimeException('invalid key'); | |
} | |
// 1 - Création du dossier de cache | |
exec("rm -rf $future_cache_dir"); | |
mkdir($future_cache_dir); | |
assert_exists($future_cache_dir, 'future dossier de cache'); | |
// 2 - renomage du dossier de cache actuel | |
exec("rm -rf $old_cache_dir"); | |
assert_not_exists($old_cache_dir, 'ancien dossier de cache'); | |
assert_exists($current_cache_dir, 'dossier de cache actuel'); | |
rename(__DIR__ . '/var/cache', __DIR__ . '/var/cache_old'); | |
assert_exists($old_cache_dir, 'ancien dossier de cache'); | |
assert_not_exists($current_cache_dir, 'dossier de cache actuel'); | |
// 3 - renomage du nouveau dossier (1) pour devenir le dossier de cache | |
rename($future_cache_dir, $current_cache_dir); | |
assert_exists($current_cache_dir, 'dossier de cache actuel'); | |
header('Script: Cache cleared OK', true, 200); | |
ob_flush(); | |
echo "Ok."; | |
exit(0); | |
} catch (RuntimeException $exception) | |
{ | |
header("Script: Cache cleared failed - {$exception->getMessage()}", true, 500); | |
echo "Erreur : " . $exception->getMessage(); | |
ob_flush(); | |
exit(1); | |
} | |
/** | |
* Vérifie l'existance d'un fichier ou dossier. | |
* | |
* Lève une exception si n'existe pas. | |
* | |
* @param string $path | |
* @param string $description | |
* | |
* @throws RuntimeException | |
*/ | |
function assert_exists(string $path, string $description) | |
{ | |
if (!file_exists($path)) | |
{ | |
throw new RuntimeException("Erreur : le dossier $path ($description) n'existe pas."); | |
} | |
} | |
function assert_not_exists(string $path, string $description) | |
{ | |
if (file_exists($path)) | |
{ | |
throw new RuntimeException("Erreur : le dossier $path ($description) existe."); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment