public function __construct(mdb_config $config, imdb_logger $logger) { $this->config = $config; $this->logger = $logger; if (($this->config->usecache || $this->config->storecache) && !is_dir($this->config->cachedir)) { $this->logger->critical("[Cache] Configured cache directory [{$this->config->cachedir}] does not exist!"); throw new imdb_exception("[Cache] Configured cache directory [{$this->config->cachedir}] does not exist!"); } if ($this->config->storecache && !is_writable($this->config->cachedir)) { $this->logger->critical("[Cache] Configured cache directory [{$this->config->cachedir}] lacks write permission!"); throw new imdb_exception("[Cache] Configured cache directory [{$this->config->cachedir}] lacks write permission!"); } }
/** * Check cache and purge outdated files * This method looks for files older than the cache_expire set in the * mdb_config and removes them * @TODO add a limit on how frequently a purge can occur */ public function purge() { $cacheDir = $this->config->cachedir; $this->logger->debug("[Cache] Purging old cache entries"); if (is_dir($cacheDir)) { if (is_writable($cacheDir)) { $thisdir = dir($cacheDir); $now = time(); while ($file = $thisdir->read()) { if ($file != "." && $file != "..") { $fname = $cacheDir . $file; if (is_dir($fname)) { continue; } $mod = filemtime($fname); if ($mod && $now - $mod > $this->config->cache_expire) { unlink($fname); } } } } elseif (!empty($cacheDir)) { $this->logger->critical("[Cache] Cache directory [{$cacheDir}] lacks write permission - purge aborted."); } } elseif (!empty($cacheDir)) { $this->logger->critical("[Cache] Cache directory [{$cacheDir}] does not exist - purge aborted."); } }