diff --git a/cachefile.php b/cachefile.php index 8f6be19..2dd07cf 100644 --- a/cachefile.php +++ b/cachefile.php @@ -6,6 +6,48 @@ class cachefile /** If TRUE : no information is cached */ public $nocache = false; + /** Check if there is some garbage to clean */ + public function garbage () + { + try + { + $this->cachedir (); + } + catch (Exception $e) + { + throw new Exception ($e->getMessage(), $e->getCode()); + } + + $res = $this->read ("CACHE-Garbage"); + if ($res === false) + { + $this->write ("CACHE-Garbage", "Let's go, garbage !", 24*60*60); + $files = glob ($this->directory."/*", GLOB_NOSORT); + foreach ($files as $fileCache) + { + $datas = file_get_contents ($fileCache); + if ($datas === false) + { + unlink ($fileCache); + continue; + } + + $datas = unserialize ($datas); + if (! isset ($datas["ttl"]) || ! isset ($datas["data"]) || + ! isset ($datas["createTime"])) + { + unlink ($fileCache); + continue; + } + + if (($datas["createTime"] + $datas["ttl"]) >= time ()) + { + unlink ($fileCache); + } + } + } + } + /** This function check if the cachedir exists and create it if it is not the case. Check if the cache dir is writable and readable */ @@ -24,7 +66,7 @@ class cachefile if (!mkdir ($this->directory)) throw new Exception (sprintf (_("Can not create cache directory %s"), $this->directory), 500); - chmod ($this->directory, 0666); + chmod ($this->directory, 0777); } if (! is_writable ($this->directory)) throw new Exception (sprintf (_("Cache directory %s is not writable"),