cachedir = $cachedir; } } // write data to cache file given an ID public function set($id, $data) { $file = $this->cachedir . $id; if (file_exists($file)) { unlink($file); } // write data to cache if (!file_put_contents($file, serialize($data))) { throw new Exception('Error writing data to cache file.'); } } // read data from cache file given an ID public function get($id) { $file = glob($this->cachedir . $id); $file = array_shift($file); if (!$data = file_get_contents($file)) { throw new Exception('Error reading data from cache file.'); } return unserialize($data); } // check if the cache file is valid or not public function valid($id) { $file = glob($this->cachedir . $id); $file = array_shift($file); return (bool)(time() - filemtime($file) <= $this->expire); } }// End Cache class