summaryrefslogtreecommitdiff
path: root/lib/Minz/Cache.php
diff options
context:
space:
mode:
authorGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-01-29 21:26:19 +0100
committerGravatar Marien Fressinaud <dev@marienfressinaud.fr> 2014-01-29 21:26:19 +0100
commit165eb57459a152b3cc6aa3fd15ca990c3d908829 (patch)
tree640ddacbc6eabc985646dd65b0578f369ea92321 /lib/Minz/Cache.php
parent75096e6a39fe5d34d3951991f296f616e62a9fd8 (diff)
parentc053825ff8f9792e692c101585481129b006937b (diff)
Sortie de la version 0.70.7.0
Diffstat (limited to 'lib/Minz/Cache.php')
-rw-r--r--lib/Minz/Cache.php116
1 files changed, 116 insertions, 0 deletions
diff --git a/lib/Minz/Cache.php b/lib/Minz/Cache.php
new file mode 100644
index 000000000..fcb627eb2
--- /dev/null
+++ b/lib/Minz/Cache.php
@@ -0,0 +1,116 @@
+<?php
+/**
+ * MINZ - Copyright 2011 Marien Fressinaud
+ * Sous licence AGPL3 <http://www.gnu.org/licenses/>
+*/
+
+/**
+ * La classe Cache permet de gérer facilement les pages en cache
+ */
+class Minz_Cache {
+ /**
+ * $expire timestamp auquel expire le cache de $url
+ */
+ private $expire = 0;
+
+ /**
+ * $file est le nom du fichier de cache
+ */
+ private $file = '';
+
+ /**
+ * $enabled permet de déterminer si le cache est activé
+ */
+ private static $enabled = true;
+
+ /**
+ * Constructeur
+ */
+ public function __construct () {
+ $this->_fileName ();
+ $this->_expire ();
+ }
+
+ /**
+ * Setteurs
+ */
+ public function _fileName () {
+ $file = md5 (Minz_Request::getURI ());
+
+ $this->file = CACHE_PATH . '/'.$file;
+ }
+
+ public function _expire () {
+ if ($this->exist ()) {
+ $this->expire = filemtime ($this->file)
+ + Minz_Configuration::delayCache ();
+ }
+ }
+
+ /**
+ * Permet de savoir si le cache est activé
+ * @return true si activé, false sinon
+ */
+ public static function isEnabled () {
+ return Minz_Configuration::cacheEnabled () && self::$enabled;
+ }
+
+ /**
+ * Active / désactive le cache
+ */
+ public static function switchOn () {
+ self::$enabled = true;
+ }
+ public static function switchOff () {
+ self::$enabled = false;
+ }
+
+ /**
+ * Détermine si le cache de $url a expiré ou non
+ * @return true si il a expiré, false sinon
+ */
+ public function expired () {
+ return time () > $this->expire;
+ }
+
+ /**
+ * Affiche le contenu du cache
+ * @print le code html du cache
+ */
+ public function render () {
+ if ($this->exist ()) {
+ include ($this->file);
+ }
+ }
+
+ /**
+ * Enregistre $html en cache
+ * @param $html le html à mettre en cache
+ */
+ public function cache ($html) {
+ file_put_contents ($this->file, $html);
+ }
+
+ /**
+ * Permet de savoir si le cache existe
+ * @return true si il existe, false sinon
+ */
+ public function exist () {
+ return file_exists ($this->file);
+ }
+
+ /**
+ * Nettoie le cache en supprimant tous les fichiers
+ */
+ public static function clean () {
+ $files = opendir (CACHE_PATH);
+
+ while ($fic = readdir ($files)) {
+ if ($fic != '.' && $fic != '..') {
+ unlink (CACHE_PATH.'/'.$fic);
+ }
+ }
+
+ closedir ($files);
+ }
+}