As I explained in the section that you just read, it’s necessary to define a method within the previous cache class that allows you to determine whether or not a specified cache file is valid. Of course, there are many strategies one can use for doing this, but the one I’ll use here will be based on a predefined expiry time, which will have a default value of 60 seconds. Having outlined the strategy that we will use with the cache class, here’s the file that contains its full source code, this time including the method that verifies the validity of a determined cache file: (Cache.php) class Cache { private $cachedir = 'cache/'; private $expire = 60; private static $instance = NULL;
// get Singleton instance of Cache class public static function getInstance($cachedir = '') { if (self::$instance === NULL) { self::$instance = new self($cachedir); } return self::$instance; }
// constructor public function __construct($cachedir = '') { if ($cachedir !== '') { if (!is_dir($cachedir) or !is_writable($cachedir)) { throw new Exception('Cache directory must be a valid writeable directory.'); } $this->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 By way of the brand new “valid()” method, the caching class is now capable of checking to see if a specified cache is valid. For this example, the same expiry value is assigned to all of the cache files created, but with minor tweaks it’s possible to assign expiry times to each file individually. And with this last code fragment, I’m finishing this seventh part of the series. Now the sample framework can cache data via the caching class that you saw before. I know it’s still pretty hard to see how most of the classes built so far will fit in the framework’s MVC layer, but as I said before, be patient, as everything will be revealed at the proper time. Final thoughts In this seventh chapter of the series, I added a cache class to the stack of classes that comprise this MVC-based framework. As you saw earlier, the class uses the file system as the underlying back end for caching data, but it’s also possible to create a different class that caches data on shared memory, a SQLite database and so forth. Indeed, the possibilities are numerous. In the next tutorial, things will become even more interesting. I’m going to add to the framework a view class, which will be responsible for rendering data injected by the corresponding controllers on the browser. Don’t miss the upcoming part!
blog comments powered by Disqus |
|
|
|
|
|
|
|